Draw bitmaps from resources over another
12,556
Solution 1
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.
Solution 2
If you use an ImageView you can set the first bitmap as a background and the second as an image source.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:src="@drawable/foreground"/>
Author by
Kris
Updated on June 04, 2022Comments
-
Kris over 1 year
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
-
Kris over 11 yearsHow to set in Android these Alpha aspects of foreground bitmap?
-
Cat over 11 yearsWhen you create the Bitmap, give it an RGBA_8888 parameter, then draw to it as normal (do not set a background color). See Bitmap.Config. This example may also give you some ideas.
-
IgorGanapolsky over 7 yearsWhat if not using an ImageView?
-
IgorGanapolsky over 7 yearsWhy are you supplying
null
instead of Paint instance? -
Cat over 7 years@IgorGanapolsky You don't need to specify a paint when drawing a bitmap. It's an optional argument.
-
jakk over 7 years- On FrameLayout you can specify a foreground via setForeground(). - You can also use LayerDrawable to put multiple drawables on top of each other. - Else just go with Eric's approach.