imageview上的Alpha渐变不在图像上

时间:2019-04-17 17:13:01

标签: android android-imageview

是否可以在图像视图上放置Alpha? 不是在图像上,而是在视图上?

此刻,我需要对图像进行photoshop和 我不想编辑每个图像。

它应该像这样:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法来伪造您想要达到的效果:

android:foreground="@drawable/image_overlay"

imageView.setForeground(imageOverlayDrawable);

这实际上不会使图像透明,但是如果您具有静态的纯色背景色,则足以使图像与背景融合在一起。

如果不是这样,请尝试以下操作:

// 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);