is there any way to find resource Id of drawable

2020-03-08 09:17发布

Is there any way to get the Drawable resource ID? For example, I am using an ImageView and I may initially use icon.png as its image but later I may change the image to icon2.png. I want to find out using the code that which image my ImageView is using from the resource. Is there any way?

7条回答
家丑人穷心不美
2楼-- · 2020-03-08 10:02

Create a custom imageview, the rest is simple.

public class CustomImageView extends ImageView {

    private int resID;

    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageResource(int resId) {
        this.resID = resId;
        super.setImageResource(resId);
    }

    public int getResourceId() {
        return resID;
    }
}
查看更多
登录 后发表回答