【发布时间】:2018-05-23 16:28:10
【问题描述】:
我的目标是使图像上的特定颜色透明,就像在加载时忽略一种纯背景颜色的游戏一样。
我读到的
- Draw a Bitmap on a Canvas replacing Black Pixels to Transparent
- Replacing a color in a Bitmap
- How to change colors of a Drawable in Android?
- Android: Make specific (green) color in background image transparent
- 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 掩码的情况下做到这一点。
【问题讨论】:
-
spriters-resource.com/resources/sheets/20/21264.png 这是我的意思的例子。橙色被忽略
标签: android image android-layout