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

本文介绍了如何利用Bitmap和Canvas在Android中实现图层的叠加,通过在原图上绘制一层特定颜色的Bitmap来创建灰色遮罩效果。详细步骤包括加载图片、使用Canvas进行绘图,并提供了具体的代码实现。

---- 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);//填充颜色



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值