如何在android中按名称访问可绘制资源
在我的应用程序中,我需要在我不想保留引用的地方获取一些位图可绘制文件如何在android中按名称访问可绘制资源,android,drawable,Android,Drawable,在我的应用程序中,我需要在我不想保留引用的地方获取一些位图可绘制文件R。因此,我创建了一个类DrawableManager,来管理drawables public class DrawableManager { private static Context context = null; public static void init(Context c) { context = c; } public static Drawable getD
R
。因此,我创建了一个类DrawableManager
,来管理drawables
public class DrawableManager {
private static Context context = null;
public static void init(Context c) {
context = c;
}
public static Drawable getDrawable(String name) {
return R.drawable.?
}
}
然后,我想在这样的地方按名称获取drawable(car.png放在res/drawables中):
但是,如您所见,我无法通过以下名称访问资源:
public static Drawable getDrawable(String name) {
return R.drawable.?
}
有其他选择吗?请注意,您的方法几乎总是错误的(最好将上下文传递到使用可绘制的对象本身,而不是将静态
上下文保存在某个地方)
鉴于此,如果要进行动态可拉伸加载,可以使用:
你可以这样做-
public static Drawable getDrawable(String name) {
Context context = YourApplication.getContext();
int resourceId = context.getResources().getIdentifier(name, "drawable", YourApplication.getContext().getPackageName());
return context.getResources().getDrawable(resourceId);
}
为了从任何地方访问上下文,您可以扩展应用程序类-
public class YourApplication extends Application {
private static YourApplication instance;
public YourApplication() {
instance = this;
}
public static Context getContext() {
return instance;
}
}
并将其映射到您的清单
应用程序
标记中
<application
android:name=".YourApplication"
....
修改图像内容:
ImageView image = (ImageView)view.findViewById(R.id.imagenElement);
int resourceImage = activity.getResources().getIdentifier(element.getImageName(), "drawable", activity.getPackageName());
image.setImageResource(resourceImage);
使用Kotlin
fun Context.getResource(name:String): Drawable? {
val resID = this.resources.getIdentifier(name , "drawable", this.packageName)
return ActivityCompat.getDrawable(this,resID)
}
我把它写为扩展函数,这样它就可以在代码中的任何地方使用
注意:context.getResources().getDrawable(resourceId)代码>在Java中已被弃用
注意:文件名是没有扩展名的文件名,用于ex“a.png”如果您需要内部资源,文件名将是“a”
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("eskb048", "drawable",context.getPackageName());
// return like: R.drawable.eskb048.png
如果您需要牵引
检查第一个答案是否适用于您可以实现的所有目标
int resourceId = getResources().getIdentifier("your_drawable_name", "drawable", getPackageName());
在Imageview中设置resourceId
imageView.setImageResource(resourceId);
将上下文传递到使用可绘制对象的对象本身比将静态上下文保留在某个位置要好
Hi,你能告诉我更多吗?@hguser-你想要一个Drawable
的原因是要在屏幕上显示它-每个视图/活动/片段都有一个与之关联的上下文
-将该上下文
传递到你的getDrawable
方法中。我不知怎么搞不好这个。即使drawable可用,并且我正在传递正确的上下文,我的资源id也最终为0。@PratikMandrekar-假设您试图获取资源,例如R.drawable.your_image
,您可能希望使用resource.getIdentifier(“your_image”,“drawable”,context.getPackageName())
-如果没有特定的代码,很难看出哪里出了问题。请注意,您的资源名称末尾应该没有扩展名(至少对于图片),否则您的resourceId将为0。不要将“picture.jpg”作为名称传递,只传递“picture”。谢谢,我以前从来都不知道我可以通过这种方式获得上下文。它似乎会使代码更干净。以这种方式获取上下文,而不是将其传入,对可测试性不利。例如,要测试调用YourApplication.getContext()
的任何方法,测试必须首先实例化YourApplication
,以便YourApplication.getContext()
不返回null。以静态的形式创建全局变量通常是一种糟糕的做法。你能解释一下这个答案是如何添加其他答案没有添加的内容的吗?(特别是给定的!)可以从类内调用它吗!?
int resourceId = getResources().getIdentifier("your_drawable_name", "drawable", getPackageName());
imageView.setImageResource(resourceId);