android assets中.9.png的使用

來源:互聯網
上載者:User

Button button1 = (Button) findViewById(R.id.bt);

InputStream stream = null;

 try {

           stream = getAssets().open("button_compiled.9.png");

       } catch (IOException e1) 

      {

          e1.printStackTrace();

     }

Bitmap bitmap = BitmapFactory.decodeStream(stream);

byte[] chunk = bitmap.getNinePatchChunk();

boolean bResult = NinePatch.isNinePatchChunk(chunk);

NinePatchDrawable patchy = new NinePatchDrawable(bitmap, chunk,new Rect(), null);

 button1.setBackgroundDrawable(patchy);



assets中使用的.9.png圖片必須要編譯過,否則無法正常使用,如:中的第二個按鈕,已經失真。


編譯.9.png


.9.png可以使用draw9patch.bat工具來製作,這樣的png用圖片瀏覽工具開啟可以看到圖片的周圍有黑色的像素點,這是就是使用draw9patch.bat工具畫上去的。當經過android編譯後,從apk包中解壓出來.9.png,這時使用圖片瀏覽工具開啟可以看到圖片的周圍有黑色的像素點消失了。這就是編譯過的.9.png。


編譯.9.png: 調用系統的aapt.ext工具對包含png的程式進行處理,zip包,解壓zip得到的.9.png即編譯過的.9.png
建一個java工程並將android.jar和AndroidManifest.xml放到工程目錄下,建一個圖片目錄res/drawable
工程如:

使用aapt命令:
aapt p -M E:\workspace\CompileNinePatch\AndroidManifest.xml -S E:\workspace\CompileNinePatch\res -I E:\workspace\CompileNinePatch\android.jar -F C:\Users\liuyt\Desktop\temp.zip
將編譯後的zip包產生到案頭
 

解壓後可以看到編譯好的.9.png


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.