现在我有一些类负责保存内部存储器位图,然后列在ListViews中。
列出后,求助于一种方法,告诉您要返回的图像名称,并接收相应的位图。
当应用程序启动时,将所有需要的图像加载到内存(HashMap)中,一旦将图像从内部存储器直接加载到列表视图,该过程需要很长时间。
我正在使用视图的回收,每个项目都有两个和两个图像视图文本视图。然而,scrooll非常缓慢并且有很多滞后。
将图像存储在内存(hashmap)中是一个很好的解决方案吗?在列表时加载到内存中甚至更慢。图像有100x100像素。
我的问题是:我的延迟问题可能与图像都在内存中的事实有关?什么是最好的解决方案?
修改
07-25 16:56:33.195: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.195: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.203: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.203: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.210: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.257: V/ADAPTER(8648): recicling view [16]
07-25 16:56:33.265: V/ADAPTER(8648): recicling view [15]
07-25 16:56:33.273: V/ADAPTER(8648): recicling view [14]
07-25 16:56:33.281: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.289: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.289: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.289: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.296: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.296: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.304: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.312: V/ADAPTER(8648): recicling view [7]
07-25 16:56:33.320: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.328: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.328: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.335: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.335: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.343: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.343: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.359: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.359: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.367: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.367: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.367: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.375: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.382: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.398: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.398: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.406: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.406: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.414: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.414: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.484: V/ADAPTER(8648): recicling view [13]
07-25 16:56:33.492: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.500: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.500: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.507: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.507: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.507: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.515: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.523: V/ADAPTER(8648): recicling view [7]
07-25 16:56:33.531: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.539: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.539: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.546: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.546: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.554: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.554: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.562: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.570: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.578: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.578: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.578: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.585: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.585: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.609: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.609: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.617: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.617: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.625: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.625: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.679: V/ADAPTER(8648): recicling view [12]
07-25 16:56:33.687: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.695: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.695: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.703: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.703: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.710: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.710: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.718: V/ADAPTER(8648): recicling view [7]
07-25 16:56:33.734: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.734: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.742: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.742: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.750: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.750: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.757: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.765: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.773: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.773: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.773: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.781: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.781: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.789: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.804: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.812: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.812: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.812: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.820: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.820: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.875: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.882: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.890: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.890: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.898: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.898: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.906: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.914: V/ADAPTER(8648): recicling view [7]
07-25 16:56:33.929: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.929: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.937: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.937: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.945: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.953: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.953: V/ADAPTER(8648): recicling view [6]
07-25 16:56:33.968: V/ADAPTER(8648): recicling view [0]
07-25 16:56:33.976: V/ADAPTER(8648): recicling view [1]
07-25 16:56:33.976: V/ADAPTER(8648): recicling view [2]
07-25 16:56:33.976: V/ADAPTER(8648): recicling view [3]
07-25 16:56:33.984: V/ADAPTER(8648): recicling view [4]
07-25 16:56:33.984: V/ADAPTER(8648): recicling view [5]
07-25 16:56:33.992: V/ADAPTER(8648): recicling view [6]
07-25 16:56:34.007: V/ADAPTER(8648): recicling view [0]
07-25 16:56:34.015: V/ADAPTER(8648): recicling view [1]
07-25 16:56:34.015: V/ADAPTER(8648): recicling view [2]
07-25 16:56:34.015: V/ADAPTER(8648): recicling view [3]
07-25 16:56:34.023: V/ADAPTER(8648): recicling view [4]
07-25 16:56:34.031: V/ADAPTER(8648): recicling view [5]
每当我滚动(列表继续),我得到这个输出。其中[]内的内容对应于位置。他经常出现0-6的位置并不奇怪吗?
修改
现在发现了一个可能的原因。
我目前是tab_activity,其中包含所有必要的视图。 在xml中添加了tab_activity top_bar,其中我有一个不断被更改的图像。当图像处于自动模式(不断更改)时,选项卡内的ListView会被“重新绘制”,因此会出现延迟。
我正在使用在x时间内更改x图像的服务(音乐)。
为什么在重新绘制图像时重新绘制ListViews?
答案 0 :(得分:0)
我不确定图片的加载是否导致延迟,但如果是,那么我建议您创建某种用户界面,向用户报告图片正在加载。 (要检查图像是否导致滞后,我建议你看看DDMS只是用LogCat登录)
我使用以下代码将位图保存到内存中:
以下内容将写入内存:
public void writeBitmapToMemory(String filename, Bitmap bitmap) {
FileOutputStream fos;
// Use the compress method on the Bitmap object to write image to the OutputStream
try {
fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
// this.gameEngineLog.d(classTAG, "Bitmap successfully written: " + filename);
}
catch (FileNotFoundException e) {
e.printStackTrace();
// this.gameEngineLog.d(classTAG, "Bitmap couldn't be written: " + filename);
}
catch (IOException e) {
e.printStackTrace();
// this.gameEngineLog.d(classTAG, "Bitmap couldn't be written: " + filename);
}
}
这将从内存中读取:
public Bitmap readBitmapFromMemory(String filename) {
Bitmap defautBitmap = null;
File filePath = game.getFileStreamPath(filename);
FileInputStream fi;
try {
fi = new FileInputStream(filePath);
defautBitmap = BitmapFactory.decodeStream(fi);
// this.gameEngineLog.d(classTAG, "Bitmap successfully read: " + filename);
}
catch (FileNotFoundException e) {
e.printStackTrace();
// this.gameEngineLog.d(classTAG, "Bitmap couldn't be opened: " + filename);
}
return defautBitmap;
}