我正在尝试使用GridView + Picasso \ Universal Image Loader创建一个库,我遇到了一些问题。每次我只加载本地文件(来自外部存储),而不是从网络加载。我制作了两个视频,更好地描述了问题,然后是文字。
我使用自定义适配器扩展BaseAdapter
。
thumbs
是带有图片的File
数组
足够快地加载图像,但滚动速度非常慢。Here the video demonstration on youtube
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Get the view
View view;
if (convertView == null)
view=inflater.inflate(R.layout.gallery_photo_item,parent,false);
else
view = convertView;
//find the componetnts
final ImageView image_view = (ImageView)view.findViewById(R.id.galley_photo_item_image);
final CheckBox check = (CheckBox)view.findViewById(R.id.galley_photo_item_checkbox);
//Toggling checkbox if DELETE MODE
if(is_checking) {
check.setChecked(false);
check.setVisibility(View.VISIBLE);
}
else
check.setVisibility(View.INVISIBLE);
view.setTag(position);
check.setTag(position);
//Some event handling
check.setOnClickListener(this);
view.setOnClickListener(this);
//Setting up Picasso
Picasso.Builder builder = new Picasso.Builder(context);
builder.listener(new com.squareup.picasso.Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
Log.e("Picasso","Loading failed: "+exception.getMessage());
image_view.setVisibility(View.GONE);
}
});
//Display image
Picasso pic = builder.build();
pic
.load(thumbs[position])
//.placeholder(R.drawable.ic_launcher)
.fit()
.into(image_view);
return view;
}
快速滚动,但加载速度很慢。此外,当您快速滚动时,您可以看到错误的图像,这些图像将被替换为正确的(当它们已加载时)几秒钟。 Demonstration on youtube
我在这里添加了View Holder
模式。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view_holder;
if(convertView==null)
{
convertView = inflater.inflate(R.layout.gallery_photo_item,parent,false);
ImageView image = (ImageView)convertView.findViewById(R.id.galley_photo_item_image);
CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.galley_photo_item_checkbox);
view_holder = new ViewHolder(convertView,image,checkBox);
convertView.setTag(view_holder);
}
else
view_holder = (ViewHolder)convertView.getTag();
//Toggle checkbox on DELETE MODE
if(is_checking) {
view_holder.CheckBox.setChecked(false);
view_holder.CheckBox.setVisibility(View.VISIBLE);
}
else
view_holder.CheckBox.setVisibility(View.INVISIBLE);
//view.setTag(position);
//check.setTag(position);
//Event handling
view_holder.CheckBox.setOnClickListener(this);
view_holder.View.setOnClickListener(this);
//Display image
String uri = Uri.fromFile(thumbs[position]).toString();
ImageLoader loader = ImageLoader.getInstance();
loader.displayImage(uri,view_holder.ImageView, options);
return convertView;
}
什么是让画廊更快地工作的好方法(作为原生Android应用程序,完美)?
答案 0 :(得分:2)
尝试使用谷歌推荐的Glide Image Loader Library 请参阅此处: - http://coderzpassion.com/android-working-glide-image-loader-library/