android 半透明位图背景为黑色

发布于 2024-12-04 05:31:37 字数 874 浏览 0 评论 0原文

我正在尝试创建半透明位图,我使用了以下代码:

private Bitmap SemiTransparent(Bitmap bitmap, int opacity) {


    // Create an array to hold the data of bitmap for which semi transparent bitmap is to be obtained
    int[] data = new int[(int)(bitmap.getWidth()) + 1];

    for (int y = 0; y < bitmap.getHeight(); ++y)
    {
           // Get a single line of data
          bitmap.getPixels(data, 0, bitmap.getWidth(), 0, y,bitmap.getWidth(), 1);     

     // Reset the alpha values 
           for (int x = bitmap.getWidth(); x>0; --x) 
           {

                data[x] = (data[x] & 0x00ffffff) | (opacity << 24);
           }     
          //fill alphaBitmap data
           bitmap.setPixels(data, 0, bitmap.getWidth(), 0, y, bitmap.getWidth(), 1);

    }


         return bitmap;
}

唯一的问题是背景(透明)颜色始终为黑色 - 如何修改它以使背景颜色完全透明而其余部分仍然半透明?

I'm trying to create semitransparent bitmaps, I used this code:

private Bitmap SemiTransparent(Bitmap bitmap, int opacity) {


    // Create an array to hold the data of bitmap for which semi transparent bitmap is to be obtained
    int[] data = new int[(int)(bitmap.getWidth()) + 1];

    for (int y = 0; y < bitmap.getHeight(); ++y)
    {
           // Get a single line of data
          bitmap.getPixels(data, 0, bitmap.getWidth(), 0, y,bitmap.getWidth(), 1);     

     // Reset the alpha values 
           for (int x = bitmap.getWidth(); x>0; --x) 
           {

                data[x] = (data[x] & 0x00ffffff) | (opacity << 24);
           }     
          //fill alphaBitmap data
           bitmap.setPixels(data, 0, bitmap.getWidth(), 0, y, bitmap.getWidth(), 1);

    }


         return bitmap;
}

The only problem is the background (transparent) color is always black - how can I modify this to make the background Color completely transparent and the rest still semitransparent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-12-11 05:31:37

使用此方法使位图透明:

/**
 * @param bitmap The source bitmap.
 * @param opacity a value between 0 (completely transparent) and 255 (completely
 * opaque).
 * @return The opacity-adjusted bitmap.  If the source bitmap is mutable it will be
 * adjusted and returned, otherwise a new bitmap is created.
 */
private Bitmap adjustOpacity(Bitmap bitmap, int opacity)
{
    Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

请参阅此处的说明:
http://blog.uncommons。 org/2011/01/12/ adjustment-the-opacity-of-an-android-bitmap/

在博客文章中,它检查位图是否可变 - 这可能会导致问题如果位图是可变的但没有配置 ARGB_8888 - 那么请使用我的方法。

Use this method to make a bitmap transparent:

/**
 * @param bitmap The source bitmap.
 * @param opacity a value between 0 (completely transparent) and 255 (completely
 * opaque).
 * @return The opacity-adjusted bitmap.  If the source bitmap is mutable it will be
 * adjusted and returned, otherwise a new bitmap is created.
 */
private Bitmap adjustOpacity(Bitmap bitmap, int opacity)
{
    Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

See an explanation here:
http://blog.uncommons.org/2011/01/12/adjusting-the-opacity-of-an-android-bitmap/

In the blog-post it checks if the bitmap is mutable or not - which can cause problems if the bitmap is mutable but doesn't have the config ARGB_8888 - so use my method instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文