6

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

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

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

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

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

非常感谢你的帮助!

4

3 回答 3

2

可能没有太大帮助,但有 Resources.getDrawableForDensity() 但这适用于 API 15 :-(

于 2012-10-16T03:48:41.273 回答
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).

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.

于 2012-08-28T19:14:53.760 回答
-1
ImageView image = (ImageView)findViewById(R.id.imageView);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.abc);
image.setImageBitmap(bm);

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

于 2012-09-03T09:08:01.817 回答