【发布时间】:2019-04-18 22:25:45
【问题描述】:
【问题讨论】:
-
是的,但我需要一个带有渐变 Alpha 的视图
【问题讨论】:
您可以使用以下方法伪造您想要达到的效果:
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);
【讨论】: