【问题标题】:android studio | setting specific color to transparent on image安卓工作室 |在图像上将特定颜色设置为透明
【发布时间】:2018-05-23 16:28:10
【问题描述】:

我的目标是使图像上的特定颜色透明,就像在加载时忽略一种纯背景颜色的游戏一样。

我读到的

  1. Draw a Bitmap on a Canvas replacing Black Pixels to Transparent
  2. Replacing a color in a Bitmap
  3. How to change colors of a Drawable in Android?
  4. Android: Make specific (green) color in background image transparent
  5. Android how to apply mask on ImageView?

我已添加到默认 .java 的代码

ImageView log = (ImageView) findViewById(R.id.iconLogo);
int col = ResourcesCompat.getColor(getResources(), R.color.colorTrans, null);
log.getDrawable().mutate().setColorFilter(col, PorterDuff.Mode.SRC_IN);
//log.getDrawable().setColorFilter(col, PorterDuff.Mode.SRC_IN); //take2
//log.setColorFilter(R.color.colorTrans, PorterDuff.Mode.SRC_IN); //take3

使图像可变可提供更好的结果,但仍不会从图像中减去R.color.colorTrans。总体而言,在使用过滤器后,它似乎在整个图像上方作为过滤器工作,并且没有搜索我设置的颜色,因此该颜色始终存在并且不排除不取决于所使用的模式。

第二个选项

 public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap, int BgColor) {
        Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
        int nWidth = result.getWidth();
        int nHeight = result.getHeight();
        for (int y = 0; y < nHeight; ++y)
            for (int x = 0; x < nWidth; ++x) {
                int nPixelColor = result.getPixel(x, y);
                if (nPixelColor == BgColor)
                    result.setPixel(x, y, Color.TRANSPARENT);
            }
        return result;
    }

仅使用一个 600x600 像素的 png 文件将应用加载时间从 1-2 秒增加到 8-12 秒,而且它不会删除特定颜色。因此不能用于多个游戏。

我再次需要的是从 ImageView 中的 Image 中删除/忽略特定颜色,但不要使所有 pic 透明。由于游戏使用没有 alpha 的图块地图,因此必须有一种方法可以在不添加 alpha 掩码的情况下做到这一点。

【问题讨论】:

标签: android image android-layout


【解决方案1】:

解决方案在这里unable to transparent pixels of a bitmap image

但是,即使在一张图片上使用它也会大大增加加载时间。可能.png 已经清除了背景会更好用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2011-02-25
    • 2017-06-02
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多