Gallery和ImageSwitcher

Gallery可以用来进行横向的图片浏览。不过在API 16以后,谷歌已经废弃了这个控件,推荐使用HorizontalScrollView或者ViewPager替代。
ImageSwitcherGallery很像,可以用来进行图片的切换显示,并且可以添加动画效果。

在XML文件中创建Gallery

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

自定义适配器

新建适配器继承BaseAdapter,需要重写其中的四个方法:

  • getCount():获取子控件的数量。一般直接返回数据源的数量。
  • getItem():获取对应的数据源的内容。
  • getItemId():获取对应数据源的位置。一般直接返回position
  • getView():获取对应的视图。

一个用来显示图片的适配器如下:

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private int[] res;

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

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setBackgroundResource(res[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));
        imageView.setScaleType(ScaleType.CENTER_INSIDE);
        return imageView;
    }

}

在上面的代码中,重写了构造方法,用来传递数据源和上下文。

将图片设置为循环播放

如果想让图片循环播放,可以让getCount()返回一个很大的整数,然后对资源是视图的位置进行取余操作:

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private int[] res;

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

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

    @Override
    public Object getItem(int position) {
        return res[position % res.length];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setBackgroundResource(res[position % res.length]);
        imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));
        imageView.setScaleType(ScaleType.CENTER_INSIDE);
        return imageView;
    }

}

绑定适配器

在绑定适配器之前,先要准备好数据源。也就是要将图片资源的id保存在数组res[]中,然后新建适配器并调用setAdapter()就可以。

        gallery = (Gallery) findViewById(R.id.gallery);
        adapter = new ImageAdapter(this, res);
        gallery.setAdapter(adapter);

设置监听器

Gallery最常用的监听器是OnItemClickListener,它可以对点击事件进行响应:

        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

在XML文件中创建ImageSwitcher

    <ImageSwitcher
        android:id="@+id/image_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ImageSwitcher>

为ImageSwitcher建立视图

首先要在MainActivity中对ImageSwitcher进行初始化,然后调用setFactory()方法对ImageSwitcher建立视图。这里需要继承ViewFactory并重写其中的makeView()方法:

        imageSwitcher = (ImageSwitcher) findViewById(R.id.image_switcher);
        imageSwitcher.setFactory(new ViewFactory() {

            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getApplicationContext());
                imageView.setScaleType(ScaleType.FIT_CENTER);
                return imageView;
            }

为ImageSwitcher设置图像和动画

设置图像可以使用setBackgroundResources()方法,这样图片可以让填满整个控件空间。
设置动画有两个方法。setInAnimation()用来设置进入的动画;setOutAnimation()用来设置退出的动画。

        imageSwitcher.setBackgroundResource(res[position % res.length]);
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

上面的代码中使用了系统自带的动画。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值