Gallery
可以用来进行横向的图片浏览。不过在API 16以后,谷歌已经废弃了这个控件,推荐使用HorizontalScrollView
或者ViewPager
替代。
而ImageSwitcher
和Gallery
很像,可以用来进行图片的切换显示,并且可以添加动画效果。
在XML文件中创建Gallery
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
自定义适配器
新建适配器继承BaseAdapter
,需要重写其中的四个方法:
getCount()
:获取子控件的数量。一般直接返回数据源的数量。getItem()
:获取对应的数据源的内容。getItemId()
:获取对应数据源的位置。一般直接返回positiongetView()
:获取对应的视图。
一个用来显示图片的适配器如下:
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));
上面的代码中使用了系统自带的动画。