Nodpi images vs. Android 1.5
I have an image in my app that I don't want to scale on loading. At the same time, I want to retain compatibility with Android 1.5.
So here's the deal. If I plac开发者_运维问答e the image under drawable
, Android 1.6+ thinks it's MDPI, and scales accordingly. If I place the image under drawable-nodpi
, Android 1.6+ does not scale it, but Android 1.5 cannot find it.
If I place a copy in both folders, Android 1.6 prefers the one in drawable
, and scales it as if MDPI. And I don't want to ship two copies of the same file, so this was a last resort measure anyway.
Can you think of any way out? Preferably a way that does not involve two copies of the file.
You could probarly do it though code. Put myimage.png in the drawable folder, and create a file called myimage_nodpi.xml in the drawable-nodpi folder.
In this xml file you put:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/myimage" />
This way, you won't have to copy the file.
In code, you can check if the API level is 4, and only load the nodpi version if that is the case:
int version = Integer.parseInt(android.os.Build.VERSION.SDK);
if(version >= 4) {
drawable = res.getDrawable(R.drawable.myimage_nodpi);
} else {
drawable = res.getDrawable(R.drawable.myimage);
}
Disclaimer: Not tested!
I've had the same issue with my app. Fortunately, there is a bitmap loader implementation that works around the Android bug. Just use UnscaledBitmapLoader.loadFromResource(getResources(), R.drawable.your_id, null)
.
I suggest trying to put images in two folders:
res\drawable
res\drawable-nodpi-v4
Does this article not do the trick for you?
Seems like you can trick your compiler to allow for the drawable-nodpi support, but still have 1.5 find it.
精彩评论