Gallery組件主要用于橫向顯示圖像列表,不過按常規(guī)做法。Gallery組件只能有限地顯示指定的圖像。也就是說,如果為Gallery組件指定了10張圖像,那么當(dāng)Gallery組件顯示到第10張時,就不會再繼續(xù)顯示了。這雖然在大多數(shù)時候沒有什么關(guān)系,但在某些情況下,我們希望圖像顯示到最后一張時再重第1張開始顯示,也就是循環(huán)顯示。要實(shí)現(xiàn)這種風(fēng)格的Gallery組件,就需要對Gallery的Adapter對象進(jìn)行一番改進(jìn)。
Gallery組件的傳統(tǒng)用法
<!--[endif]-->
在實(shí)現(xiàn)可循環(huán)顯示圖像的Gallery組件之前先來回顧一下Gallery組件的傳統(tǒng)用法。Gallery組件可以橫向顯示一個圖像列表,當(dāng)單擊當(dāng)前圖像的后一個圖像時,這個圖像列表會向左移動一格,當(dāng)單擊當(dāng)前圖像的前一個圖像時,這個圖像列表會向右移動一樣。也可以通過拖動的方式來向左和向右移動圖像列表。當(dāng)前顯示的是第1個圖像的效果如圖1所示。Gallery組件顯示到最后一個圖像的效果如圖2所示。
從圖2可以看出,當(dāng)顯示到最后一個圖像時,列表后面就沒有圖像的,這也是Gallery組件的基本顯示效果。在本文后面的部分將詳細(xì)介紹如何使Gallery組件顯示到最后一個圖像時會從第1個圖像開始顯示。
好了,現(xiàn)在我們來看一下圖1和圖2的效果是如何做出來的吧。Gallery既然用于顯示圖像,那第1步就必須要有一些圖像文件用來顯示,F(xiàn)在可以隨意準(zhǔn)備一些圖像。在本文的例子中準(zhǔn)備了15個jpg文件(item1.jpg至item15.jpg)。將這些文件都放在res\drawable目錄中。
下面將這些圖像的資源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文件中配置了一個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方法中裝載這個組件,代碼如下:
代碼
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對象
ImageAdapter imageAdapter = new ImageAdapter(this);
// 設(shè)置Gallery組件的Adapter對象
gallery.setAdapter(imageAdapter);
}
在上面的代碼中涉及到一個非常重要的類:ImageAdapter。該類是android.widget.BaseAdapter的子類,用于描述圖像信息。下面先看一下這個類的完整代碼。
代碼
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對象
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;
}
}
在編寫ImageAdapter類時應(yīng)注意的兩點(diǎn):
1. 在ImageAdapter類的構(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類中有兩個非常重要的方法:getCount和getView。其中g(shù)etCount方法用于返回圖像總數(shù),要注意的是,這個總數(shù)不能大于圖像的實(shí)際數(shù)(可以小于圖像的實(shí)際數(shù)),否則會拋出越界異常。當(dāng)Gallery組件要顯示某一個圖像時,就會調(diào)用getView方法,并將當(dāng)前的圖像索引(position參數(shù))傳入該方法。一般getView方法用于返回每一個顯示圖像的組件(ImageView對象)。從這一點(diǎn)可以看出,Gallery組件是即時顯示圖像的,而不是一下將所有的圖像都顯示出來。在getView方法中除了創(chuàng)建了ImageView對象,還用從resIds數(shù)組中獲得了相應(yīng)的圖像資源ID來設(shè)置在ImageView中顯示的圖像。最后還設(shè)置了Gallery組件的背景顯示風(fēng)格。
OK,現(xiàn)在來運(yùn)行這個程序,來回拖動圖像列表,就會看到如圖1和圖2所示的效果了。
循環(huán)顯示圖像的原理
<!--[endif]-->
循環(huán)顯示有些類似于循環(huán)鏈表,最后一個結(jié)點(diǎn)的下一個結(jié)點(diǎn)又是第1個結(jié)點(diǎn)。循環(huán)顯示圖像也可以模擬這一點(diǎn)。
也許細(xì)心的讀者從上一節(jié)實(shí)現(xiàn)的ImageAdapter類中會發(fā)現(xiàn)些什么。對!就是getView方法中的position參數(shù)和getCount方法的關(guān)系。position參數(shù)的值是不可能超過getCount方法返回的值的,也就是說,position參數(shù)值的范圍是0至getCount() - 1。
如果這時Gallery組件正好顯示到最后一個圖像,position參數(shù)值正好為getCount() - 1。那么我們?nèi)绾卧僮孏allery顯示下一個圖像呢?也就是說讓position參數(shù)值再增1,對!將getCount()方法的返回值也增1。
那么這里還有一個問題,如果position參數(shù)值無限地增加,就意味著resIds數(shù)組要不斷地增大,這樣會大大消耗系統(tǒng)的資源。想到這,就需要解決兩個問題:既要position不斷地增加,又讓resIds數(shù)組中保存的圖像資源ID是有限的,該怎么做呢?對于getCount()方法非常好解決,可以讓getCount方法返回一個很大的數(shù),例如,Integer.MAX_VALUE。這時position參數(shù)值就可以隨著Gallery組件的圖像不斷向前移動而增大,F(xiàn)在resIds數(shù)組只有15個元素,如果position的值超過數(shù)組邊界,要想繼續(xù)循環(huán)取得數(shù)組中的元素(也就是說,當(dāng)position的值是15時,取resIds數(shù)組的第0個元素,是16時取第1個元素),最簡單的方法就是取余,代碼如下:
resIds[position % resIds.length]
在本節(jié)對ImageAdapter類做了如下兩個改進(jìn):
1. 使getCount方法返回一個很大的值。建議返回Integer.MAX_VALUE。
2. 在getView方法中通過取余來循環(huán)取得resIds數(shù)組中的圖像資源ID。
通過上面兩點(diǎn)改進(jìn),可以使圖像列表在向右移動時會循環(huán)顯示圖像。當(dāng)然,這種方法從本質(zhì)上說只是偽循環(huán),也就是說,如果真把圖像移動到getCount方法返回的值那里,那也就顯示到最后一個圖像的。不過在這里getCount方法返回的是Integer.MAX_VALUE,這個值超過了20億,除非有人真想把圖像移動到第20億的位置,否則Gallery組件看著就是一個循環(huán)顯示圖像的組件。
實(shí)現(xiàn)循環(huán)顯示圖像的Gallery組件
<!--[endif]-->
在本節(jié)將組出與循環(huán)顯示圖像相關(guān)的ImageAdapter類的完整代碼。讀者可以從中看到上一節(jié)介紹的兩點(diǎn)改進(jìn)。為了使界面看上去更豐滿,本例還在單擊某一個Gallery組件中的圖像時在下方顯示一個放大的圖像(使用ImageSwitcher組件)。本例的顯示效果如圖3所示。當(dāng)不斷向后移動圖像時,圖像可不斷顯示,讀者可以自己運(yùn)行本例來體驗(yàn)一下。
<!--[endif]-->
在main.xml文件中定義的Gallery和ImageSwitcher組件的代碼如下:
代碼
<?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" />
<ImageSwitcher android:id="@+id/imageswitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</LinearLayout>
本例中Main類的完整代碼如下:
代碼
package net.blogjava.mobile;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class Main extends Activity implements OnItemSelectedListener,
ViewFactory
{
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private ImageAdapter imageAdapter;
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 };
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context context)
{
mContext = context;
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0);
}
// 第1點(diǎn)改進(jìn),返回一個很大的值,例如,Integer.MAX_VALUE
public int getCount()
{
return Integer.MAX_VALUE;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(mContext);
// 第2點(diǎn)改進(jìn),通過取余來循環(huán)取得resIds數(shù)組中的圖像資源ID
imageView.setImageResource(resIds[position % resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
// 選中Gallery中某個圖像時,在ImageSwitcher組件中放大顯示該圖像
imageSwitcher.setImageResource(resIds[position % resIds.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
@Override
// ImageSwitcher組件需要這個方法來創(chuàng)建一個View對象(一般為ImageView對象)
// 來顯示圖像
public View makeView()
{
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter = new ImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(this);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
// 設(shè)置ImageSwitcher組件的工廠對象
imageSwitcher.setFactory(this);
// 設(shè)置ImageSwitcher組件顯示圖像的動畫效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
}
總結(jié)
在本文介紹了如何實(shí)現(xiàn)可循環(huán)顯示的Gallery組件。實(shí)際上,這個循環(huán)顯示只是一個偽循環(huán),不過由于getCount方法返回的圖像總數(shù)很大(超過20億),這就意味著已經(jīng)非常接近無限循環(huán)了。實(shí)現(xiàn)循環(huán)顯示圖像的關(guān)鍵點(diǎn)有如下兩個:
1. getCount方法返回一個很大的整數(shù)值(例如,Integer.MAX_VALUE)。
2. 在getView方法中通過取余的方法來循環(huán)獲得圖像的資源ID。