0

Is it possible to put an alpha on an imageview? Not on the image but directly on the view?

At the moment I need to photoshop the image and I don't want to edit every image.

It should look like this:

enter image description here

J.Doe
  • 177
  • 1
  • 2
  • 13

1 Answers1

0

You might be able to fake the effect you're trying to achieve using:

android:foreground="@drawable/image_overlay"

or

imageView.setForeground(imageOverlayDrawable);

This will not actually make the image transparent, but if you have a static solid background color it should be enough to create the illusion of the image blending with the background.

If that's not an option try something like this:

// Get the image from wherever it lives and create a Bitmap of it
...

// Draw the image to a canvas so that we can modify it
Canvas canvas = new Canvas(image);
Paint imagePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(imageBitmap, 0, 0, imagePaint);

// Create a paint to draw the overlay with
PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);
Paint overlayPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
overlayPaint.setXfermode(porterDuffXfermode);

// The overlay in this case needs to be a white gradient (where fully opaque means that
// the image will be left untouched and fully transparent will completely erase the image)
Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(), R.id.drawable.gradient_overlay, Bitmap.Config.ARGB_8888);

// Apply the overlay to create the alpha effect
canvas.drawBitmap(overlayBitmap, 0, 0, overlayPaint);

// Update the ImageView
imageView.setImageBitmap(bitmap);
TofferJ
  • 4,678
  • 1
  • 37
  • 49