使用Canvas类的drawBitmap()方法绘制位图,并且可以使用Paint类中setXfermode()方法来填充空白像素。
示例代码:
//创建一个位图数组
Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
//创建画布,并将其绑定到位图数组
Canvas canvas = new Canvas(bitmap);
//使用Paint类的setColor()方法来填充画布的背景
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
//在画布上绘制位图
Bitmap bitmapToDraw = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_to_draw);
canvas.drawBitmap(bitmapToDraw, 0, 0, null);
//使用Xfermode来填充空白像素
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);