可循環顯示圖像的Android Gallery組件
https://www.cnblogs.com/nokiaguy/archive/2010/08/23/1806870.html
Gallery組件主要用於橫向顯示圖像列表,不過按常規做法。Gallery組件隻能有限地顯示指定的圖像。也就是說,如果為Gallery組件指定了10張圖像,那麼當Gallery組件顯示到第10張時,就不會再繼續顯示了。這雖然在大多數時候沒有什麼關係,但在某些情況下,我們希望圖像顯示到最後一張時再重第1張開始顯示,也就是循環顯示。要實現這種風格的Gallery組件,就需要對Gallery的Adapter對象進行一番改進。
Gallery組件的傳統用法
<!--[endif]-->


從圖2可以看出,當顯示到最後一個圖像時,列表後麵就沒有圖像的,這也是Gallery組件的基本顯示效果。在本文後麵的部分將詳細介紹如何使Gallery組件顯示到最後一個圖像時會從第1個圖像開始顯示。
好了,現在我們來看一下圖1和圖2的效果是如何做出來的吧。Gallery既然用於顯示圖像,那第1步就必須要有一些圖像文件用來顯示。現在可以隨意準備一些圖像。在本文的例子中準備了15個jpg文件(item1.jpg至item15.jpg)。將這些文件都放在res\drawable目錄中。
下麵將這些圖像的資源ID都保存在int數組中,代碼如下:
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 };
{ 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組件,代碼如下:

代碼
現在在onCreate方法中裝載這個組件,代碼如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
<LinearLayout xmlns:android="https://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>
在本例的main.xml文件中配置了一個Gallery組件,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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>
<LinearLayout xmlns:android="https://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>
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 裝載Gallery組件
Gallery gallery = (Gallery) findViewById(R.id.gallery);
// 創建用於描述圖像數據的ImageAdapter對象
ImageAdapter imageAdapter = new ImageAdapter(this);
// 設置Gallery組件的Adapter對象
gallery.setAdapter(imageAdapter);
}
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 裝載Gallery組件
Gallery gallery = (Gallery) findViewById(R.id.gallery);
// 創建用於描述圖像數據的ImageAdapter對象
ImageAdapter imageAdapter = new ImageAdapter(this);
// 設置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);
}
// 返回圖像總數
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);
// 設置當前圖像的圖像(position為當前圖像列表的位置)
imageView.setImageResource(resIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
// 設置Gallery組件的背景風格
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
{
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);
}
// 返回圖像總數
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);
// 設置當前圖像的圖像(position為當前圖像列表的位置)
imageView.setImageResource(resIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
// 設置Gallery組件的背景風格
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
在編寫ImageAdapter類時應注意的兩點:
1. 在ImageAdapter類的構造方法中獲得了Gallery組件的屬性信息。這些信息被定義在res\values\attrs.xml文件中,代碼如下:
最後更新:2017-04-02 06:51:59