Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Gallery 搭配 ImageSwitcher:打造帶淡入淡出的圖片瀏覽效果

編輯:關於 Android 程式設計

Gallery 提供橫向滾動的縮圖列表,ImageSwitcher 則負責在其上方展示大圖並支援切換動畫。兩者搭配,就能做出經典的圖片瀏覽器效果。

一、布局 activity_main.xml

用 RelativeLayout 上下排列:ImageSwitcher 在上、Gallery 在下。unselectedalpha 用來設定未選中縮圖的透明度。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:paddingTop="30px" />

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:spacing="5px"
        android:unselectedalpha="0.6" />
</RelativeLayout>

二、MainActivity.java

關鍵點有三個:為 ImageSwitcher 設定進出動畫、透過 ViewFactory 提供它要顯示的 ImageView,以及監聽 Gallery 的 OnItemSelectedListener 同步切換大圖。

public class MainActivity extends Activity {

    private int[] imageId = new int[] {
            R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e,
            R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j,
            R.drawable.k
    };

    private ImageSwitcher imageSwitcher;
    private Gallery gallery;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        gallery = (Gallery) findViewById(R.id.gallery1);

        // 進出場動畫
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

        // ImageSwitcher 需要一個 ViewFactory 來產生它要展示的 View
        imageSwitcher.setFactory(new ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                return imageView;
            }
        });

        gallery.setAdapter(new GalleryAdapter(MainActivity.this, imageId));
        // 預設選中中間那張
        gallery.setSelection(imageId.length / 2);

        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                imageSwitcher.setImageResource(imageId[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) { }
        });
    }
}

三、GalleryAdapter.java

繼承 BaseAdapter,在 getView() 中產生縮圖 ImageView。這裡用了 convertView 複用,並透過 obtainStyledAttributes 取得 Gallery 的項目背景樣式。

public class GalleryAdapter extends BaseAdapter {

    private int[] imageId;
    private Context mContext;

    public GalleryAdapter(Context mContext, int[] imageId) {
        this.mContext = mContext;
        this.imageId = imageId;
    }

    @Override
    public int getCount() {
        return imageId.length;
    }

    @Override
    public Object getItem(int position) {
        return imageId[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(180, 135));

            TypedArray typedArray = mContext.obtainStyledAttributes(R.styleable.Gallery);
            imageView.setBackgroundResource(typedArray.getResourceId(
                    R.styleable.Gallery_android_galleryItemBackground, 0));
            typedArray.recycle();  // 記得回收 TypedArray

            imageView.setPadding(5, 0, 5, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(imageId[position]);
        return imageView;
    }
}

記得在 res/values/attrs.xml 中宣告 Gallery 的 styleable,並在 res/values/styles.xml 裡給 android:galleryItemBackground 一個實際的樣式資源,否則 obtainStyledAttributes 會拿不到值。

四、實務提醒

Gallery 在 API 16 就已被標記為 deprecated,官方建議改用 HorizontalScrollView 搭配 RecyclerView(設定 LinearLayoutManager 且方向為 HORIZONTAL)來實作同等效果,並能自由客製選中態的縮放動畫。不過理解 Gallery + ImageSwitcher 的「Adapter 提供資料、ViewSwitcher 負責展示與動畫」這層分工,對後續學習 RecyclerView 仍有幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved