Android学习笔记26:图片切换控件ImageSwitcher的使用
在Windows操作系統(tǒng)中,要查看多張圖片,可以通過使用“Windows照片查看器”在“上一張”和“下一張”之間切換,進(jìn)行多張圖片的瀏覽。
在Android中,可以通過使用圖片切換控件ImageSwitcher來實(shí)現(xiàn)瀏覽多張圖片的功能。下面我們就通過一個(gè)實(shí)際的例子來說明圖片切換控件ImageSwitcher的使用方法。
?
1.界面布局
在xml布局文件中,我們使用LinearLayout對整個(gè)界面進(jìn)行垂直布局。在界面的頂端設(shè)置了一個(gè)水平居中的ImageSwitcher控件,用來顯示多張圖片。在ImageSwitcher控件的下面使用LinearLayout水平布局設(shè)置四個(gè)ImageButton按鈕,在點(diǎn)擊這些按鈕時(shí)分別用于實(shí)現(xiàn)右旋圖片、顯示上一張圖片、顯示下一張圖片、左旋圖片效果。整個(gè)布局文件很簡單,具體源碼如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 7 <ImageSwitcher 8 android:id="@+id/imageSwitcher" 9 android:layout_marginTop="5dp" 10 android:layout_gravity="center" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" ></ImageSwitcher> 13 14 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 15 xmlns:tools="http://schemas.android.com/tools" 16 android:orientation="horizontal" 17 android:layout_marginTop="5dp" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent" > 20 21 <!-- 右旋箭頭 --> 22 <ImageButton 23 android:id="@+id/rightrotatearrow" 24 android:layout_marginLeft="30dp" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:src="@drawable/rightrotatearrow" ></ImageButton> 28 29 <!-- 前一個(gè)箭頭 --> 30 <ImageButton 31 android:id="@+id/forwardarrow" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:src="@drawable/forwardarrow" ></ImageButton> 35 36 <!-- 下一個(gè)箭頭 --> 37 <ImageButton 38 android:id="@+id/nextarrow" 39 android:layout_width="wrap_content" 40 android:layout_height="wrap_content" 41 android:src="@drawable/nextarrow" ></ImageButton> 42 43 <!-- 左旋箭頭 --> 44 <ImageButton 45 android:id="@+id/liftrotatearrow" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:src="@drawable/liftrotatearrow" ></ImageButton> 49 50 </LinearLayout> 51 52 </LinearLayout>程序運(yùn)行后的效果如圖1所示。
圖1?主界面
?
2.ViewFactory接口
要將圖片顯示在ImageSwitcher控件中,必須為ImageSwitcher類設(shè)置一個(gè)ViewFactory,用來將顯示的圖片和父窗口區(qū)分開來。這可以通過如下方法來實(shí)現(xiàn):
mImageSwitcher.setFactory();
此外,我們還需要實(shí)現(xiàn)ViewSwitcher.ViewFactory接口中的makeView()抽象方法,通過該方法可以返回一個(gè)ImageView對象,所有圖片都將通過該ImageView對象來進(jìn)行顯示。具體實(shí)現(xiàn)方法如下:
1 /* 2 * Function : makeView() 3 * Describe : 將所有圖片通過ImageView來顯示 4 * Author : 博客園-依舊淡然 5 */ 6 public View makeView() { 7 return new ImageView(this); 8 }?
3.存儲圖片資源
在《Android學(xué)習(xí)筆記25:畫廊控件Gallery的使用》(http://www.cnblogs.com/menlsh/archive/2013/02/26/2934434.html)中,我們是通過使用一個(gè)繼承自BaseAdapter類的派生類ImageAdapter來存儲圖片資源的。?
在該實(shí)例中,我們將新建一個(gè)ArrayList對象來存儲圖片資源,方法如下:
List<Drawable>?list?=?new?ArrayList<Drawable>();
然后,可以使用list.add()方法將圖片資源加載到該ArrayList對象中,具體方法如下:
1 //將圖片資源加載到ArrayList中 2 list.add(getResources().getDrawable(R.drawable.image1)); 3 list.add(getResources().getDrawable(R.drawable.image2)); 4 list.add(getResources().getDrawable(R.drawable.image3)); 5 list.add(getResources().getDrawable(R.drawable.image4));在該實(shí)例中,我們往ArrayList中加載了4張資源圖片。
?
4.獲取圖片資源
當(dāng)我們點(diǎn)擊“上一張”和“下一張”按鈕時(shí),需要獲取圖片資源進(jìn)行顯示,這該如何實(shí)現(xiàn)呢?
通過查看ImageSwitcher的API幫助文檔(http://developer.android.com/reference/android/widget/ImageSwitcher.html),我們可以看出向ImageSwitcher加載圖片有以下三種方式:
(1)public?void?setImageDrawable(Drawable?drawable);
(2)public?void?setImageResource(int?resid);
(3)public?void?setImageURI(Uri?uri);
其中,setImageDrawable()方法通過Drawable對象來獲取圖片資源;setImageResource()方法通過圖片資源Id來獲取圖片資源;setImageURI()方法通過圖片的Uri路徑來獲取圖片資源。
在該實(shí)例中,我們選擇使用setImageDrawable()方法來獲取圖片資源,這也就是為什么我們往ArrayList對象中存儲圖片時(shí)使用Drawable對象的原因。
?
5.按鍵處理
在該實(shí)例中,我們還需要實(shí)現(xiàn)OnClickListener接口的onClick()方法,對四個(gè)按鍵進(jìn)行事件監(jiān)聽,具體實(shí)現(xiàn)方法如下:
1 /* 2 * Function : 事件監(jiān)聽處理 3 * Author : 博客園-依舊淡然 4 */ 5 public void onClick(View v) { 6 switch (v.getId()) { 7 case R.id.forwardarrow: //向前箭頭按鈕按鍵處理 8 index--; 9 if (index < 0) { 10 index = list.size() - 1; 11 } 12 mImageSwitcher.setImageDrawable(list.get(index)); 13 break; 14 case R.id.nextarrow: //向后箭頭按鈕按鍵處理 15 index++; 16 if (index >= list.size()) { 17 index = 0; 18 } 19 mImageSwitcher.setImageDrawable(list.get(index)); 20 break; 21 case R.id.liftrotatearrow: //左旋箭頭按鈕按鍵處理 22 //TO DO 23 break; 24 case R.id.rightrotatearrow: //右旋箭頭按鈕按鍵處理 25 //TO DO 26 break; 27 } 28 }其中,變量index用于對圖片進(jìn)行索引,實(shí)現(xiàn)圖片的循環(huán)顯示,即當(dāng)顯示到最后一張圖片時(shí),再次點(diǎn)擊“下一張”,則將圖片索引號重置為0,然后重新顯示第一張圖片;當(dāng)顯示第一張圖片時(shí),再次點(diǎn)擊“上一張”,則將圖片的索引號重置為最大,然后顯示最后一張圖片。
在該實(shí)例中,并未對“左旋”和“右旋”按鈕進(jìn)行按鍵處理。要實(shí)現(xiàn)圖片的旋轉(zhuǎn)效果,請見博文《Android學(xué)習(xí)筆記11:圖像的平移、旋轉(zhuǎn)及縮放》(http://www.cnblogs.com/menlsh/archive/2012/12/02/2798802.html)。
?
?
總結(jié)
以上是生活随笔為你收集整理的Android学习笔记26:图片切换控件ImageSwitcher的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下使用SSH、Crontab、
- 下一篇: 一步步的教新手如何在一台物理机上部署红帽