enfix
enfix

Reputation: 7000

Image over other image Android

How can i set image (semi-trasparent) over other image ?
I need to create new bitmap and then save it.

Thanks all.

Upvotes: 4

Views: 3783

Answers (3)

Ofir A.
Ofir A.

Reputation: 3162

All you need to do is to take the two bitmaps and set their
bounds. Then you need to draw them both on the canvas. If you want to set the image as semi-trasparent, you need to set the alpha of the picture.

This is an example:

    Bitmap bitmap = null;
    try {

        bitmap = Bitmap.createBitmap(500, 500, Config.ARGB_8888);
        Canvas c = new Canvas(bitmap);
        Resources res = getResources();


        Bitmap bitmap1 = BitmapFactory.decodeResource(res, R.drawable.test1); //blue

        Bitmap bitmap2 = BitmapFactory.decodeResource(res, R.drawable.test2); //green
        Drawable drawable1 = new BitmapDrawable(bitmap1);
        Drawable drawable2 = new BitmapDrawable(bitmap2);


        drawable1.setBounds(100, 100, 400, 400);
        drawable2.setBounds(150, 150, 350, 350);
        drawable1.draw(c);
        drawable2.draw(c);


    } catch (Exception e) {
    }
    return bitmap;

}

Upvotes: 2

Luke Taylor
Luke Taylor

Reputation: 9599

Create a canvas object from the bottom layer canvas. Then draw the semi-transparent Bitmap to that canvas. The original Bitmap object will now have the semi-transparent bitmap written on top of it.

Upvotes: 0

Ali Alnoaimi
Ali Alnoaimi

Reputation: 2317

    Bitmap bitmap1 = null; // define it
    Bitmap bitmap2 = null; // define it

    Bitmap resultBitmap = Bitmap.createBitmap(bitmap1.getWidth(), bitmap1.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(resultBitmap);

    c.drawBitmap(bitmap1, 0, 0, null);

    Paint p = new Paint();
    p.setAlpha(127);

    c.drawBitmap(bitmap2, 0, 0, p);

    // Your final bitmap is resultBitmap

Upvotes: 8

Related Questions