【问题标题】:Alpha gradient on imageview not on imageimageview 上的 Alpha 渐变不在图像上
【发布时间】:2019-04-18 22:25:45
【问题描述】:

是否可以在图像视图上添加 alpha? 不在图像上,而是直接在视图上?

目前我需要对图像进行 Photoshop 和 我不想编辑每张图片。

应该是这样的:

【问题讨论】:

标签: android android-imageview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2015-04-14
    • 1970-01-01
    相关资源
    最近更新 更多