我可以在Android中动态设置图片,但
如何获得5张图片,每次点击按钮都应该更改imageview中的图像。但是5张图片完成了重置旧图片..请帮助我..
OnClick(View v){
if(counter == 0){
imageView.setImageResource(R.drawable.image1);
}
if(counter == 1){
imageView.setImageResource(R.drawable.image2);
}
if(counter == 2){
imageView.setImageResource(R.drawable.image3);
}
if(counter == 3){
imageView.setImageResource(R.drawable.image4);
}
if(counter == 4){
imageView.setImageResource(R.drawable.image5);
}
if(counter == 5){
imageView.setImageResource(R.drawable.image6);
counter = -1;
}
counter++}
答案 0 :(得分:0)
这只是基于我们的逻辑。请自己想一想。
if(counter == 0){
imageView.setImageResource(R.drawable.image1);
counter++;
}else if(counter == 1){
imageView.setImageResource(R.drawable.image2);
counter++;
}else if(counter == 2){
imageView.setImageResource(R.drawable.image3);
counter++;
}else if(counter == 3){
imageView.setImageResource(R.drawable.image4);
counter++;
}else if(counter == 4){
imageView.setImageResource(R.drawable.image5);
counter++;
}else{
imageView.setImageResource(R.drawable.image6);
counter=0;
}
}
答案 1 :(得分:0)
更优雅的解决方案是将这些可绘制的ID存储在数组中,然后通过索引
进行选择 int counter = 0;
Integer[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(images[counter]);
if(counter++ >= images.length) counter = 0;
}
});