开发者

Android: How to compare resource of an image with R.drawable.imagename?

开发者 https://www.devze.com 2023-03-13 13:35 出处:网络
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 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.

@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)
                {
   开发者_JS百科                 // do nothing
                }
                else
                {
                    // do something         
                }


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.


if(((ImageView)v).getDrawable().getConstantState() == MainActivity.this
                        .getResources().getDrawable(R.drawable.unlock)
                        .getConstantState()))
{

// Do Something
}


The proper way is to user setTag() and getTag. I guess you are making your own adapter.

In the method public View getView(int position, View convertView, ViewGroup parent), you set tag to Imageview when you add other properties to it.

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

        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);
                } 
            }
        });


You can use this.

  if((pic1.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.addpics).getConstantState()))
    {
      //do something
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号