網路上流傳的 Gallery 範例大多只把圖片鋪滿螢幕,看起來能用,實際操作起來卻總差一點意思。我在舊版基礎上重寫了 Gallery 元件,補上了兩個最常被詬病的細節。
第一,縮圖下方可以顯示文字標題,瀏覽時不再是一堆無從辨識的色塊;第二,原生 Gallery 的滑動速度偏快,手指輕輕一掃就飛過好幾張,這裡把捲動節奏調慢,操作起來更貼手。
核心做法是自訂 Adapter:用 HashMap 快取每個位置的 View,避免 getView 反覆 inflate;再把 getCount 回傳 Integer.MAX_VALUE,讓列表可以無限循環,位置一律用取餘數映射回真正的資源。
縮圖項目的配置 gallery_item.xml 需要包含一個 ImageView(id 為 item_gallery_image)與一個 TextView(id 為 item_gallery_text),兩者上下排列即可。
package cn.yj3g.GalleryTest2;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class GalleryTestActivity extends Activity
implements AdapterView.OnItemClickListener,
ViewSwitcher.ViewFactory {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.image_switcher_1);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this, mThumbIds.length));
g.setOnItemClickListener(this);
g.setSelection(1);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);
}
public void onNothingSelected(AdapterView parent) {
}
@Override
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
private int mGalleryItemBackground;
private HashMap<Integer, View> mViewMaps;
private int mCount;
private LayoutInflater mInflater;
public ImageAdapter(Context c, int count) {
this.mCount = count;
mViewMaps = new HashMap<Integer, View>(count);
mInflater = LayoutInflater.from(GalleryTestActivity.this);
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
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) {
View viewGroup = mViewMaps.get(position % mCount);
if (viewGroup == null) {
viewGroup = mInflater.inflate(R.layout.gallery_item, null);
ImageView imageView = (ImageView) viewGroup.findViewById(R.id.item_gallery_image);
TextView textView = (TextView) viewGroup.findViewById(R.id.item_gallery_text);
imageView.setBackgroundResource(mGalleryItemBackground);
mViewMaps.put(position % mCount, viewGroup);
imageView.setImageResource(mImageIds[position % mImageIds.length]);
textView.setText(titles[position % mImageIds.length]);
}
return viewGroup;
}
}
private String[] titles = {"標題1", "標題2", "標題3", "標題4",
"標題5", "標題6", "標題7", "標題8",};
private Integer[] mThumbIds = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };
private Integer[] mImageIds = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7 };
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSwitcher.setImageResource(mThumbIds[position % mImageIds.length]);
}
}
滑動速度若還想再慢一些,可在 Gallery 的 Layout 上設定 android:animationDuration,或直接改寫 setSelection 後的捲動行為;標題字串則抽成 string-array 資源,日後維護更省事。