The @PauloRodrigues answer is pretty cool. But I leave my suggestion, which would be to extend ImageView
, adding the behavior you want. Of course, it involves refactoring if you are already using ImageView's
in your application, but do not involve adding more code (which you can forget) by setting tag
to ImageView
. >
The subclass would be:
public class CustomImageView extends ImageView {
private int mImageResource;
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs, 0, defStyle);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
// Recupera o id do resource setado no xml
final TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ImageView, defStyleAttr, defStyle);
mImageResource = a.getResourceId(com.android.internal.R.styleable.ImageView_src, 0);
a.recycle();
}
@Override
public void setImageResource(int resId) {
mImageResource = resId;
super.setImageResource(resId);
}
public int getImageResource() {
return mImageResource;
}
}
The use in xml would be similar to the others, just changing ImageView
by nome.do.seu.pacote.CustomImageView
.