如何检查哪个图像链接到Android中的ImageView? [英] How to check which image is linked to ImageView in android?
问题描述
好吧,我的应用程序中有一个按钮和一个ImageView.我想做的是当我按下按钮时,ImageView上的图像将改变.我只有两个图片文件.
Well i have one button and one ImageView in my app. What i am trying to do is when i pressing on the button then the image at the ImageView will change. All i have are two pics file.
我想做的是-如果将第一个图片链接到ImageView,而不是通过单击按钮将其更改为pic2,并且如果将pic2链接了,则单击按钮会将其更改回第一个图片文件.
What i am trying to do is - if the first pic is linked to the ImageView than change it to pic2 by clicking on the button, and if pic2 is linked than a click on the button will change it back to the first pic file.
这是我尝试使用的onClick方法:
here's the onClick method i tried to use:
public void onClick(View v) {
ImageView ib1 = (ImageView)findViewById(R.id.imageView1)
View p1 = findViewById(R.drawable.pic1);
if(ib1.getResources()==R.drawable.pic1){
ib1.setImageResource(R.drawable.pic2);
}else{
ib1.setImageResource(R.drawable.pic1);
}
}
感谢任何帮助
推荐答案
我建议您每次更改图像时都设置 ImageView
的信息标签,而不是检查图像,例如:
Rather than checking the image, I would suggest set the information tag of the ImageView
each time you change the image, like:
if(ib1.getTag() != null && ib1.getTag().toString().equals("pic1")){
ib1.setImageResource(R.drawable.pic2);
ib1.setTag("pic2");
} else {
ib1.setImageResource(R.drawable.pic1);
ib1.setTag("pic1");
}
这篇关于如何检查哪个图像链接到Android中的ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!