Far*_*an 24 resources android drawable
有没有办法获得Drawable资源ID?例如,我使用的是ImageView,我最初可能会使用icon.png作为其图像,但稍后我可能会将图像更改为icon2.png.我想找出使用ImageView正在使用的图像资源的代码.有什么办法吗?
PIR*_*HAH 14
当你在ur prgoram中点击ImageView时,这个是查找R.drawablw.image1值的最佳方法.该方法有点像在主程序中首先保存标签中的图像值就像那样
public...activity
{
//-----this vl store the drawable value in Tag of current ImageView,which v vl retriew in //image ontouchlistener event...
ImageView imgview1.setTag(R.drawable.img1);
ImageView imgview2.setTag(R.drawable.img2);
onTouchListnener event...
{
ImageView imageView = (ImageView) v.findViewById(R.id.imgview1)v;
Object tag = imageView.getTag();
int id = tag == null ? -1 : Integer.parseInt(tag.toString());
switch(id)
{
case R.drawable.img1:
//do someoperation of ur choice
break;
case R.drawable.img2:
//do someoperation of ur choice
break:
}//end of switch
}//end of touch listener event
}//end of main activity
"PIR FAHIM SHAH/kpk uet mardan campus"
Run Code Online (Sandbox Code Playgroud)
您是否正在尝试确定当前图像的内容imageview,以便将其更改为其他图像?
如果是这样,我建议使用代码而不是xml来做所有事情.
即用于在初始化期间setImageResource()设置初始图像,并跟踪resource ids代码中某处使用的内容.
例如,您可以拥有一个包含imageviews相应数组的数组,int其中包含resource idfor eachimageview
然后,每当您想要更改图像时,循环遍历数组并查看id是什么.
创建自定义imageview,其余的很简单.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40993 次 |
| 最近记录: |