我正在开发一个Android应用程序,在那里我提供一些内置图像,并让用户可以选择从Web下载更多内容以在应用程序中使用.在我的应用程序的某个时刻,我在我的布局中查看ImageView,并想确定Drawable内部是内置资源还是我从Web下载到SD卡的图像.
有没有办法提取ImageView中使用的Drawable的URI?这样我就可以看到它是资源还是下载文件.
到目前为止,这是我的代码:
ImageView view = (ImageView) layout.findViewById(R.id.content_img);
Drawable image = view.getDrawable();
Run Code Online (Sandbox Code Playgroud)
更新:使用Barry Fruitman的建议,我将图像的URI直接存储在我的自定义ImageView中供以后使用.以下是我的实现:
public class MemoryImageView extends ImageView {
private String storedUri = null;
public MemoryImageView(Context context, String Uri) {
super(context);
}
public MemoryImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MemoryImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public String getStoredUri() {
return storedUri;
}
public void setStoredUri(String storedUri) {
this.storedUri = storedUri;
}
Run Code Online (Sandbox Code Playgroud)
}
用法如下:
MemoryImageView view = (MemoryImageView) layout.findViewById(R.id.content_img);
String img = view.getStoredUri();
if(img.startsWith("android.resource")) {
//in-built resource
} else {
//downloaded image
}
Run Code Online (Sandbox Code Playgroud)
不会。一旦创建了 Drawable,信息就会丢失。我建议您做的是子类化 ImageView 并添加额外的成员来跟踪您想要的任何内容。
代替:
<ImageView />
Run Code Online (Sandbox Code Playgroud)
和
<com.mypackage.MyImageView />
Run Code Online (Sandbox Code Playgroud)
并创建:
class MyImageView extends ImageView {
protected final int LOCAL_IMAGE = 1;
protected final int REMOTE_IMAGE = 2;
protected int imageType;
}
Run Code Online (Sandbox Code Playgroud)
MyImageView 的行为与 ImageView 完全相同,但有了这个额外的成员,您可以在任何您想要的地方读写。您可能还必须使用仅调用 super() 的构造函数来重写 ImageView 构造函数。
| 归档时间: |
|
| 查看次数: |
3166 次 |
| 最近记录: |