如何确定加载了哪些位图资源(ldpi、mdpi 或 hdpi)?
我创建了多个位图 - 每个文件夹一个(mdpi、hdpi、ldpi)。有人可以向我展示一些代码或向我指出适当的方法,以便我能够检测 Android 决定加载哪些资源。
谢谢,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 DisplayMetric 获取 DensityDPI 并根据预定义常量进行检查
Get the densityDPI from DisplayMetric and check it against predefined constants
这在位图本身中可用 - http://developer.android。 com/reference/android/graphics/Bitmap.html#getDensity()
如果您通过更高级别的 Resources.getDrawable() API 加载它,您将不会拥有 Bitmap,而只是一个抽象的 Drawable。如果您可以保证资源始终是位图(不是九个补丁或其他类似的东西),您可以将其转换为 BitmapDrawable 并从中获取位图。
如果您正在为生产代码执行此操作,我对此会有点不舒服,因为通常应用程序应该让框架处理密度,或者自行处理所有问题(例如,将位图放入drawable-nodpi) 。如果您正在玩基于加载密度的位图的游戏,您可能会搬起石头砸自己的脚。或者你可能没问题,因为我真的不知道你在做什么。 :)
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. :)
我不确定代码可以做什么,但出于测试目的,我添加了一个名为resolution.png的小图标,该图标在ldpi中有一个版本,其中包含字母“l”,mdpi的版本有一个字母“m”其中,hdpi 的版本中有字母“h”。这样您就可以看到正在加载哪个版本的资源。
我大部分时间都在布局中将其注释掉进行测试,并在我想调查时取消注释。
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.
直接加载位图并将其与通过资源加载的位图进行比较:
资源可以作为原始数据访问:使用 AssetManager.open(..)
然后你可以使用 BitmapFactory.decodeStream(. .) 从数据流创建位图。
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.