gpt4 book ai didi

android:缓存可绘制元素以提高性能

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:24 24 4
gpt4 key购买 nike

我正在尝试缓存包含在 ListView 中的 Drawable 对象:

我在适配器中有以下内容:

private final ImageThreadloader imageloader=new ImageThreadloader();

public View getView(int position, View convertView, ViewGroup parent)
{
View row=convertView;
ViewWrapper wrapper=null;
Activity activity=(Activity)getContext();

RssItem item=getItem(position);

if (row == null) {

LayoutInflater inflater=activity.getLayoutInflater();
row=inflater.inflate(R.layout.row,null);
wrapper=new ViewWrapper(row);
row.setTag(wrapper);
}
else
{
wrapper=(ViewWrapper)row.getTag();
}

wrapper.getTitle().setText(item.getTitle());
String cleaned=item.getDescription().replaceAll("\\<.*?\\>", "");
int Long=cleaned.length();
if (Long<=100)
{
wrapper.getDescription().setText(cleaned);
}
else wrapper.getDescription().setText(cleaned.substring(0, 50)+"...");

String laurl=item.getImageUrl();

if (laurl!="")
{
imageloader.loadImage(laurl, (ImageView)wrapper.getImage());
}
else
{
wrapper.getImage().setImageResource(R.drawable.icon);
}

return row;

}

static class ViewWrapper {

private View base;
private TextView title=null;
private TextView description=null;
private ImageView icono=null;

ViewWrapper (View base) {
this.base=base;
}

public TextView getTitle() {
if (title==null) {
title=(TextView)base.findViewById(R.id.TitleText);
}
return title;
}

public TextView getDescription() {
if (description==null) {
description=(TextView)base.findViewById(R.id.DescriptionText);
}
return description;
}

public ImageView getImage() {
if (icono==null) {
icono=(ImageView)base.findViewById(R.id.ImageIcon);
}
return icono;
}

}

ImageThreadLoad类如下:

public class ImageThreadloader  {


public void loadImage(String imageurl,ImageView imageview)
{
DrawableDownLoaderTask imagetask= new DrawableDownLoaderTask(imageview);
imagetask.execute(imageurl);
}


public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
try {
inputStream = new URL(url).openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Drawable.createFromStream(inputStream, "src");
}

class DrawableDownLoaderTask extends AsyncTask<String, Void, Drawable> {

private String url;
private final WeakReference<ImageView> imageviewreference;

public DrawableDownLoaderTask(ImageView imageview) {
imageviewreference=new WeakReference<ImageView>(imageview);
}

@Override
protected Drawable doInBackground(String... params)
{
url=params[0];
Drawable drawable=loadImageFromUrl(url);
return drawable;

}

@Override
protected void onPostExecute(Drawable drawable)
{

if (isCancelled())
{
drawable=null;
}

if (imageviewreference!=null)
{
ImageView imageview=imageviewreference.get();
if (imageview!=null)
{
imageview.setImageDrawable(drawable);
}
}



}
}
}

工作完美,但如果我快速滚动 ListView ,它会有一些延迟。我已经阅读了几个教程,解决方案总是相同的:缓存。

所以我尝试了以下方法(更改在单独线程中加载图像的类):

private final HashMap <String, SoftReference <Drawable>> imagecache= new HashMap <String, SoftReference<Drawable>>();

public void loadImage(String imageurl,ImageView imageview)
{
if (imagecache.containsKey(imageurl))
{
SoftReference<Drawable> drawable= imagecache.get(imageurl);
imageview.setImageDrawable(drawable.get());
}
else
{
DrawableDownLoaderTask imagetask= new DrawableDownLoaderTask(imageview);
imagetask.execute(imageurl);
}

}
class DrawableDownLoaderTask extends AsyncTask<String, Void, Drawable> {

private String url;
private final WeakReference<ImageView> imageviewreference;

public DrawableDownLoaderTask(ImageView imageview) {
imageviewreference=new WeakReference<ImageView>(imageview);
}

@Override
protected Drawable doInBackground(String... params)
{
url=params[0];
Drawable drawable=loadImageFromUrl(url);
if (drawable!=null) imagecache.put(url, new SoftReference<Drawable>(drawable));
return drawable;

}

@Override
protected void onPostExecute(Drawable drawable)
{
ImageView imageview=imageviewreference.get();
imageview.setImageDrawable(drawable);
}
}

每个似乎都工作正常,但是当我滚动图像时,某些行中的图像开始消失。有问题?有什么工具可以帮助我解决这个问题吗?

提前致谢

最佳答案

首先,让我们看一下这段代码。

SoftReference<Drawable> drawable= imagecache.get(imageurl);
imageview.setImageDrawable(drawable.get());

SoftReference 的要点是系统可以收集包含的引用。在这种情况下,drawable.get() 将返回 null,您的图像将消失。

另请查看 ignition - 无论是为了灵感还是为了使用。

关于android:缓存可绘制元素以提高性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068284/

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