西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開(kāi)發(fā)其它知識(shí) → 可循環(huán)顯示圖像的Android Gallery組件

可循環(huán)顯示圖像的Android Gallery組件

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2010/8/24 0:14:12字體大。A-A+

作者:佚名點(diǎn)擊:2798次評(píng)論:0次標(biāo)簽: Android 循環(huán)

  • 類(lèi)型:源碼相關(guān)大。23.6M語(yǔ)言:中文 評(píng)分:9.1
  • 標(biāo)簽:
立即下載
2 頁(yè) 這些圖像的資源ID都保存在int數(shù)組中
下面將這些圖像的資源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所示的效果了。

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門(mén)評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)