1、代码:
private int[] imgageId= new int[]{R.drawable.img01,
R.drawable.img02,R.drawable.img03,R.drawable.img04,
R.drawable.img05,R.drawable.img06
};
private int index = 0;
private ImageSwitcher imageSwicher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwicher=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
// 设置动画效果
imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imageSwicher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
imageSwicher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView imageview = new ImageView(MainActivity.this);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return imageview;
}
});
Button up=(Button)findViewById(R.id.button1);
Button down= (Button)findViewById(R.id.button2);
up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(index >0){
index--;
}else{
index = imgageId.length-1;
}
// 显示当前图片
imageSwicher.setImageResource(imgageId[index]);
}
});
down.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(index<imgageId.length-1){
index++;
}else{
index=0;
}
imageSwicher.setImageResource(imgageId[index]);
}
});
}
2、现象