加载图像在GridView与异步任务,没有正确加载 [英] Loading images in a gridview with async task, not loading correctly

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

问题描述

我试图加载thumnails在GridView异步的,因为其他的方式,它需要很长时间才能显示!

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

当我这样做是正常的方式,它显示出细腻的图像。 (code和图像)

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

utils的

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的

And this is the result with the asyncTask

的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;
    }

该图像是小黑色矩形。

The images are little black rectangles.

URI正确becaus我能触摸到的图像,并在弹出的窗口中打开!

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天全站免登陆