圆角,倒影,圆形图片,截图,获取图片资源

本文介绍了一种在Android应用中处理图像的方法,包括调整大小、获取圆角图片、创建倒影效果及圆形图片等。同时提供了从Drawable资源转换为Bitmap、从ImageView获取Bitmap等实用技巧。

首先是布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

<ImageView 
android:id="@+id/image01"
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10px"
/> 
<ImageView 
android:id="@+id/image02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
/> 

<ImageView 
android:id="@+id/image03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
/> 
</LinearLayout>  



然后是工具类:

package com.example.guangbo;


import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.LinearGradient; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.PixelFormat; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.Rect; 
import android.graphics.RectF; 
import android.graphics.Bitmap.Config; 
import android.graphics.PorterDuff.Mode; 
import android.graphics.Shader.TileMode; 
import android.graphics.drawable.Drawable; 
public class ImageUtil { 
//放大缩小图片
public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){ 
int width = bitmap.getWidth(); 
int height = bitmap.getHeight(); 
Matrix matrix = new Matrix(); 
float scaleWidht = ((float)w / width); 
float scaleHeight = ((float)h / height); 
matrix.postScale(scaleWidht, scaleHeight); 
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
return newbmp; 

//将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable){ 
int width = drawable.getIntrinsicWidth(); 
int height = drawable.getIntrinsicHeight(); 
Bitmap bitmap = Bitmap.createBitmap(width, height, 
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 
: Bitmap.Config.RGB_565); 
Canvas canvas = new Canvas(bitmap); 
drawable.setBounds(0,0,width,height); 
drawable.draw(canvas); 
return bitmap; 

//获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){ 
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
.getHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(output); 
final int color = 0xff424242; 
final Paint paint = new Paint(); 
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
final RectF rectF = new RectF(rect); 
paint.setAntiAlias(true); 
canvas.drawARGB(0, 0, 0, 0); 
paint.setColor(color); 
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
canvas.drawBitmap(bitmap, rect, rect, paint); 
return output; 

//获得带倒影的图片方法
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ 
final int reflectionGap = 4; 
int width = bitmap.getWidth(); 
int height = bitmap.getHeight(); 
Matrix matrix = new Matrix(); 
matrix.preScale(1, -1); 
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 
0, height/2, width, height/2, matrix, false); 
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmapWithReflection); 
canvas.drawBitmap(bitmap, 0, 0, null); 
Paint deafalutPaint = new Paint(); 
canvas.drawRect(0, height,width,height + reflectionGap, 
deafalutPaint); 
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 
Paint paint = new Paint(); 
LinearGradient shader = new LinearGradient(0, 
bitmap.getHeight(), 0, bitmapWithReflection.getHeight() 
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); 
paint.setShader(shader); 
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 
+ reflectionGap, paint); 
return bitmapWithReflection; 
}


public static Bitmap createCircleImage(Bitmap source, int min)  
{  
final Paint paint = new Paint();  
paint.setAntiAlias(true);  
Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);  


//产生一个同样大小的画布 


Canvas canvas = new Canvas(target);  


//  首先绘制圆形 


canvas.drawCircle(min / 2, min / 2, min / 2, paint);  


//     使用SRC_IN 


paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  


//     绘制图片 


canvas.drawBitmap(source, 0, 0, paint);  
return target;  
}  




}  



最后在activity中实现:

public class YuanActivity extends Activity {
private ImageView mImageView01,mImageView02,mImageView03; 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yuan);
super.onCreate(savedInstanceState); 
setupViews(); 


private void setupViews(){ 
mImageView01 = (ImageView)findViewById(R.id.image01); 
mImageView02 = (ImageView)findViewById(R.id.image02); 
mImageView03 = (ImageView)findViewById(R.id.image03);


//Resources res=getResources();
//Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.init_pic);
//bmp=ImageUtil.getRoundedCornerBitmap(bmp, 100.0f);
//Bitmap bb=ImageUtil.drawableToBitmap(tupiantwo.getDrawable());
//mImageView01.setImageBitmap(bmp);


//获取壁纸返回值是Drawable
Drawable drawable = getWallpaper(); 
//将Drawable转化为Bitmap
Bitmap bitmap = ImageUtil.drawableToBitmap(drawable); 
//缩放图片
Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100, 100); 
//获取圆角图片
Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10.0f); 
//获取倒影图片
Bitmap reflectBitmap = ImageUtil.createReflectionImageWithOrigin(zoomBitmap); 
//圆形图片
Bitmap yuan=ImageUtil.createCircleImage(zoomBitmap,60);
//这里可以让Bitmap再转化为Drawable
// Drawable roundDrawable = new BitmapDrawable(roundBitmap);
// Drawable reflectDrawable = new BitmapDrawable(reflectBitmap);
// mImageView01.setBackgroundDrawable(roundDrawable);
// mImageView02.setBackgroundDrawable(reflectDrawable);
mImageView01.setImageBitmap(roundBitmap); 
mImageView02.setImageBitmap(reflectBitmap); 


mImageView03.setImageBitmap(yuan); 



}
}


/////////////////////////////////////////////////////分离线///////////////////////////////////////////////////////////////////////

截图的实现(可以截整个屏幕),也可以直接一小块,拿一个RelativeLayout 做例子:


private RelativeLayout jt;              //声明

jt=(RelativeLayout) findViewById(R.id.jt);  //找到

Bitmap btm=getScreenShot(jt);     //传入RelativeLayout

然后:

//获取截图
private Bitmap getScreenShot(RelativeLayout waterPhoto) {
// View是你需要截图的View
View view = waterPhoto;
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();


/*// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);*/


/*// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();*/

//获取长和高
int width=view.getWidth();
int height=view.getHeight();


// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, 0, width, height);
view.destroyDrawingCache();
return b;
}

截图搞定



//////////////////////////////////////////////////////////////////华丽分界线/////////////////////////////////////////////////////////////////////////////////////////////////////

获取drawable下的图片:

Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.init_pic);


从imageView中获取bitmap:

imageview.setDrawingCacheEnabled(true);   //设置可以获取

imageview.getDrawingCache();                     //获取

imageview.setDrawingCacheEnabled(false);    //清空缓存



//////////////数据转换///////////////////////////

//Bitmap----->byte[]
public byte[] Bitmap2Bytes(Bitmap bm) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

//byte[]------->Bitmap
public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }













源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值