Android 加载多个高质量位图并缩放它们
我的应用程序有一个严重的性能问题,当加载位图时,它似乎占用了很多内存 我有一个可绘制的文件夹,其中包含所有android设备的位图大小,这些位图质量很高。基本上,它会遍历每个位图,并根据大小为设备创建一个新的位图。(决定这样做是因为它支持正确的方向和任何设备)。它可以工作,但它占用了大量内存,并且需要很长时间才能加载。任何人都可以对以下代码提出任何建议Android 加载多个高质量位图并缩放它们,android,bitmap,Android,Bitmap,我的应用程序有一个严重的性能问题,当加载位图时,它似乎占用了很多内存 我有一个可绘制的文件夹,其中包含所有android设备的位图大小,这些位图质量很高。基本上,它会遍历每个位图,并根据大小为设备创建一个新的位图。(决定这样做是因为它支持正确的方向和任何设备)。它可以工作,但它占用了大量内存,并且需要很长时间才能加载。任何人都可以对以下代码提出任何建议 public Bitmap getBitmapSized(String name, int percentage, int screen_dim
public Bitmap getBitmapSized(String name, int percentage, int screen_dimention, int frames, int rows, Object params)
{
if(name != "null")
{
_tempInt = _context.getResources().getIdentifier(name, "drawable", _context.getPackageName());
_tempBitmap = (BitmapFactory.decodeResource(_context.getResources(), _tempInt, _BM_options_temp));
}
else
{
_tempBitmap = (Bitmap) params;
}
_bmWidth = _tempBitmap.getWidth() / frames;
_bmHeight = _tempBitmap.getHeight() / rows;
_newWidth = (screen_dimention / 100.0f) * percentage;
_newHeight = (_newWidth / _bmWidth) * _bmHeight;
//Round up to closet factor of total frames (Stops juddering within animation)
_newWidth = _newWidth * frames;
//Output the created item
/*
Log.w(name, "Item");
Log.w(Integer.toString((int)_newWidth), "new width");
Log.w(Integer.toString((int)_newHeight), "new height");
*/
//Create new item and recycle bitmap
Bitmap newBitmap = Bitmap.createScaledBitmap(_tempBitmap, (int)_newWidth, (int)_newHeight, false);
_tempBitmap.recycle();
return newBitmap;
}
这将节省空间。若不使用Alpha颜色,最好不要使用带有通道的颜色
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
// or Bitmap.Config.RGB_565 ;
// or Bitmap.Config.ARGB_4444 ;
Bitmap newBitmap = Bitmap.createScaledBitmap(_tempBitmap, (int)_newWidth, (int)_newHeight, options);
Android培训网站上有一个很好的指南:
这是关于位图图像的有效加载-强烈推荐
\u 8888
(包括alpha在内的每像素4字节)是内存方面可以获得的最大值。使用其他配置的缺点是,如果使用例如565,则绘制图像的速度可能会变慢,因为gfx硬件通常针对32位颜色进行优化。我只是提供了一个示例,但最终压缩将取决于使用情况。我使用各种配置。我不会在我的滚动背景上使用alpha,因为它们后面没有任何东西,并且会压缩更多的东西,比如爆炸,因为它们通常会快速运行动画。切换时我有了很大的改进,但我相信这取决于使用情况。我读过这篇文章,这似乎是将来加载位图的唯一方法。