我是 Android 开发新手。在我使用的 Android 应用程序中HashMap
,但我收到警告:
**"Use new SparseArray<String>(...) instead for better performance"**
这是什么意思,我该如何使用SparseArray<String>
反而?
Use new SparseArray<String>(...)
而是为了更好的性能
由于所述原因,您收到此警告here http://developer.android.com/reference/android/util/SparseArray.html.
SparseArrays 将整数映射到对象。与普通数组不同
对象,索引中可能存在间隙。其目的是为了更多
比使用 HashMap 将整数映射到对象更高效。
Now
我如何使用 SparseArray ?
您可以通过以下方式进行:
-
HashMap
way:
Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
_bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
_bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
_bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(),
}
Bitmap bm = _bitmapCache.get(R.drawable.icon);
-
SparseArray
way:
SparseArray<Bitmap> _bitmapCache = new SparseArray<Bitmap>();
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
_bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
_bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
_bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(),
}
Bitmap bm = _bitmapCache.get(R.drawable.icon);
希望它会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)