Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android ImageSwitcher 與 Gallery 搭配實作圖片輪播

要點拆解

(1)Gallery 只要綁一個 Adapter 就能運作。

(2)Activity 實作 ViewFactory 介面,並完成其中的 makeView() 方法,這個方法負責回傳一個 View 物件;接著呼叫 imageSwitcher.setFactory(this),Factory 的作用就是提供 ImageSwitcher 切換時所需的 View。

(3)實作 OnItemSelectedListener 供 Gallery 使用,需要覆寫 onItemSelected() 與 onNothingSelected()。

完整程式碼

public class ImageSwitcherActivity extends Activity
        implements OnItemSelectedListener, ViewFactory {

    private Gallery gallery;
    private ImageSwitcher imageSwitcher;
    private ImageAdapter imageAdapter;
    private int[] resId = {
        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
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gallery = (Gallery) findViewById(R.id.dddd);
        imageAdapter = new ImageAdapter(this);
        gallery.setAdapter(imageAdapter);
        gallery.setOnItemSelectedListener(this);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(
                AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(
                AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    }

    private class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
            // 這裡讀取自定義屬性 R.styleable.Gallery,定義 gallery 的預設邊框
            TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
            mGalleryItemBackground = typedArray.getResourceId(
                    R.styleable.Gallery_android_galleryItemBackground, 0);
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(resId[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
            imageView.setBackgroundResource(mGalleryItemBackground);
            return imageView;
        }
    }

    @Override
    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 onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        imageSwitcher.setImageResource(resId[arg2 % resId.length]);
    }

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

這裡有兩個小技巧值得注意:Adapter 的 getCount 回傳 Integer.MAX_VALUE,讓 Gallery 可以無限循環;取圖片時用 arg2 % resId.length 做取餘運算,避免索引超出陣列範圍。

Copyright © Linux教程網 All Rights Reserved