0

我正在按照此处Android Smooth Scrolling的说明进行平滑滚动,但 onPostExecute 上的方法 setImageBitmap 仍然冻结我的列表视图。这是我的异步任务代码

private static class ThumbnailTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public ThumbnailTask(ImageView imageView) {

        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(Integer... params) {

        Bitmap image =  fragment.getAlbum().getPhotos().get(params[0]).getImage(Utils.THUMBNAIL_SCREEN_SIZE);
        return ThumbnailUtils.extractThumbnail(image, 40, 40);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }




}

这是我在 getView 方法上使用它的方式

 new ThumbnailTask(holder.image).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, position);

漫长的过程是doInBackground方法中的“getImage”,我只是将图像放在onPostExecute上的ImageView上。如果我评论 setImageBitmap,滚动会再次变得平滑。我做错了什么?

4

1 回答 1

0

我认为您需要在异步任务中使用 get() 。我认为当您注释掉这一行时,您看不到 get 的负面影响:

imageView.setImageBitmap(bitmap);

但这不是问题。

我推荐这个:

private static class ThumbnailTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;
    private final ImageView yourImageView = null;
    public ThumbnailTask(ImageView imageView) {

        imageViewReference = new WeakReference<ImageView>(imageView);
    }

    @Override
    protected Bitmap doInBackground(Integer... params) {

        Bitmap image =  fragment.getAlbum().getPhotos().get(params[0]).getImage(Utils.THUMBNAIL_SCREEN_SIZE);
        return ThumbnailUtils.extractThumbnail(image, 40, 40);
        if (imageViewReference != null) {
            yourImageView = imageViewReference.get();
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}
于 2013-07-29T05:47:04.507 回答