【问题标题】:ListView with Thumbnails not scrolling smooth带有缩略图的 ListView 滚动不流畅
【发布时间】:2013-12-17 04:31:15
【问题描述】:

我有一个带有缩略图的列表视图。我从一个网址获取缩略图。下载缩略图时,我会显示一个微调器。

问题
在列表视图开始时,几乎同时所有的微调器都被下载的图像替换。微调器冻结,列表视图冻结 0.5 秒。

当我以明显的速度滚动列表视图时,也会发生同样的情况。

问题
我怎样才能加快我的列表视图,并使其再次平滑?我应该在后台执行一些任务,还是下载并一张一张地设置图像。还是别的什么?

获取视图

 public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){

        convertView = mInflater.inflate(R.layout.listview_row, null);
    }

    UrlImageView thumb = (UrlImageView) convertView.findViewById(R.id.thumbnail);

    final Video video = videos.get(position);
    // Set the image for the list item
    thumb.setImageDrawable(video.getThumbUrl());    
    return convertView;
}

我还注意到,如果我使用 if(convertView == null){ 来回收列表项,有时会显示错误的缩略图。

UrlImageView

package com.package;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;

import imports;

public class UrlImageView extends LinearLayout {

private Context mContext;
private Drawable mDrawable;
private ProgressBar mSpinner;
private ImageView mImage;

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

public UrlImageView(Context context) {
    super(context);
    init(context);
}

private void init(final Context context) {
    mContext = context;

    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());

    mImage = new ImageView(mContext);
    mImage.setLayoutParams(new LayoutParams(width,height));
    mImage.setScaleType(ScaleType.CENTER_CROP);
    mImage.setVisibility(View.VISIBLE);

    mSpinner = new ProgressBar(mContext);
    mSpinner.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    mSpinner.setIndeterminate(true);

    addView(mSpinner);
    addView(mImage);
}

/**
 * Set's the view's drawable, this uses the internet to retrieve the image
 * don't forget to add the correct permissions to your manifest
 * 
 * @param imageUrl the url of the image you wish to load
 */
public void setImageDrawable (final String imageUrl) {
    mDrawable = null;
    mSpinner.setVisibility(View.VISIBLE);
    mImage.setVisibility(View.VISIBLE);
    new Thread() {
        public void run() {
            try {
                mDrawable = getDrawableFromUrl(imageUrl);
                imageLoadedHandler.sendEmptyMessage(RESULT_OK);
            } catch (MalformedURLException e) {
                imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
            } catch (IOException e) {
                imageLoadedHandler.sendEmptyMessage(RESULT_CANCELED);
            }
        };
    }.start();
}

/**
 * Callback that is received once the image has been downloaded
 */
private final Handler imageLoadedHandler = new Handler(new Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
        case RESULT_OK:
            mImage.setImageDrawable(mDrawable);
            mImage.setVisibility(View.VISIBLE);

            mSpinner.setVisibility(View.GONE);
            break;
        case RESULT_CANCELED:
        default:
            // possible fail image
            break;
        }
        return true;
    }
});

private static Drawable getDrawableFromUrl (final String url) throws IOException, MalformedURLException {

    return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()), "name");
}

}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    查看此官方博客文章以获取一些提示:

    http://developer.android.com/training/improving-layouts/smooth-scrolling.html

    【讨论】:

    • 谢谢,我之前注意到了这篇文章,虽然我在将它应用到我的应用程序时遇到了一些困难。如果你能帮助我开始,那就太棒了!
    • 你能具体说明什么是困难的吗?可以从 ViewHolder 入手,比较简单。实际上看起来您的图像加载已经在后台执行了。
    • 我已经实现了viewholder,但我没有注意到任何区别。 - 我想知道如果我在后台执行所有与图像相关的任务,为什么我的列表视图运行不顺畅。我也在使用最新的硬件(Nexus 5),“unsmoothless”的来源应该在 UrlImageView.java
    • 也许您可以尝试为您的图像添加缓存。还有一些现成的库可以帮助您在 Android 中加载图像。例如github.com/nostra13/Android-Universal-Image-Loader
    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多