关闭

关于getView中图片重复加载

标签: scrollview图片加载onMeasure
217人阅读 评论(0) 收藏 举报
分类:

在ListView或GridView中,经常会出现getView反复调用的问题。究其原因,是视图进行绘制前多次onMeasure的结果。
要解决这个问题,就要避免父视图反复测量以适应整个布局,尽量避免使用wrap_content属性。

普通的getView反复调用问题并不会有太大的影响,会产生影响的往往是在getView中实行的操作,比如在getView中加载图片之类的。

例如,我要在ScrollView中嵌入一个GridView中,然后在GridView显示图片,为了避免一些奇怪的问题产生(比如滑动冲突或者干脆没法滑动),就要覆写onMeasure方法使其自适应:

public class AlbumGridView extends GridView {

    public AlbumGridView(Context context) {
        super(context);
    }

    public AlbumGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlbumGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height =MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }
}

我将AlbumGridView放入ScrollView中,然后用AlbumGridView加载图片,得到以下Log:

05-31 08:21:56.729 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.729 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.730 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.730 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (3).jpg
05-31 08:21:56.731 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (2).jpg
05-31 08:21:56.734 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (1).jpg
05-31 08:21:56.800 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.800 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.817 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.817 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.850 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.851 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.878 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:21:56.879 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg

可以看到,getView在重复调用,且第一张图片img (4).jpg更是反复调用了11次。总共4张图片,调用了14次,试验多次,第一张图片总是会反复调用多次——单纯调用getView影响并不大,但因为是图片加载,要在getView中根据uri加载图片,这样问题就大了。
于是改写AlbumGridView并在getView中判断是否在测量中,如果在测量中,就先不加载图片,否则就开始加载图片,这样一来会节省很多资源:

public class AlbumGridView extends GridView {
    public boolean isOnMeasure;

    public AlbumGridView(Context context) {
        super(context);
    }

    public AlbumGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AlbumGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        isOnMeasure = true;
        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        isOnMeasure = false;
        super.onLayout(changed, l, t, r, b);
    }

    public boolean isOnMeasure() {
        return isOnMeasure;
    }

加一个isOnMeasure标记以判断视图当前状态;

public class PhotosAdapter extends BaseAdapter {

    ......

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = layoutInflater.inflate(R.layout.item, parent, false);//inflate item
        } else {
            view = convertView;
        }

        if (parent instanceof AlbumGridView) {
            if (((AlbumGridView) parent).isOnMeasure()) {
                return view;
            } else {
                Log.w("load",""+url);
                //加载图片
            }
        }
        return view;
    }

}

这样一来,getView其实还是反复在调用,但是图片加载却不会再反复加载了。其Log如下:

05-31 08:40:54.750 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (4).jpg
05-31 08:40:54.751 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (3).jpg
05-31 08:40:54.751 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (2).jpg
05-31 08:40:54.752 1689-1689/com.xter.picbrowser W/load: file:///storage/emulated/0/Download/img/img (1).jpg
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5202次
    • 积分:148
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类