java Android:如何将图像资源与 R.drawable.imagename 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6353170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to compare resource of an image with R.drawable.imagename?
提问by rizalp1
I am working on a sample application in which I need to get the resource of an image view in a onClick listener and compare that with the image source that I know exists. If the resources are the same, I want to launch another intent. The problem I am facing right now is to access that ImageView (and hence its resource Id integer) to compare to the drawable resource.
我正在开发一个示例应用程序,其中我需要在 onClick 侦听器中获取图像视图的资源,并将其与我知道存在的图像源进行比较。如果资源相同,我想启动另一个意图。我现在面临的问题是访问 ImageView(以及它的资源 Id 整数)以与可绘制资源进行比较。
@Override
// should int be final ??
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This is not working and I need to find a way to solve this >>
if (((ImageView)v).getResources().getInteger(0) == R.drawable.imageToCompare)
{
// do nothing
}
else
{
// do something
}
采纳答案by Michael
You can't get a drawable id from an ImageView
. But if you set it from code, you can also store it somewhere, for example in the tag field. Take a look at the similar question: Who I compare an background Image resource TextView with a R.drawable.bg_image for switch.
你不能从ImageView
. 但是如果你从代码中设置它,你也可以将它存储在某个地方,例如在标签字段中。看看类似的问题:谁我将背景图像资源 TextView 与 R.drawable.bg_image 进行比较。
回答by swat
if(((ImageView)v).getDrawable().getConstantState() == MainActivity.this
.getResources().getDrawable(R.drawable.unlock)
.getConstantState()))
{
// Do Something
}
回答by sandalone
The proper way is to user setTag()
and getTag
. I guess you are making your own adapter.
正确的方法是用户setTag()
和getTag
。我猜你正在制作自己的适配器。
In the method public View getView(int position, View convertView, ViewGroup parent)
, you set tag to Imageview
when you add other properties to it.
在方法中public View getView(int position, View convertView, ViewGroup parent)
,Imageview
当您向其添加其他属性时,将标记设置为。
ImageView temp = (ImageView) v.findViewById(resId);
temp.setImageResource(icon);
temp.setTag(icon); //drawable unique ID will be my identifier here
Then in the onClick
you simply compare View v
with your drawable
id. For example, I wanted to change image on click and I coded it this way. Take notice how I also change tag when I set a new drawable
然后在onClick
您只需View v
与您的drawable
id进行比较。例如,我想在单击时更改图像,并以这种方式对其进行编码。请注意我在设置新的可绘制对象时如何更改标签
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if ((Integer)v.getTag() == R.drawable.current_img) {
v.setBackgroundResource(R.drawable.new_img);
v.setTag(R.drawable.new_img);
}
}
});