Gallery:我们有时候在手机上或者pc上面看到动态的图片,可以通过鼠标或者手指触摸来移动它,产生动态的滚动效果,还可以根据你的点击或触摸触发其它事件响应。同样的,在Android中也提供这种实现,这就是通过Gallery在UI上实现缩略图浏览器。
1.准备数据源
private int[] res = { 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 };
2.准备适配器(自定义的ImageAdapter)
public class ImageAdapter extends BaseAdapter{
private int[]res;
private Context context;
public ImageAdapter(int []res,Context context)
{
this.res=res;
this.context=context;
}
//返回数据源的数量
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return res[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView image=new ImageView(context);
image.setBackgroundResource(res[position%res.length]);
image.setLayoutParams(new Gallery.LayoutParams(200, 150));
image.setScaleType(ScaleType.FIT_XY);//xy缩放到200、150
return image;
}
}
3.初始化适配器
adapter = new ImageAdapter(res, this);
4.gallery加载适配器
gallery.setAdapter(adapter);
自定义Adapter需继承BaseAdapter:
BaseAdapter中的重要方法:
1.public int getCount()//返回已定义的数据源总数量
2.public Object getItem(int position)
public long getItemId(int position)
//告诉适配器取得目前容器中的对象和数据ID
3.public View getView(int position,View convertView,ViewGroup parent)
//取得目前欲显示的图像View,传入数组ID值使之读取与成像
OnItemSelected监听器与ImageSwitcher的配合使用:
ImageSwitcher:与ImageView的功能有点类似,它们都可以用于显示图片,区别在于ImageSwicher的效果更炫,它可以指定图片切换时的动画效果。ImageSwicher的粗略理解就是ImageView的选择器,它需要设置ViewFactory。一般情况下,我们用该ViewFactory的makeView()方法返回ImageView。
为ImageSwitcher加入动画:
imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
imageSwicher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
gallery.setOnItemSelectedListener(this);
is.setFactory(this);
is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//image.setBackgroundResource(res[position%res.length]);
is.setBackgroundResource(res[position%res.length]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView image=new ImageView(this);
image.setScaleType(ScaleType.FIT_CENTER);
return image;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageSwitcher
android:id="@+id/is"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ImageSwitcher>
</LinearLayout>