Android ImageSwitcher
原创
©著作权归作者所有:来自51CTO博客作者wx59bdec579ef96的原创作品,请联系作者获取转载授权,否则将追究法律责任
ImageSwitcher 继承自ViewSwitcher。ImageSwitcher继承自ViewSwitcher,并重写了showNext,showPrevious方法,ViewFactory返回的组件必须是ImageView,使用更简单。只需要两步:
1. 为ImageSwitcher提供一个ViewFactory,ViewFactory返回的只能是ImageView
2. 切换图片时只需要调用ImageSwitcher的
public void setImageDrawable(Drawable drawable)
public void setImageResource(int resid)
public void setImageURI(Uri uri)
// -----------ImageSwitcher--------------
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
Button IButton1 = (Button) findViewById(R.id.IButton1);
Button IButton2 = (Button) findViewById(R.id.IButton2);
final int currentPig[] = { -1 };// 数据源设为上面的那个数组。
// int[] images = {};
imageSwitcher.setInAnimation(ViewAnimatorDemo.this,
android.R.anim.fade_in);
imageSwitcher.setOutAnimation(ViewAnimatorDemo.this,
android.R.anim.fade_out);
imageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView image = new ImageView(ViewAnimatorDemo.this);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return image;
}
});
OnClickListener IListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.IButton1) {
if (--currentPig[0] < 0)
currentPig[0] = images.length - 1;
} else {
if (++currentPig[0] >= images.length)
currentPig[0] = 0;
}
imageSwitcher.setImageResource(images[currentPig[0]]);
}
};
IButton1.setOnClickListener(IListener);
IButton2.setOnClickListener(IListener);
// 初始化一下
currentPig[0] = 0;
imageSwitcher.setImageResource(images[currentPig[0]]);