我有一个动态填充项目的列表视图。 每个项目都有图像和文字。我知道这个上下文在其他stackoverflow的问题中如此重复。但是我仍然无法找到解决方案。
首先,我只有这个调用成功地在列表视图中设置每个图像。
imageview.setImageResource(getResuID());
尽管如此,由于滚动和延迟较慢,我希望在google training guide之后对其进行优化。
然后我尝试使用训练中的代码,问题就出现了。现在我从BitmapFactory到处都是null。
我read关于异步任务和GC,我还尝试将缩放移动到 AsyncTask ,这既没有解决问题。
这就是我的所作所为:
从" getView"调用任务在我的listview适配器中,它从BaseAdapter扩展:
int resID = getResID();
AsyncImageSetter task = (AsyncImageSetter) new AsyncImageSetter(thumb_image,resID).execute();
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public class AsyncImageSetter extends AsyncTask<Void, Void, Void> {
private ImageView img;
private int image_resId;
private Bitmap bmp;
public AsyncImageSetter( ImageView img, int image_ResId ) {
this.img = img;
this.image_resId = image_ResId;
}
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bmp = (decodeSampledBitmapFromResource(activity.getResources(), image_resId, 100, 100));
return null;
}
@Override
protected void onPostExecute(Void result) {
img.setImageBitmap(bmp);
bmp = null;
super.onPostExecute(result);
}
}
接收此日志消息:
01-07 19:57:54.167: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.167: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.277: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.277: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.277: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.306: D/dalvikvm(2141): GC_CONCURRENT freed 1247K, 12% free 10097K/11463K, paused 5ms+6ms
01-07 19:57:54.387: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.387: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.397: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.507: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.507: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.517: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.626: D/skia(2141): --- SkImageDecoder::Factory returned null
01-07 19:57:54.626: D/skia(2141): --- SkImageDecoder::Factory returned null
这是由于GC内存空闲吗?
这很奇怪,但如果不是
bmp = (decodeSampledBitmapFromResource(activity.getResources(), resources.getResID(), 100, 100));
我用
bmp = (decodeSampledBitmapFromResource(activity.getResources(), R.drawable.image1, 100, 100));
它有效&#34;罚款&#34;。我看到所有listview项目都有重复的图像。
此外,我尝试按照BitmapFun示例来合并缓存,但它确实是高级示例。也许你可以在没有这么多课程的情况下建议我一些更简单的方法。
由于