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

---- bitmap和canvas画出叠加的2张照片

bitmap和canvas实现图层叠加(可实现灰色遮罩)_第1张图片

--- 图片1原图

bitmap和canvas实现图层叠加(可实现灰色遮罩)_第2张图片

------ 图片2原图

bitmap和canvas实现图层叠加(可实现灰色遮罩)_第3张图片

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

        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);

--------画多层

bitmap和canvas实现图层叠加(可实现灰色遮罩)_第4张图片


 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

bitmap和canvas实现图层叠加(可实现灰色遮罩)_第5张图片


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



你可能感兴趣的:(bitmap)