日日行,不怕千萬裡;常常做,不怕千萬事。 ——《格言聯璧·處事》
本講内容:Drawable及其相關類的使用
一、Drawable作用:
一個讓人賞心悅目的界面對軟體來說非常重要,是以圖形圖像資源也顯得非常重要。下面是它的繼承關系,可以看到BitmapDrawable,AnimationDrawable等對象都是它的子類。

二、Drawable的使用:
最簡單的使用Drawable資源的方法是,把圖檔放入Android工程的res\drawable目錄下,程式設計環境會自動在R類裡為此資源建立一個引用。你可以使用此引用通路該資源對象。譬如對應用程式的圖示,在Java代碼中可以用R.drawable.icon引用到它,在XML中可以用@drawable/icon引用到它。
那麼如果圖檔資源不在項目中而是在SDCard中時如何使用呢,我們看一下下面的例子學習一下Drawable的使用,并且順便學習一下Bitmap和BitmapFactory的使用。
1、首先拷貝g1.jpg和g2.jpg兩個圖檔到sdcard中
下面是res/layout/activity_main.xml 布局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.text1.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="檢視圖檔A1"/>
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="檢視圖檔A2"/>
<Button
android:id="@+id/b3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="設定圖檔A1為桌面"/>
<Button
android:id="@+id/b4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="設定圖檔A2為桌面"/>
<Button
android:id="@+id/b5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="恢複預設桌面"/>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在AndroidManifest.xml檔案中注冊
<!-- 設定桌面桌面 -->
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.text1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
下面是MainActivity.java主界面檔案:
public class MainActivity extends Activity implements OnClickListener {
private Button b1;
private Button b2;
private Button b3;
private Button b4;
private Button b5;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
image = (ImageView) findViewById(R.id.image);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
// 給ImageView設定圖檔,從存儲卡中擷取圖檔為Drawable,然後把Drawable設定為ImageView的背景
image.setBackgroundDrawable(Drawable.createFromPath("/sdcard/g1.jpg"));
break;
case R.id.b2:
image.setBackgroundDrawable(Drawable.createFromPath("/sdcard/g2.jpg"));
// image.setImageResource(R.drawable.a8);
break;
case R.id.b3:
try {
// Activity的父類ContextWrapper有這個setWallpaper方法,當然使用此方法需要有android.permission.SET_WALLPAPER權限
setWallpaper(BitmapFactory.decodeFile("/sdcard/g1.jpg"));
} catch (Exception e) {
}
break;
case R.id.b4:
try {
setWallpaper(BitmapFactory.decodeFile("/sdcard/g2.jpg"));
} catch (Exception e) {
}
break;
case R.id.b5:
try {
//Activity的父類ContextWrapper有這個clearWallpaper方法,作用是恢複預設桌面,當然使用此方法需要有android.permission.SET_WALLPAPER權限
clearWallpaper();
} catch (Exception e) {
}
break;
}
}
}
下面是運作結果:
本講到這裡,謝謝大家!