【问题标题】:Android gradients with banding artifact带有条带伪影的 Android 渐变
【发布时间】:2012-02-25 15:25:05
【问题描述】:

我遇到了 Android 图形问题。我正在做游戏开发,需要显示其中一些具有颜色渐变的图像。我的问题是,当我加载带有渐变的位图图像(以 png 格式)时,图像会显示带状伪影。这是在 Android 4 上。我研究了许多与此问题相关的帖子,并尝试了许多解决方案,包括:

  1. 抖动输入图像

    BitmapFactory.Options factoryOptions = 
      new BitmapFactory.Options();
    factoryOptions.inDither = true;
    ...
    background = BitmapFactory.decodeResource( resources, R.drawable.game_page_background, factoryOptions );
    
  2. 从“res/raw”而不是“res/drawable”加载图像

  3. 验证我的显示器的像素格式为:Bitmap Config ARGB_8888

  4. 使用输入流从资产目录加载图像。

我认为解决方案 2 和 4 应该阻止 Android 图像“优化”(我再次假设)正在产生工件。但是没有一个解决方案有效。无论我如何加载位图,工件仍然存在。最后,我不得不做一个可怕的解决方法,即使用 Photoshop 将噪点烘焙到图像中。显然,这是一个可怕的解决方法。

这个社区的任何人都可以提供任何进一步的建议,以了解如何在没有条带伪影的情况下在 Android 中获得具有渐变的位图图像以平滑渲染?

以下代码片段显示了我是如何生成这些测试图像的...

代码片段**

...
InputStream is = null;
try
{
    is = ((Activity)gameMngr).getAssets().open("test_background_3.png");
}
catch( IOException ioe)
{
    Log.d(TAG, "TEST CODE: Unable to open resources. ");
}
this.background = BitmapFactory.decodeStream(is);
...

// ELSEWHERE
...
canvas.drawBitmap( this.background, 0, 0, null );
...

END FRAG **

【问题讨论】:

    标签: android image bitmap gradient artifact


    【解决方案1】:

    我认为您可以从 decodeStream 创建一个副本并指定位图配置 这是您修改后的代码片段:

    InputStream is = null;
    try
    {
        is = ((Activity)gameMngr).getAssets().open("test_background_3.png");
    }
    catch( IOException ioe)
    {
        Log.d(TAG, "TEST CODE: Unable to open resources. ");
    }
    this.background = BitmapFactory.decodeStream(is);
    
    //create copy and specify the config of the bitmap, setting to true make the bitmap
    //mutable.
    Bitmap newBtmp = this.background.copy(Bitmap.Config.ARGB_8888, true);
    
    //use the newBtmp object
    canvas.drawBitmap( newBtmp, 0, 0, null );
    ...
    

    如果这篇文章对你有帮助,请把这篇文章作为答案。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多