編輯:關於 Android 開發

製作圖片瀏覽器時,若能實現左右無限循環滑動,使用者體驗會大幅提升。要達成這個效果,關鍵在於自定義 Gallery 的 Adapter。
自定義 Adapter 前,需先掌握四個關鍵方法:getCount 返回資料源數量、getItem 返回指定位置的物件、getItemId 返回資料 ID、getView 取得要顯示的 View。
要實現左右循環滑動,做法是:讓 getCount 返回最大值(Integer.MAX_VALUE),所有資料 ID 對原始數量取餘數,最後把 Gallery 的初始位置設在中間。這樣一來,無論往哪個方向滑動都不會碰到盡頭。
public class MyAdapter extends BaseAdapter {
private int[] id_image;
private Context contex;
public MyAdapter(Context contex, int[] id_image) {
this.contex = contex;
this.id_image = id_image;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
return id_image[position % id_image.length];
}
@Override
public long getItemId(int position) {
return position % id_image.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(contex);
imageView.setBackgroundResource(id_image[position % id_image.length]);
imageView.setLayoutParams(new Gallery.LayoutParams(250, 200));
imageView.setScaleType(ScaleType.FIT_XY);
return imageView;
}
}
public class MainActivity extends Activity implements OnItemSelectedListener, ViewFactory {
private ImageSwitcher imageSwitcher;
private Gallery gallery;
private int[] id_image = { R.drawable.beauty1, R.drawable.beauty2, R.drawable.beauty3,
R.drawable.beauty4, R.drawable.beauty5, R.drawable.beauty6, R.drawable.beauty7,
R.drawable.beauty8, R.drawable.beauty9 };
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.id_gallery);
imageSwitcher = (ImageSwitcher) findViewById(R.id.id_imageSwitcher);
myAdapter = new MyAdapter(this, id_image);
imageSwitcher.setFactory(this);
gallery.setOnItemSelectedListener(this);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
gallery.setAdapter(myAdapter);
gallery.setSelection(id_image.length * 100); // 設為中間位置
}
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id) {
imageSwitcher.setBackgroundResource(id_image[position % id_image.length]);
}
@Override
public void onNothingSelected(AdapterView> parent) {
}
@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setScaleType(ScaleType.FIT_CENTER);
return image;
}
}
透過 ImageSwitcher 搭配淡入淡出動畫切換圖片,再加上 Gallery 的循環 Adapter,就能打造流暢自然的無限滑動瀏覽體驗。別忘了設定初始位置在中間,這一步是循環效果得以成立的關鍵。
Android GUI 系統之 WMS(二)——窗口添加流程全解析
Android GUI 系統之 WMS(二)——窗口添加流程全解析 Android 上的窗口大致可以分成兩類:一類由系統進程管理,即系統窗口,例如狀態欄、導航欄;另一類由
Android 以 GET 方式提交資料到伺服器:HttpURLConnection 完整實作
Android 以 GET 方式提交資料到伺服器:HttpURLConnection 完整實作 行動應用幾乎都要跟後端打交道。本文從零開始搭建一個最簡單的登入場景:先用
Android ImageScale 圖片變換:縮放、旋轉、平移與倒影效果
使用 Matrix 處理圖片變換在 Android 中,若需要對 Bitmap 進行縮放、旋轉、平移或鏡像處理,Matrix 是非常關鍵的 API。開發者可以先建立與原圖
Android 音訊焦點 AudioFocus 原始碼解析:多應用競爭下的播放仲裁
一台手機上往往裝著好幾個會出聲的 App。當它們同時想發音的時候,系統究竟讓誰出聲?如果只能讓一個出聲,憑什麼標準挑?為了處理這層關係,Android 從 2.3 開始引