您的位置:首页 > 其它

bitmap和canvas实现图层叠加(可实现灰色遮罩)

2017-04-01 15:39 344 查看
---- bitmap和canvas画出叠加的2张照片



--- 图片1原图



------ 图片2原图



--------- 代码实现

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
int outWidth = options.outWidth;
int outHeight = options.outHeight;

Bitmap background2 = BitmapFactory.decodeResource(getResources(), R.drawable.icon, null);
Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.sophie, null);

// 创建一个新的和SRC长度宽度一样的位图
Bitmap newbmp = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newbmp);
//draw bg into
cv.drawBitmap(background2, 0, 0, null);
// 在 0,0坐标开始画入bg
// draw fg into
cv.drawBitmap(foreground, 30, 60, null);

imageViewBlur2.setImageBitmap(newbmp);

--------画多层



BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
int outWidth = options.outWidth;
int outHeight = options.outHeight;

Bitmap background2 = BitmapFactory.decodeResource(getResources(), R.drawable.icon, null);
Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.sophie, null);

// 创建一个新的和SRC长度宽度一样的位图
Bitmap newbmp = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newbmp);
//draw bg into
cv.drawBitmap(background2, 0, 0, null);
// 在 0,0坐标开始画入bg
// draw fg into
cv.drawBitmap(foreground, 10, 30, null);
cv.drawBitmap(foreground, 20, 50, null);
cv.drawBitmap(foreground, 30, 70, null);
cv.drawBitmap(foreground, 40, 90, null);

imageViewBlur2.setImageBitmap(newbmp);

----- 由此可见,canvas实现的原理是图层的叠加,并非将上一图层给替换掉,layer叠加

----- 如果想在图片上加一层灰色遮罩,canvas实现 图层的叠加,灰色遮罩是bitmap

实现思路,在图片上再画一层0x33000000颜色的bitmap



BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
int outWidth = options.outWidth;
int outHeight = options.outHeight;

Bitmap foreground = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
foreground.eraseColor(0x4d000000);//填充颜色

Bitmap background2 = BitmapFactory.decodeResource(getResources(), R.drawable.icon, null);
// Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.sophie, null);

// 创建一个新的和SRC长度宽度一样的位图
Bitmap newbmp = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newbmp);
//draw bg into
cv.drawBitmap(background2, 0, 0, null);

//画前景
cv.drawBitmap(foreground, 0, 0, null);
imageViewBlur2.setImageBitmap(newbmp);

① Bitmap不借助canvas创建颜色填充bitmap
eraseColor

void eraseColor (int c)

Fills the bitmap's pixels with the specified Color.
Bitmap foreground = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
foreground.eraseColor(0x4d000000);//填充颜色
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息