2013-01-21 63 views
5

我正在嘗試以編程方式創建9個補丁圖像。我試過Griffey's的方法,但似乎無法使其工作。關於如何使用課程的信息很少。我相信我錯誤地導入了位圖。如果有人知道如何讓這個工作或知道更好的方法,我會非常感謝您的幫助。用代碼創建9個補丁圖像

我的代碼:

// Dynamically create the 9patch image and apply it to a LinearLayout 
LinearLayout llcode = (LinearLayout) findViewById(R.id.llcode); 
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.white_button); 
Drawable np = NinePatchBitmapFactory.createNinePathWithCapInsets(getResources(), bitmap, 15, 15, 16, 16, null); 

llcode.setBackgroundDrawable(np); 

// The following code using the actual 9patch image: white_button_np.9.png 
LinearLayout llxml = (LinearLayout) findViewById(R.id.llxml); 
llxml.setBackgroundDrawable(getResources().getDrawable(R.drawable.white_button_np)); 

該截圖顯示在上面,用在底部的機器人工具的典型9patch方法的編程方法。

enter image description here

原始圖像,然後再將其轉換爲9patch。

enter image description here

+1

經過一些更多的測試後,我發現上述錯誤只發生在舊手機和所有模擬器中。我在運行4.1.2的Galax Nexus上進行了測試,Griffey的課程運行良好。我在運行2.3.4的Droid X以及所有仿真器(包括Galaxy Nexus-4.1.2)上進行了測試,LinearLayout具有各種奇怪的顏色,如上面的屏幕截圖。 – Brendan

+0

我也被困在這一個,你找到一種方法來解決它? – Nativ

+0

我已經用模擬器測試了這個按鈕。在API級別10中,它似乎被破壞了,但在API級別16中,它對我來說確實很好。 – Scorchio

回答

0

你試過迫使解碼器配置,以確保有一個alpha通道?

嘗試設置inPreferredConfigARGB_8888

+0

謝謝您的建議。不幸的是,這似乎沒有效果。 – Brendan