To reply from @Paulorodrigues is pretty cool. But I leave my suggestion, that would extend the ImageView
, adding the behavior you want. Of course it involves a Refactoring if you are already using ImageView's
in your application, but it does not involve adding more code (which you may forget) when setting a tag
in the 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
for nome.do.seu.pacote.CustomImageView
.
Juarez, I don’t quite understand your question. Do you only want to store the Resid of a drawable in a variable? If so, the
R.drawable.icone_x
already does this job for you!– rsicarelli
This Imageview is dynamic, as the user clicks it changes image, I created a Reader and need to know which image it is currently showing.
– Skywalker