2012-02-25 206 views
1

我遇到了Android圖形問題。我正在做遊戲開發,需要顯示其中一些有顏色漸變的圖像。我的問題是,當我加載帶有漸變的位圖圖像(以png格式)時,圖像顯示帶狀僞影。這是在Android 4.我研究了有關這個問題無數的帖子,並嘗試多種解決方案,包括:帶漸變神器的Android漸變

  1. 抖動對圖像輸入

    BitmapFactory.Options factoryOptions = 
        new BitmapFactory.Options(); 
    factoryOptions.inDither = true; 
    ... 
    background = BitmapFactory.decodeResource(resources, R.drawable.game_page_background, factoryOptions); 
    
  2. 從「RES /原始加載圖像「而不是 」RES /可繪「

  3. 驗證我顯示作爲的像素格式:位圖配置ARGB_8888

  4. 使用輸入流從資產目錄加載圖像。

我認爲解決方案2和4應該阻止Android圖像「優化」(我再次假設)產生的神器。但是沒有一個解決方案可行。無論我如何加載位圖,工件都會保留。最後,我不得不做一個可怕的解決方法,就是用photoshop將圖像烘烤成圖像。顯然,這是一個可怕的解決方法。

這個社區的任何人可以提供任何進一步的建議,如何獲得梯度的位圖圖像在Android中順利呈現沒有帶狀僞影?

下面的代碼斷肢展示如何已經生成這些測試圖片...

CODE FRAG **

... 
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 **

回答

0

我認爲你可以從decodeStream創建你的副本,並指定位圖COFFIFER 這裏是你的修改後的代碼斷枝:

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); 
... 

如果這篇文章可以幫助你,請讓這個帖子作爲一個答案。

謝謝。