gpt4 book ai didi

android - 带有缩略图的 ListView 滚动不流畅

转载 作者:行者123 更新时间:2023-11-30 03:14:46 26 4
gpt4 key购买 nike

我有一个带有缩略图的 ListView 。我正在从 url 获取缩略图。下载缩略图时,我会显示一个微调器。

问题
在 ListView 开始时,所有微调器几乎同时被下载的图像替换。微调器卡住, ListView 卡住 0.5 秒。

当我以明显的速度滚动浏览我的 ListView 时,会发生同样的情况。

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

获取 View

 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");
}

}

最佳答案

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

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

关于android - 带有缩略图的 ListView 滚动不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20311817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com