Android 圖片資源

2018-02-18 13:59 更新

Android生成放置在/res/drawable子目錄中的圖像文件的資源ID。

支持的圖像類型包括.gif,.jpg和.png。此目錄中的每個(gè)圖像文件都從其基本文件名生成唯一的ID。

如果圖像文件名為 sample_image.jpg ,那么生成的資源ID是 R.drawable.sample_image 。

對于具有相同基本文件名的兩個(gè)文件名,將出現(xiàn)錯(cuò)誤。將忽略 /res/drawable 下的子目錄。

例子

以下代碼顯示如何在XML布局定義中使用圖像資源。

<Button
      android:id="@+id/button1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Dial"
      android:background="@drawable/sample_image"
/>

下面的代碼顯示了如何檢索Java中的圖像,并將其設(shè)置為類似于按鈕的UI對象。

BitmapDrawable d = activity.getResources().getDrawable(R.drawable.sample_image);
button.setBackgroundDrawable(d);

//or you can set the background directly from the Resource Id
button.setBackgroundResource(R.drawable.sample_image);

Android還支持一種特殊類型的圖像,稱為可拉伸圖像。



以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號