使用异步任务在 gridview 中加载图像,加载不正确 [英] Loading images in a gridview with async task, not loading correctly

查看:13
本文介绍了使用异步任务在 gridview 中加载图像,加载不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 gridview 异步中加载缩略图,因为其他方式需要很长时间才能显示!

I'm trying to load thumnails in a gridview async, because other way it take too long to show!

当我以正常方式进行操作时,它显示的图像很好.(代码和图片)

When I do it the normal way, it shows the images fine. (Code and image)

实用工具

public static Bitmap getThumbnail(Context context, Bitmap bitmap, int columns){
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        int scale = size.x / columns;
        Bitmap bit = ThumbnailUtils.extractThumbnail(bitmap, scale, scale);
        return bit;
    }

适配器

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.adapter_item, null);
        }
        else{
            view = convertView;
        }
        ImageView image = (ImageView)view.findViewById(R.id.img_item);
        image.setImageBitmap(Utilities.getThumbnail(context, BitmapFactory.decodeFile(((Item) this.collection.get(position)).getSrc()), 5));
        return view;
    }

这是 asyncTask 的结果

异步任务

public class ImageGridHandler extends AsyncTask<String, Void, Bitmap>{
    private final WeakReference<ImageView> imageViewReference;
    private Context context;

    public ImageGridHandler(Context context, ImageView img){
        imageViewReference = new WeakReference<ImageView>(img);
        this.context = context;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        return PixzelleUtilities.getThumbnail(this.context, BitmapFactory.decodeFile(params[0]) ,5);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        final ImageView imageView = imageViewReference.get();
        imageView.setImageBitmap(result);
    }
}

适配器

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.adapter_item, null);
        }
        else{
            view = convertView;
        }
        ImageView image = (ImageView)view.findViewById(R.id.img_item);
        image.setImageBitmap(null);
        ImageGridHandler handler = new ImageGridHandler(context, image);
        handler.execute(((Item)this.collection.get(position)).getSrc());
        return view;
    }

图像是黑色的小矩形.

uris 是正确的,因为我可以触摸图像并在弹出窗口中打开!

The uris are correct becaus I can touch the images and open in popup window!

我错过了什么吗?

推荐答案

您需要通知适配器布局已更改,以便可以重新测量视图.然而,这很昂贵,并且每次加载图像时都会导致重新布局.您应该通过为每个 gridview 图块设置固定大小来避免这种情况.目前,我的猜测是您的 R.layout.adapter_item.xml 具有 wrap_content 的高度.将此更改为固定值,它应该可以解决您的问题.

You need to notify your adapter that the layout has changed so the view can be re-measured. This is expensive, however, and will cause a re-layout every time an image loads. You should avoid this by having a fixed size for each of the gridview tiles. Currently, my guess is that your R.layout.adapter_item.xml has wrap_content for it's height. Change this to a fixed value and it should solve your problem.

如果您想降低性能,请通过 notifyDataSetChanged 通知您的适配器发生了更改.

If you want to take a performance hit, notify your adapter that a change has occured with notifyDataSetChanged.

http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()

这篇关于使用异步任务在 gridview 中加载图像,加载不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆