【问题标题】:How can I get a translucent and colorized image in Android like the new Google Play Newsstand header?如何在 Android 中获得像新的 Google Play 报亭标题一样的半透明和彩色图像?
【发布时间】:2015-01-28 19:35:03
【问题描述】:

我正在尝试获得像新的 Google Play 报亭应用一样的半透明和彩色图像,但到目前为止我尝试的所有方法都无法完美运行。下面的图片更能说明我的意思。

Google Play 报亭标题如下所示:

但我的代码在我看来是这样的标题:

太可怕了!

我的代码是:

ImageUtils.getImageFiltered(this, R.drawable.disaster, R.color.transparent_teal)

在“ImageUtils”类中,方法getImageFiltered(...)是:

public static Drawable getImageFiltered(Context context, int res, int color) {
    Drawable drawable = createContrast(context, res, 50);
    color = context.getResources().getColor(color);
    ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.DARKEN);
    drawable.setColorFilter(filter);
    return drawable;
}

createContrast(context, res, 50); 方法只是将图像改为黑白。

那么,有谁知道如何获得像 Google 应用一样的图像?

非常感谢,对不起我的英语,我刚刚学习。

【问题讨论】:

  • 你应该用你正在使用的语言进行标记 - 这样它更有可能被可以提供帮助的人看到。
  • @JamesWaddington - 我已重新标记帖子并嵌入图片以改进帖子。
  • 谢谢@JamesWaddington。我没有添加图像,因为我还不能。我是 Stack Overflow 的新手。

标签: java android image-processing transparency translucency


【解决方案1】:

我认为问题出在 createContrast() 函数中,它使较浅的灰色阴影过亮。您可以尝试使用应用了 setSaturation(0) 的 ColorMatrix 来修复它以返回不饱和灰度图像而不应用任何对比度调整。然后申请 PorterDuff。

或者,您可以使用 ColorMatrixColorFilter 创建效果,通过连接一个 ColorMatrix 来转换为灰度和另一个通过颜色进行调制来创建,如下所示:

public static Drawable getImageFiltered(Context context, int res, int color) {
    // load image:
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res);
    BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);

    // create matrix to convert to greyscale:
    ColorMatrix greyscaleMatrix = new ColorMatrix();
    greyscaleMatrix.setSaturation(0);

    // create matrix to colorize and apply transluceny:
    ColorMatrix colorizeMatrix = new ColorMatrix();
    color = context.getResources().getColor(color);
    colorizeMatrix.setScale(Color.red(color) / 255.0f,
            Color.green(color) / 255.0f,
            Color.blue(color) / 255.0f,
            Color.alpha(color) / 255.0f); // <- try setting this to 1.0f for no translucency

    // concatenate the two matrices and create a ColorMatrixColorFilter from the result:
    greyscaleMatrix.postConcat(colorizeMatrix);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(greyscaleMatrix);

    // apply the filter:
    drawable.setColorFilter(filter);
    return drawable;
}

还需要使图像半透明吗?除了黑色背景之外,似乎没有任何东西从它后面显露出来。

【讨论】:

  • 伙计,你是对的,你的解决方案是最好的。我应该说我的应用标题比谷歌应用标题漂亮(在我看来)哈哈哈。非常感谢你。你拯救了我的一天。
  • 嗨,@samgak。如果我想要渐变半透明背景怎么办?我试过GradientDrawable,但没有成功。你能帮我解决这个问题吗?
  • 嗨@AthusVieira,我认为最好为此提出一个新问题
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2021-03-22
  • 2019-02-15
相关资源
最近更新 更多