10.1 Bitmap
Bitmap.ARGB_8888 最好啥也有 ,内存大
Bitmap.ARGB_565 没有透明需求就用这个,内存 小一半
创建:
BitmapFactory:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
裁剪
原图
要个狗鼻子
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
//裁剪图片的一部分 从/2这个点开始 截取一个 宽高为原图/3的
Bitmap bitmapCut = Bitmap.createBitmap(bitmapSrc, bitmapSrc.getWidth() / 2, bitmapSrc.getHeight() / 2,
bitmapSrc.getWidth() / 3, bitmapSrc.getHeight() / 3);
mViewBinding.iv.setImageBitmap(bitmapCut);
图片缩放
Bitmap bitmapSrc = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Matrix matrix = new Matrix();
matrix.setScale(2, 1);
//裁剪图片的一部分 从/2这个点开始 截取一个 宽高为原图/3的
Bitmap bitmapCut = Bitmap.createBitmap(bitmapSrc, bitmapSrc.getWidth() / 2, bitmapSrc.getHeight() / 2,
bitmapSrc.getWidth() / 3, bitmapSrc.getHeight() / 3, matrix, true);
mViewBinding.iv.setImageBitmap(bitmapCut);
指定色彩创建图像
简单的纯色
Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.BLUE);
mViewBinding.iv.setImageBitmap(bitmap);
Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.WHITE);
Glide.with(this)
.load(bitmap)
// radius 最大25 sampling 默认1
.apply(RequestOptions.bitmapTransform(new BlurTransformation(20, 10)))
.into(mViewBinding.iv);
没有看出那种毛玻璃的效果
extractAlpha()
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.bikabika);
Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.GRAY);
canvas.drawBitmap(src.extractAlpha(), 0, 0, paint);
mViewBinding.iv.setImageBitmap(bitmap);
src.recycle();
发光
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.bikabika);
//获取AlphaPaint
Paint alphaPaint = new Paint();
BlurMaskFilter blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL);
alphaPaint.setMaskFilter(blurMaskFilter);
int[] offserXY = new int[2];
Bitmap alphaBitmap = src.extractAlpha(alphaPaint, offserXY);
//创建Bitmap
Bitmap bitmap = Bitmap.createBitmap(alphaBitmap.getWidth(), alphaBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint mPaint = new Paint();
mPaint.setColor(Color.GRAY);
canvas.drawBitmap(alphaBitmap, 0, 0, mPaint);
//绘制源图像
canvas.drawBitmap(src, -offserXY[0], -offserXY[1], null);
//设置图像并回收没用的图像资源
mViewBinding.iv.setImageBitmap(bitmap);
src.recycle();
回收图片内存的写法:
if(bmp != null && bmp.isRecycler()){
bmp.recycle();
bmp = null;
system.gc();
}
图片水印效果
public Bitmap creatWaterBitmap(Bitmap src, Bitmap watermark) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//创建空白图像
//创建一个新的和src长度,宽度一样的bitmap
Bitmap newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
//画原图 从0,0 坐标画入
canvas.drawBitmap(src, 0, 0, null);
//在src的右下角画入水印
canvas.drawBitmap(watermark, w - ww + 6, h - wh + 6, null);
return newBitmap;
}