我的应用程序我有一个ListView,它有2个TextViews和2个ImageViews,我注意到当我滚动它时,它并不完全平滑,我已经四处搜索并注意到我需要一个后台工作线程我需要在listView中渲染我的ImageViews。我试图实现它,但我不能,因为我从我的线程内部引用了viewHolder类,所以持有者需要是最终的,但这不可能发生,所以可能我做错了。有没有人有关于如何实现这一点的任何想法?提前谢谢
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MonumentHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new MonumentHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imageView3);
holder.txtTitle = (TextView)row.findViewById(R.id.textView1);
holder.marker = (ImageView)row.findViewById(R.id.imageView2);
holder.descr = (TextView)row.findViewById(R.id.textView2);
row.setTag(holder);
}
else{
holder = (MonumentHolder)row.getTag();
}
Monument monument = monuments.get(position);
holder.txtTitle.setText(monument.getTitle());
if(monument.getTitleOfCategory().equals("Βυζαντινά Μνημεία"))
holder.marker.setImageResource(R.drawable.memorial);
else if(monument.getTitleOfCategory().equals("Αρχαιολογικά Μνημεία"))
holder.marker.setImageResource(R.drawable.ar);
else if(monument.getTitleOfCategory().equals("Ιστορικά"))
holder.marker.setImageResource(R.drawable.hist);
else
holder.marker.setImageResource(R.drawable.museum);
localPhotosUrl=monument.getLocalPhotosUrl();
holder.imgIcon.setImageBitmap(null);
if(!localPhotosUrl.isEmpty()){
File picToLoad = new File(localPhotosUrl.get(0));
byte[] lala = null;
try {
lala = org.apache.commons.io.FileUtils.readFileToByteArray(picToLoad);
s(lala.length);
System.gc();
Bitmap bitmap = BitmapFactory.decodeByteArray(lala, 0, lala.length);
holder.imgIcon.setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
holder.descr.setText(monument.getContent());
return row;
}
答案 0 :(得分:1)
我建议您使用Picasso进行图像加载/处理缓存并在imageview上渲染。一旦你的工作区中有了jar,你就需要一行代码。
而不是:
holder.imgIcon.setImageBitmap(null);
if(!localPhotosUrl.isEmpty()){
File picToLoad = new File(localPhotosUrl.get(0));
byte[] lala = null;
try {
lala = org.apache.commons.io.FileUtils.readFileToByteArray(picToLoad);
s(lala.length);
System.gc();
Bitmap bitmap = BitmapFactory.decodeByteArray(lala, 0, lala.length);
holder.imgIcon.setImageBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你可以用
替换它Picasso.with(context).load(localPhotosUrl).into(holder.imgIcon);
答案 1 :(得分:0)
您正在寻找的是图像的“延迟加载”。在GetView中加载图像会降低速度。图像加载已在一个线程中完成。