我有两个位图,背景和前景。如何在不使用另一个 Canvas 的情况下在背景上绘制位图前景?
解决方案:
1) 首先使用附加选项 ARGB_8888 从资源创建位图
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
2) 声明位图
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background, options);
Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.foreground, options);
3) onDraw()函数内部绘制图形
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.White);
Paint paint = new Paint();
canvas.drawBitmap(background, 0, 0, paint);
paint.setXfermode( new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
canvas.drawBitmap(foreground, 0, 0, paint);
}
正如 Soxxeh 所说,这是非常好的信息来源:http: //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html