how to get the source of ImageView in order to change it?

后端 未结 7 1867
醉话见心
醉话见心 2020-12-15 06:23

I know that changing the ImageView resource is not big deal just using myImageView.setImageResource(mynewImageDrawable)

but what I want to do is to chec

7条回答
  •  庸人自扰
    2020-12-15 06:42

    There is no getDrawableId function so you'll need to do something like set a tag for the ImageView when you change its drawable. For instance, set the drawable id as a tag for the ImageView so you could just get the drawable id from the tag.

    How to do that?

    I'd say 90% of the time, your views wont have any tag on them, so the easiest way is to assume your tag is the only tag:

    myImageView.setTag(R.drawable.currentImage);    //When you change the drawable
    int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id
    

    What if I already have a tag on my view

    Android views can host multiple tags at the same time, as long as they have a unique identifier. You'd need to create a unique id resource and add it as the first argument to the setTag method call. Leaving the code like this:

    myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
    int drawableId = (Integer)myImageView.getTag(R.id.myTagId);
    

提交回复
热议问题