毕加索运作不正常 [英] Picasso not functioning consistently

查看:127
本文介绍了毕加索运作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,其中有很多图块-几百个.所有数据都是从Web服务收集的.

I have a gridview which has lots of tiles - something like several hundreds. All the data is gathered from a webservice.

每个网格单元都有两个TextView,一个Button和一个ImageView.

Each gridcell has two TextViews, a Button and one ImageView.

我绝对无法使用我编写的原始代码来加载图像,该应用每次给出OOM都会崩溃每次,因此我决定使用Picasso.顺便说一下,我的asynctasks运作良好.

I was absolutely unable to load images using raw code written by me, the app would crash every single time giving OOM, so I decided to use Picasso. My asynctasks, by the way, are working perfectly.

我必须说,实现不仅更简单,而且我的应用程序甚至没有崩溃过一次.

I must say, the implementation is not only simpler, but my app hasnt crashed even once.

但是,毕加索什么时候显示图像,什么时候对我来说似乎不是完全随机-

However, when Picasso will display images and when it won't seems to be completely random to me -

  1. 有时,要花一些时间才能显示当前正在查看的单元格中的图像(按年龄,我指的是30-40秒).

  1. Sometimes, it would take ages to show the images in the cells currently in view (by ages I mean 30-40 secs).

在其他情况下,将立即加载2-3张图像,然后再加载其他图像.我向下滚动并看到10到20个单元格的间隙后会看到一些图像.然后是另一个差距.然后是一些图片.

At other instances, 2-3 images would load instantly and then no other will. I scroll down and see some images after a gap of 10-20 cells. Then another gap. Then some images.

我可以看到它正在缓存图像,但是显然存在一些问题.当我滚动回到我曾经去过的地方时,似乎重新加载"了图像.

I can see it is caching the images, but clearly there is some problem. It seems to "reload" the images when I scroll back to some place where I have already been.

一旦我重新启动仿真器,一切都将正常运行. 完美无瑕.下次不一样.下次我重新启动模拟器时,情况并非如此.

Once I re-started the emulator and everything worked flawlessly. Flawlessly. Not so the next time. And not so the next time I re-started the emulator.

我不会说毕加索是不可靠的,因为更多的机会是我以其他方式弄乱了我的内存管理.但是,如果有的话,我将无法指出.

I won't go so far as to say Picasso is not dependable, for more chances are that I have messed up my memory management in some other way. However, if I have I am unable to point it out.

这是我的适配器类-

package com.tech4i.hujum;

// all imports

@SuppressLint("NewApi")
public class DealsAdapter extends BaseAdapter {

    private Context mContext;
    int width;
    public static List<Deals> deals_list;

    Deals deal;
    DealsViewHolder dvh;

    AbsListView.LayoutParams layoutParams;
    LayoutInflater inflater = null;

    public DealsAdapter(Context context, List<Deals> objects) {

        mContext = context;
        deals_list = objects;

        inflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    }

   // ViewHolder
    class DealsViewHolder {

        ImageView pic;

        TextView details;
        TextView price;

        Button button;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        width = (int) ((MainActivity.width * 0.23 * mContext.getResources()
                .getDisplayMetrics().density) + 0.5);

        layoutParams = new AbsListView.LayoutParams(width, (int) (width *      0.95));

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.gridcell_deals, parent,
                    false);
            convertView.setLayoutParams(layoutParams);

            dvh = new DealsViewHolder();

            dvh.details = (TextView) convertView
                    .findViewById(R.id.deal_details);
            dvh.price = (TextView)  convertView.findViewById(R.id.deal_price);
            dvh.pic = (ImageView) convertView.findViewById(R.id.deal_picture);

            dvh.button = (Button) convertView
                    .findViewById(R.id.deal_shop_now_button);

            convertView.setTag(dvh);

        } else {

            dvh = (DealsViewHolder) convertView.getTag();
        }

        deal = deals_list.get(position);

      Picasso.with(dvh.pic.getContext()).load(deal.getPhoto_link()).noFade()
            .into(dvh.pic);

        dvh.details.setText(deal.getDescription());

        dvh.price.setText(deal.getValue() + " /-");

        return convertView;

    }

}

任何帮助将不胜感激.谢谢!

Any help will be appreciated. Thanks!

推荐答案

您尝试加载的图像似乎很大.因此,您可以尝试将尺寸调整添加到Picasso方法链中:

It seems that images you try to load are huge. So you may try to add resizing to Picasso methods chain:

Picasso.with(dvh.pic.getContext())
       .load(deal.getPhoto_link())
       .noFade()
       .resize(width, height) // <-- ImageView size in cell
       .into(dvh.pic);

它将减少内存消耗.另外,如果您的API允许您获取缩略图而不是完整版本的图像,则最好在网格视图中下载它们.

It will reduce memory consumption. Also if your API allows you to obtain thumbnails instead of full version of images it will preferable to download them in grid view.

这篇关于毕加索运作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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