Draw bitmaps from resources over another

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

+6
source share
2 answers

Try the following:

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

The second image (foreground image) must have alpha aspects or you do not see it.

+5
source

If you use ImageView, you can set the first bitmap as a background, and the second as the source of the image.

 <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background" android:src="@drawable/foreground"/> 
+1
source

Source: https://habr.com/ru/post/917696/


All Articles