下面將這些圖像的資源ID都保存在int數(shù)組中,代碼如下:
代碼
private int[] resIds = new int[]
{ R.drawable.item1, R.drawable.item2, R.drawable.item3,
R.drawable.item4, R.drawable.item5, R.drawable.item6,
R.drawable.item7, R.drawable.item8, R.drawable.item9,
R.drawable.item10, R.drawable.item11, R.drawable.item12,
R.drawable.item13, R.drawable.item14, R.drawable.item15 };
在本例的main.xml文件中配置了一個(gè)Gallery組件,代碼如下:
代碼
<?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">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="30dp" />
</LinearLayout>
現(xiàn)在在onCreate方法中裝載這個(gè)組件,代碼如下:
代碼
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 裝載Gallery組件
Gallery gallery = (Gallery) findViewById(R.id.gallery);
// 創(chuàng)建用于描述圖像數(shù)據(jù)的ImageAdapter對(duì)象
ImageAdapter imageAdapter = new ImageAdapter(this);
// 設(shè)置Gallery組件的Adapter對(duì)象
gallery.setAdapter(imageAdapter);
}
在上面的代碼中涉及到一個(gè)非常重要的類(lèi):ImageAdapter。該類(lèi)是android.widget.BaseAdapter的子類(lèi),用于描述圖像信息。下面先看一下這個(gè)類(lèi)的完整代碼。
代碼
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context context)
{
mContext = context;
// 獲得Gallery組件的屬性
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
// 返回圖像總數(shù)
public int getCount()
{
return resIds.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
// 返回具體位置的ImageView對(duì)象
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(mContext);
// 設(shè)置當(dāng)前圖像的圖像(position為當(dāng)前圖像列表的位置)
imageView.setImageResource(resIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
// 設(shè)置Gallery組件的背景風(fēng)格
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
在編寫(xiě)ImageAdapter類(lèi)時(shí)應(yīng)注意的兩點(diǎn):
1. 在ImageAdapter類(lèi)的構(gòu)造方法中獲得了Gallery組件的屬性信息。這些信息被定義在res\values\attrs.xml文件中,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
上面的屬性信息用于設(shè)置Gallery的背景風(fēng)格。
2. 在ImageAdapter類(lèi)中有兩個(gè)非常重要的方法:getCount和getView。其中g(shù)etCount方法用于返回圖像總數(shù),要注意的是,這個(gè)總數(shù)不能大于圖像的實(shí)際數(shù)(可以小于圖像的實(shí)際數(shù)),否則會(huì)拋出越界異常。當(dāng)Gallery組件要顯示某一個(gè)圖像時(shí),就會(huì)調(diào)用getView方法,并將當(dāng)前的圖像索引(position參數(shù))傳入該方法。一般getView方法用于返回每一個(gè)顯示圖像的組件(ImageView對(duì)象)。從這一點(diǎn)可以看出,Gallery組件是即時(shí)顯示圖像的,而不是一下將所有的圖像都顯示出來(lái)。在getView方法中除了創(chuàng)建了ImageView對(duì)象,還用從resIds數(shù)組中獲得了相應(yīng)的圖像資源ID來(lái)設(shè)置在ImageView中顯示的圖像。最后還設(shè)置了Gallery組件的背景顯示風(fēng)格。
OK,現(xiàn)在來(lái)運(yùn)行這個(gè)程序,來(lái)回拖動(dòng)圖像列表,就會(huì)看到如圖1和圖2所示的效果了。
本文導(dǎo)航
- 第1頁(yè): 首頁(yè)
- 第2頁(yè): 這些圖像的資源ID都保存在int數(shù)組中
- 第3頁(yè): 循環(huán)顯示圖像的原理
- 第4頁(yè): 實(shí)現(xiàn)循環(huán)顯示圖像的Gallery組件
- 第5頁(yè): 本例中Main類(lèi)的完整代碼