【问题标题】:opacity map on bitmap android位图android上的不透明度贴图
【发布时间】:2013-07-18 21:19:15
【问题描述】:

我有两个位图,一个是墙纸图像,另一个是包含 alpha 信息的黑白图像。我想将该 alpha 信息应用于壁纸图像。

现在我知道我可以像这样改变不透明度:

img.setOpacity(50);

但这设置了整个位图的 alpha,这不是我想要的。我想要一种基于黑白源设置图像 alpha 的高级方法。

【问题讨论】:

    标签: android opacity


    【解决方案1】:

    你想要Bitmap.setPixel。例如,假设您的位图大小相同:

    Bitmap a,b;//initialized - a is the b&w image, b is the normal image
    
    for (int x = 0; x < a.width(); x++)
    {
        for (int y = 0; y < a.height(); y++)
        {
            int color = b.getPixel(x, y);
            int red = (color >> 16) & 0xFF;
            int green = (color >> 8) & 0xFF;
            int blue = (color >> 0) & 0xFF;
            if (a.getPixel(x, y) == Color.WHITE)
                b.setPixel(x, y, Color.argb(0.5, red, green, blue));//alpha = 0.5
            else
                b.setPixel(x, y, Color.argb(1, red, green, blue));//alpha = 1
        }
    }
    

    【讨论】:

    • 试过了,得到一个错误:java.lang.IllegalStateException 07-19 20:41:30.285: E/AndroidRuntime(8210): at android.graphics.Bitmap.setPixel(Bitmap.java:1074) .....为了记录,这就是我应该画的,对吧? c.drawBitmap(b, 0, 0, p);
    • 你考虑过只使用 ImageView 吗?
    • 我在动态壁纸中使用它
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 2017-04-02
    • 2016-07-25
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多