【问题标题】:How to make a specific colour in an image transparent in Android studio?如何在Android Studio中使图像中的特定颜色透明?
【发布时间】:2021-06-17 18:05:10
【问题描述】:

我正在使用带有字母“LYIT”和白色背景的图像。我想让白色背景透明,所以只有字母可见。

我试过android:background="@android:color/transparent",但它只改变了图像Logo的一小部分。

app:tint="@color/colorWhite" 也不起作用。

有没有办法可以定位图像的白色部分并使其透明?

另外,我正在使用 Kotlin 编写课程。

谢谢

【问题讨论】:

  • 也许更好的主意是创建没有背景的 SVG 版本?)
  • 在 ImageView 上使用 android:background="@android:color/transparent" 只会替换包含您的图片的 View 的背景。如果您的图片不透明,则不会自动裁剪字母。最好的方法是为您的 ImageView 提供一个透明的 SVG(或 PNG)资源。
  • @KilianP。但不同设备上的 PNG 损失质量)所以最适合这种情况的是 SVG
  • @Syorito Hatsuki 当然,SVG 是必须使用的!但在他的例子中,我们不知道“标志”是否在 SVG 中可用(也许他只有一个 PNG 可用),这是一个有用的信息,表明如果需要,他可以在最坏的情况下使用 PNG。
  • @KilianP。那是真的)如果我的客户给我发 PNG,我要求在项目中使用它的双倍价格:D 然后所有开始创建正确的文件)

标签: android xml android-studio kotlin imageview


【解决方案1】:

我在另一个问题上找到了这个答案, 你可以在后端编写如下代码

val image = findViewById<ImageView>(R.id.*write the id of the image here*)
image.background.alpha = 0 //the value of alpha's range is [0,255], 0is transperent and 255 is quite opaque

【讨论】:

    猜你喜欢
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    相关资源
    最近更新 更多