I have two bitmaps, a background and a foreground. How to draw a bitmap on a background without using another canvas?
Decision:
1) First create raster images from resources with the additional option ARGB_8888
BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888;
2) Raster image declaration
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background, options); Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.foreground, options);
3) Drawing function inside 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); }
And as Soxxeh said, this is a very good source of information: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
source share