Gallery可以在UI中实现缩略图浏览器
自定义适配器--通过继承BaseAdapter
BaseAdapter 的重要方法
首先要自己定义构造方法,一般需要传入的值也要定义好。
- public int getCount()--返回已定义的数据源的总数量
- public Object getItem(int position)
- public long getItemId(int position)
- public View getView(int position,View concertView,ViewGroup parent)
- 取得目前要显示的图像View,传入数组ID值使之读取与成像
OnItemSelectListenner 监听器
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
- public void onNothingSelected(AdapterView<?> arg0)
ImageSwitcher 功能类似ImageView,但是 ImageSwitcher可以再切换
图片添加炫酷的动画效果。加入动画的方法有:
- .setInAnimation()
- .setOutAnimation()
ViewFactory,可以理解为ImageView 的选择器,它需要设置ViewFactory
,一般情况下用ViewFactory的makeView方法来返回ImageView。
example:
-----------------------------------------------------------------------------------------------------------
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{
private int []res={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,
R.drawable.pic6,R.drawable.pic7,R.drawable.pic8,R.drawable.pic9,R.drawable.pic10,
R.drawable.pic11,R.drawable.pic12};
private Gallery gallery;
private ImageAdapter adaptet;
private ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switcher= (ImageSwitcher) findViewById(R.id.imageSwitcher);
switcher.setFactory(this);
gallery = (Gallery) findViewById(R.id.gallery);
adaptet = new ImageAdapter(this, res);
gallery.setAdapter(adaptet);
gallery.setOnItemSelectedListener(this);
switcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
switcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
switcher.setBackgroundResource(res[arg2%res.length]);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public View makeView() {
// TODO Auto-generated method stub
ImageView view =new ImageView(this);
view.setScaleType(ScaleType.FIT_CENTER);
return view;
}
}
----------------------------------------------------------------------------------------------------------------------
imageAdapter.java
public class ImageAdapter extends BaseAdapter{
private int []res;
private Context context;
public ImageAdapter(Context context,int []res){
this.res=res;
this.context=context;
}
//返回数据源的数量
public int getCount() {
// TODO Auto-generated method stub
// 返回整形的最大值来保证Gallery的无限循环
return Integer.MAX_VALUE;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return res[arg0];
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView image = new ImageView(context);
//通过求余数 保证无限循环
mage.setBackgroundResource(res[arg0%res.length]);
image.setLayoutParams(new Gallery.LayoutParams(200,150));
image.setScaleType(ScaleType.FIT_XY);
return image;
}
}