I have created 开发者_开发问答multiple bitmaps - one for each folder (mdpi, hdpi, ldpi). Can someone show me some code or point me to the appropriate method that will allow me to detect which resource Android decided to load.
Thanks,
Get the densityDPI from DisplayMetric and check it against predefined constants
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi)
{
case DisplayMetrics.DENSITY_HIGH: //HDPI
case DisplayMetrics.DENSITY_LOW: //LDPI
case DisplayMetrics.DENSITY_MEDIUM: //MDPI
}
This is available in the bitmap itself -- http://developer.android.com/reference/android/graphics/Bitmap.html#getDensity()
If you are loading this through the higher level Resources.getDrawable() API, you won't have a Bitmap but just an abstract Drawable. If you can guarantee the resource will always be a bitmap (not a nine patch or other such thing), you can cast this to a BitmapDrawable and get the Bitmap from that.
If you are doing this for production code, I would be a little uncomfortable with this since generally an app should either let the framework take care of density, or take care of it all itself (by for example putting the bitmaps in drawable-nodpi). If you are playing games with bitmaps base on loaded density, you may shoot yourself in the foot. Or you may be fine, since I don't really know what you are doing. :)
I'm not sure what can do with code but for testing purposes I added a small icon called resolution.png that had a version in the ldpi which had the letter 'l' in it, the version for mdpi had a letter 'm' in it and the version for hdpi had the letter 'h' in it. That way you can see which version of the resources are being loaded.
I test with it commented out in the layout most of the time and un-comment it when I want to investigate.
Load a Bitmap directly and compare it with the one loaded via resources:
Resources can be accessed as raw data: use AssetManager.open(..) Then you can use BitmapFactory.decodeStream(..) to create a Bitmap from the data stream.
精彩评论