Android:从特定的-dpi文件夹访问drawable?

时间:2021-02-13 00:31:38

I have two drawable folders: drawable-mdpi and drawable-ldpi

我有两个drawable文件夹:drawable-mdpi和drawable-ldpi

I want to keep this structure (i.e. I don't' want to move my images to /assets), so that Android will automatically pick the appropriate artwork depending on the device density, however, on occasion I need to access the larger drawable version on the smaller device.

我想保留这个结构(即我不想将我的图像移动到/ assets),因此Android会根据设备密度自动选择合适的图片,但有时我需要访问更大的可绘制版本在较小的设备上。

Is there a way to access the drawable-ldpi folder from code? I thought the following might be the answer, but it did not work:

有没有办法从代码访问drawable-ldpi文件夹?我认为以下可能是答案,但它不起作用:

    Uri path = Uri.parse("android.resource://com.example.test/res/drawable-ldpi/icon");
    imageview.setImageURI(path); //assume imageview is already initialized etc.

I get a java.io.FileNotFoundException (no such file or directory) warning (it doesn't crash, but it just doesn't load either).

我得到一个java.io.FileNotFoundException(没有这样的文件或目录)警告(它没有崩溃,但它也没有加载)。

Thanks so much for your help!

非常感谢你的帮助!

3 个解决方案

#1


2  

probably not much help but there is Resources.getDrawableForDensity() but this is for API 15 :-(

可能没什么帮助,但有Resources.getDrawableForDensity(),但这是为API 15 :-(

#2


0  

Generally, if you ever need to use the HDPI version, keep ONLY the HDPI version of the image, and the lower density phone will automatically use the HDPI drawable because it has no choice (i.e. a lower resolution image with that drawable name does not exist).

通常,如果您需要使用HDPI版本,请仅保留图像的HDPI版本,而较低密度的手机将自动使用HDPI drawable,因为它没有选择(即,不存在具有该可绘制名称的较低分辨率图像)。

If you really need to switch between the hdpi and mdpi version I would suggest using a different filename and swapping programmatically, or showing/hiding XML elements if you prefer doing it that way... but that seems a little heavy-handed.

如果你真的需要在hdpi和mdpi版本之间切换,我会建议使用不同的文件名并以编程方式进行交换,或者显示/隐藏XML元素,如果你喜欢这样做......但这看起来有点笨拙。

#3


-1  

ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.abc);
image.setImageBitmap(bm);

Try reading THIS LINK also for more study over hdpi and mdpi concept

尝试阅读此链接还可以了解更多有关hdpi和mdpi概念的研究

#1


2  

probably not much help but there is Resources.getDrawableForDensity() but this is for API 15 :-(

可能没什么帮助,但有Resources.getDrawableForDensity(),但这是为API 15 :-(

#2


0  

Generally, if you ever need to use the HDPI version, keep ONLY the HDPI version of the image, and the lower density phone will automatically use the HDPI drawable because it has no choice (i.e. a lower resolution image with that drawable name does not exist).

通常,如果您需要使用HDPI版本,请仅保留图像的HDPI版本,而较低密度的手机将自动使用HDPI drawable,因为它没有选择(即,不存在具有该可绘制名称的较低分辨率图像)。

If you really need to switch between the hdpi and mdpi version I would suggest using a different filename and swapping programmatically, or showing/hiding XML elements if you prefer doing it that way... but that seems a little heavy-handed.

如果你真的需要在hdpi和mdpi版本之间切换,我会建议使用不同的文件名并以编程方式进行交换,或者显示/隐藏XML元素,如果你喜欢这样做......但这看起来有点笨拙。

#3


-1  

ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.abc);
image.setImageBitmap(bm);

Try reading THIS LINK also for more study over hdpi and mdpi concept

尝试阅读此链接还可以了解更多有关hdpi和mdpi概念的研究