Android 半透明白色位图显示为不透明白色

Android 半透明白色位图显示为不透明白色,android,kotlin,colors,bitmap,Android,Kotlin,Colors,Bitmap,我正在尝试创建具有半透明背景(非黑色)的位图。我使用下一个代码: val result = drawable.bitmap.copy(Bitmap.Config.ARGB_8888, true) for (y in 0 until result.height) for (x in 0 until result.width) { val dstColor = Color.argb(100, 255, 255, 255) result.setPixel

我正在尝试创建具有半透明背景(非黑色)的位图。我使用下一个代码:

    val result = drawable.bitmap.copy(Bitmap.Config.ARGB_8888, true)
    for (y in 0 until result.height) for (x in 0 until result.width) {
        val dstColor = Color.argb(100, 255, 255, 255)
        result.setPixel(x, y, dstColor)
    }

但我看到的只是白色不透明的白色。我尝试将alpha参数设置为0,使用不同的颜色(读取,绿色),但不起作用。可能的原因是什么?

我测试了下一种只使用图像视图(位图容器id)创建布局的方法,它可以工作,但我需要更改背景视图组颜色才能看到它:

val bitmap = Bitmap.createBitmap(
    100,
    100,
    Bitmap.Config.ARGB_8888
)
val color = Color.argb(100, 255, 255, 255)
Canvas(bitmap).apply { drawColor(color) }
bitmap_container.setImageBitmap(bitmap)

获取位图副本后,执行以下行以创建alpha通道:

result.setHasAlpha(true)
其他一切都应该按原样工作

例如,使用您的代码并进行如下更改:

// Get the bitmap. What the bitmap is doesn't really matter. Here it is just a jpg.
val drawable = ResourcesCompat.getDrawable(resources, R.drawable.somebitmap, null) as BitmapDrawable
val result = drawable.bitmap.copy(Bitmap.Config.ARGB_8888, true)
result.setHasAlpha(true)
val dstColor = Color.argb(100, 255, 255, 255)
for (y in 0 until result.height) for (x in 0 until result.width) {
    result.setPixel(x, y, dstColor)
}

image.setImageBitmap(result)
如果
result.setHasAlpha(true)
被注释掉,那么我们将看到下图。此处图像上没有半透明

如果取消注释
result.setHasAlpha(true)
,则可以看到半透明: