无效的代码
public void fade(View v){
ImageView pic1 = (ImageView) findViewById(R.id.pic1);
ImageView pic2 = (ImageView) findViewById(R.id.pic2);
pic1.animate().alpha(0f).setDuration(2000);
pic2.animate().alpha(1f).setDuration(2000);
}
答案 0 :(得分:1)
不太确定你要求的是什么,但是如果你想在onClick上来回更改图像,你可以尝试修改我在下面写的代码。
// set a boolean value
private boolean firstImageShown = true;
public void clickFunction(View view) {
ImageView firstImage = (ImageView) findViewById(R.id.firstImage);
if ((firstImage != null) && (firstImageShown)) {
firstImage.setImageResource(R.drawable.sample_image2);
firstImageShown = false;
} else {
if (firstImage != null) firstImage.setImageResource(R.drawable.sample_image1);
firstImageShown = true;
}
}
答案 1 :(得分:1)
//Suppose you have two images rose_1 and rose_2 and you want to switch back
//and forth between these on a button press.
//Set a static variable
private static int switcherPressedCount = 2;
//Map the onClick attribute of the button to a function say "switcherPressed".
//"imageView1" is the id attribute of the image which first gets loaded in the
//app, in this example, it is the attribute of rose_1.
public void switcherPressed(View view){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
if(switcherPressedCount%2 == 0) {
imageView.setImageResource(R.drawable.rose_2);
switcherPressedCount = 3; //to avoid reaching maximum int limit.
}
else {
imageView.setImageResource(R.drawable.rose_1);
switcherPressedCount = 2; //to avoid reaching maximum int limit.
}
}