圖片在Android系統(tǒng)中隨處可見(jiàn)。例如,列表圖標(biāo)、快捷方式圖標(biāo)、圖片按鈕等。在Android中我們通過(guò)使用Drawable類來(lái)完成對(duì)圖片的操作。Drawable類有很多個(gè)子類,如用來(lái)操作位圖的BitmapDrawable類、用來(lái)操作顏色的ColorDrawable類,以及用來(lái)操作各種形狀的ShapeDrawable類。
可以通過(guò)以下3種方法來(lái)實(shí)例化Drawable對(duì)象:一是使用保存在工程中的一個(gè)圖片文件;二是使用XML文件定義Drawable屬性;三是構(gòu)造方法實(shí)例化,這種方法在實(shí)際開(kāi)發(fā)中一般用不到。
1 使用圖片文件創(chuàng)建Drawable對(duì)象
簡(jiǎn)單的一種方式是在工程的資源文件下面保存圖片,該圖片會(huì)被Eclipse自動(dòng)在R類中創(chuàng)建引用,然后可以通過(guò)R.drawable.my_image使用該圖片對(duì)象。下面看一個(gè)實(shí)例,實(shí)例步驟說(shuō)明如下。
□ 將一個(gè)test.jpg圖片文件放置在工程的資源文件夾下。
□ 創(chuàng)建布局文件main.xml并在其中添加一個(gè)ImageView組件。
□ 創(chuàng)建Activity,并實(shí)例化ImageView組件對(duì)象。
□ 調(diào)用ImageView的setImageResource()方法,引用資源id。
Activity代碼如代碼清單1所示。
代碼清單1 MainActivity.java
package cn.com.farsight.drawable;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
//聲明圖片視圖ImageView
private ImageView myImageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通過(guò)findViewById方法獲得ImageView
myImageView = (ImageView) findViewById(R.id.ImageView01);
//為ImageView設(shè)置圖片資源
myImageView.setImageResource(R.drawable.test);
}
}
布局文件main.xml代碼如代碼清單2所示。
代碼清單2 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Drawable Test"/>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageView>
</LinearLayout>
程序運(yùn)行結(jié)果如圖1所示。

圖1 從資源文件加載圖片
2 使用XML文件定義Drawable屬性
在布局文件中定義Drawable屬性。例如,可以在布局文件中定義圖片按鈕的圖片及應(yīng)用程序的圖標(biāo)等。
代碼清單3所示的代碼演示了如何在AndroidManifest.xml配置文件中引用資源圖標(biāo)。
代碼清單3 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.farsight.drawable"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".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>
<uses-sdk android:minSdkVersion="3"/>
</manifest>
在上一個(gè)例子中也可以通過(guò)這種方式來(lái)配置ImageView的圖片資源。代碼如代碼清單4所示。
代碼清單4 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Drawable Test"/>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/test"
></ImageView>
</LinearLayout>
3 Bitmap和BitmapFactory
除了以上提到的兩種圖片保存以外,還可以將圖片文件保存在SDCard中,那么此時(shí)如何訪問(wèn)保存在SDCard中圖片文件呢?在這種情況下,可以通過(guò)Bitmap和BitmapFactory兩個(gè)類來(lái)讀取文件。下面的代碼演示了如何從SDCard中讀取圖片文件并將其設(shè)置為壁紙。
程序步驟說(shuō)明如下:
□ 在SDCard中添加一個(gè)名稱為wallpaper.jgp的圖片文件。
□ 在創(chuàng)建Activity。
□ 在在Activity的onCreate()方法中通過(guò)BitmapFactory的decodeFile()方法傳遞文件路徑,獲取Bitmap對(duì)象。
□ 在調(diào)用setWallpaper()方法設(shè)置桌面。
程序代碼如代碼清單5所示。
代碼清單5 MainActivity.java
package cn.com.farsight.drawable;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//圖片路徑
String path="/sdcard/wallpaper.jpg";
//通過(guò)BitmapFactory獲取Bitmap實(shí)例
Bitmap bm = BitmapFactory.decodeFile(path);
try{
//設(shè)置桌面
setWallpaper(bm);
}catch(IOException e){
e.printStackTrace();
}
}
}
程序運(yùn)行后桌面被改變,如圖2所示。

圖2 Bitmap和BitmapFactory的使用