imageview上的Alpha渐变不在图像上

问题描述 投票:0回答:1

是否可以在imageview上放置alpha?不是在图像上,而是直接在视图上?

目前我需要对图像进行photoshop,我不想编辑每个图像。

它应该如下所示:

enter image description here

android android-imageview
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.