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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值