【问题标题】:canvas.drawBitmap() only honors alpha layer when it is 0canvas.drawBitmap() 仅在为 0 时尊重 alpha 层
【发布时间】:2011-04-12 05:14:05
【问题描述】:

我正在使用BitmapFactory.decodeResource 加载png 资源,然后使用drawBitmap()Canvas 上绘制它们。

我一次绘制一个不同的层,以便透明对象应该遮挡它们应该遮挡的部分,但是当我的 png 中的 alpha 级别高于 0 时,它们似乎会被忽略。不渲染 alpha 为 0 的地方,这是正确的,但是在 alpha 小于 255 的地方,它不会将颜色与该像素处的现有颜色混合,它只是在没有任何 alpha 混合的情况下绘制它。

如何在Canvas 上绘制位图,并根据源图像的 Alpha 通道进行适当混合?相关代码sn-ps如下:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Config.ARGB_8888;
...
decorationTextures[1] = new 
    TextureStatic(BitmapFactory.decodeResource(resources, R.drawable.ice_1, opt));
decorationTextures[2] = new 
    TextureStatic(BitmapFactory.decodeResource(resources, R.drawable.ice_2, opt));
...
if(mTexture != null && mInPlay) {
    if(mZone != null)
        canvas.drawBitmap(mTexture.getBitmap(),
                          mScreenX + mZone.getXOffset(), 
                          mScreenY + mZone.getYOffset(), null);
    else
        canvas.drawBitmap(mTexture.getBitmap(), mScreenX, mScreenY, null);
}

【问题讨论】:

  • 你找到解决这个问题的方法了吗?

标签: android alpha


【解决方案1】:

您是否尝试过更改SurfaceView(如果您正在使用)和/或Window 的格式以匹配您正在使用的位图格式(ARGB_8888)?这可能会阻止您获得真正的透明效果,无论如何,您应该尝试match up your window/bitmap formats for better performance/quality

尝试Window.setFormat(PixelFormat.RGBA_8888);SurfaceHolder.setFormat(PixelFormat.RGBA_8888; 设置像素格式。

【讨论】:

    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多