Android筆記之Gallery與ImageSwitcher

在android4.1之前,這個使用的比較多,但在4.1之後,谷歌推薦我們使用ViewPager,使用Gallery可以實現實現圖片滾動,配合ImageSwitcher可以實現圖片查看器效果,下面是一個demo:
GalleryActivity.java

public class GalleryActivity extends Activity implements AdapterView.OnItemSelectedListener ,ViewSwitcher.ViewFactory {
    private final String TAG = "GalleryActivity";
    private Gallery mGallery;
    private ImageSwitcher imageSwitcher;

    private int[] ids;

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

        ids = new int[]{R.mipmap.little_boy_black,R.mipmap.little_boy_blue,R.mipmap.little_boy_green,R.mipmap.little_boy_grey,
                R.mipmap.little_boy_orange,
                R.mipmap.little_boy_pink,R.mipmap.little_boy_purple,R.mipmap.little_boy_red,
                R.mipmap.little_boy_white,R.mipmap.little_boy_yellow};

        mGallery = (Gallery)findViewById(R.id.gallery);


        mGallery.setAdapter(new ImageAdapter(this,ids));
        mGallery.setOnItemSelectedListener(this);

        //要實現ViewSwitcher.ViewFactory,重寫makeView方法產生ImageView
        imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);
        imageSwitcher.setFactory(this);
        //設置動畫
        imageSwitcher.setInAnimation(this,android.R.anim.fade_in);
        imageSwitcher.setOutAnimation(this,android.R.anim.fade_out);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"position:" + position + ",currentPos:" + position%ids.length);
        imageSwitcher.setBackgroundResource(ids[position%ids.length]);
    }

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

    }

    @Override
    public View makeView() {
        ImageView imageView = new ImageView(this);
        //縮放模式:按比例縮放,且居中
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return imageView;
    }
}

要實現OnItemSelectedListener 接口去監聽當前選中那張圖片,在onItemSelected方法裏面會獲取但當前圖片在Gallery的位置,因此可以在這裏做文章,下面是Gallery的Adapter,很簡單,具體代碼如下,而如果我們需要ImageSwitcher來顯示圖片的話,需要實現ViewFactory接口,重寫makeView,在裏面創建ImageSwitcher需要的ImageViwe。
ImageAdapter.java

public class ImageAdapter extends BaseAdapter{
    private Context mContext;
    private int[] mIds;

    public ImageAdapter(Context context,  int[] ids) {
        this.mContext = context;
        this.mIds = ids;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;//爲了實現無線滾動
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setBackgroundResource(mIds[position%mIds.length]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY); //設置圖片x,y自動拉伸
        imageView.setLayoutParams(new Gallery.LayoutParams(200,150)); //設置在gallery大小
        return imageView;
    }
}

佈局文件:gallery_view_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Gallery
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gallery" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
發佈了36 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章