我正在尝试在gridview异步中加载thumnails,因为其他方式显示它需要很长时间!
当我以正常方式进行时,它会显示图像正常。 (代码和图像)
的Utils
public static Bitmap getThumbnail(Context context, Bitmap bitmap, int columns){
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int scale = size.x / columns;
Bitmap bit = ThumbnailUtils.extractThumbnail(bitmap, scale, scale);
return bit;
}
适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.adapter_item, null);
}
else{
view = convertView;
}
ImageView image = (ImageView)view.findViewById(R.id.img_item);
image.setImageBitmap(Utilities.getThumbnail(context, BitmapFactory.decodeFile(((Item) this.collection.get(position)).getSrc()), 5));
return view;
}
这是asyncTask的结果
的AsyncTask
public class ImageGridHandler extends AsyncTask<String, Void, Bitmap>{
private final WeakReference<ImageView> imageViewReference;
private Context context;
public ImageGridHandler(Context context, ImageView img){
imageViewReference = new WeakReference<ImageView>(img);
this.context = context;
}
@Override
protected Bitmap doInBackground(String... params) {
return PixzelleUtilities.getThumbnail(this.context, BitmapFactory.decodeFile(params[0]) ,5);
}
@Override
protected void onPostExecute(Bitmap result) {
final ImageView imageView = imageViewReference.get();
imageView.setImageBitmap(result);
}
}
适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.adapter_item, null);
}
else{
view = convertView;
}
ImageView image = (ImageView)view.findViewById(R.id.img_item);
image.setImageBitmap(null);
ImageGridHandler handler = new ImageGridHandler(context, image);
handler.execute(((Item)this.collection.get(position)).getSrc());
return view;
}
图像是小黑色矩形。
uris是正确的,因为我可以触摸图像并在弹出窗口中打开!
我错过了什么吗?
答案 0 :(得分:1)
您需要通知适配器布局已更改,以便可以重新测量视图。然而,这是昂贵的,并且每次加载图像时都会导致重新布局。您应该通过为每个gridview切片设置固定大小来避免这种情况。目前,我的猜测是你的R.layout.adapter_item.xml的高度为wrap_content。将其更改为固定值,它应该可以解决您的问题。
如果您想要降低性能,请通过notifyDataSetChanged通知您的适配器发生了更改。
http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()