gpt4 book ai didi

android - ListView 很慢-android

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:05:07 26 4
gpt4 key购买 nike

我想在我的 ListView 中显示资源目录中的图像(所有图像都以 png 格式保存),但它非常慢。我是一个完全的菜鸟,所以我需要有关异步任务的帮助以消除延迟。

列表适配器:

   public class MyListAdapter extends ArrayAdapter < SettingsItem > {

Context context;
List < SettingsItem > items;
public MyListAdapter(Context context, int resource, List < SettingsItem > objects) {
super(context, resource, objects);
this.context = context;
items = objects;
}

@Override
public View getView(int position, View itemView, ViewGroup parent) {

ViewHolder holder;
if (itemView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
itemView = inflater.inflate(R.layout.settings_items, parent, false);

holder = new ViewHolder();
holder.imageView = (ImageView) itemView.findViewById(R.id.item_icon);
holder.textView = (TextView) itemView.findViewById(R.id.item_text);

itemView.setTag(holder);
} else {
holder = (ViewHolder) itemView.getTag();
}

SettingsItem settingsItem = items.get(position);

if (settingsItem != null) {
holder.textView.setText(settingsItem.getName());
new ImageDownloaderTask(holder.imageView, getContext(), settingsItem.getIcon_id()).execute();
}
return itemView;
}
static class ViewHolder {
ImageView imageView;
TextView textView;
}
}

行类:

public class SettingsItem{
private String name;
private int icon_id;
public SettingsItem(String a, int b){

super();
name=a;
icon_id=b;
}

public String getName() {
return name;
}

public int getIcon_id() {
return icon_id;
}
}

异步任务:

class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
Context context;
int id;
public ImageDownloaderTask(ImageView imageView, Context context, int id) {
imageViewReference = new WeakReference<ImageView>(imageView);
this.context=context;
this.id=id;
}

@Override
protected Bitmap doInBackground(String... params) {
return BitmapFactory.decodeResource(context.getResources(),id );
}

@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}

if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageResource(R.drawable.medical_bag);
}
}
}
}
}

我真的需要一些帮助。
提前致谢。 :)

最佳答案

我建议您使用 Glide库,这将为您的应用程序中的图像加载带来性能,并且非常易于使用。它还支持获取、解码和显示视频静止图像、图像和动画 GIF。

它使滚动任何类型的图像列表尽可能流畅和快速,但 Glide 对于几乎任何需要获取、调整大小和显示远程图像的情况也是有效的。

Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

将 Glide 添加到您的项目中:

dependencies {
compile 'com.github.bumptech.glide:glide:3.6.0'
compile 'com.android.support:support-v4:19.1.0'
}

官方页面的listview用法如下:

@Override
public View getView(int position, View recycled, ViewGroup container) {
final ImageView myImageView;
if (recycled == null) {
myImageView = (ImageView) inflater.inflate(R.layout.my_image_view,
container, false);
} else {
myImageView = (ImageView) recycled;
}

String url = myUrls.get(position);

Glide.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.crossFade()
.into(myImageView);

return myImageView;
}

作为引用,另一个项目使用这个库来实现更快的图像加载和更好的滚动:https://github.com/chrisbanes/cheesesquare

关于android - ListView 很慢-android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30953149/

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