0

I need to overlay two images in live wallpaper. The overlay images is the jpg which needs to be set to "additive" overlay. it adds the pixel value rather than calculating the transparency. how can i achieve this in android ?

Ravi Bhojani
  • 1,032
  • 1
  • 13
  • 24

2 Answers2

2

You can make use of Android's Bitmap and Drawable classes mixed with Canvas, and try something like in this snippet:

public static Drawable mergeImage(Drawable orig, Drawable over, int left, int top) {
    Bitmap original = ((BitmapDrawable)orig).getBitmap();
    Bitmap overlay = ((BitmapDrawable)over).getBitmap();
    Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    canvas.drawBitmap(original, 0, 0, paint);
    canvas.drawBitmap(overlay, left, top, paint);

    return new BitmapDrawable(result);
}

I've coded a photo image gridview overlayered with "online status" using the above lines. Hope that it works for you too.

Marcelo
  • 2,075
  • 5
  • 21
  • 38
0

A more general approach may be to create a PorterDuffXfermode with your wanted PorterDuffMode and then set it on the Paint object that you use with your canvas, as referenced in mthama's answer but substituting some lines. This allows you to use other Porter-Duff modes as wanted/needed.

Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(original, 0, 0, paint);
paint.setXferMode(new PorterDuffXferMode(PorterDuff.Mode.OVERLAY));
canvas.drawBitmap(overlay, left, top, paint);

Mind you, I haven't tried this, so go with mthama's answer. :)

Community
  • 1
  • 1
Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28