Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw bitmaps from resources over another

I have got two bitmaps, background and foreground. How do I draw bitmap foreground on background without using another Canvas?

Solution:

1) First create bitmaps from resources with additional option ARGB_8888

BitmapFactory.Options options = new BitmapFactory.Options();  
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

2) Declare bitmaps

Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background, options);  
Bitmap foreground = BitmapFactory.decodeResource(getResources(), R.drawable.foreground, options);

3) Inside onDraw() function draw graphics

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 very good source of information: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html

like image 854
Kris Avatar asked Jun 09 '12 19:06

Kris


1 Answers

Try this:

canvas.drawBitmap(backgroundImageBitmap, 0.0f, 0.0f, null);
canvas.drawBitmap(foregroundImageBitmap, 0.0f, 0.0f, null);

The second image (foreground image) has to have Alpha aspects or you can't see through it.

like image 100
Cat Avatar answered Oct 20 '22 13:10

Cat