我的画廊中有大约 200 多张图片。
我可以使用 Universal Image Loader 获取这些图像。
但是当将所有图像加载到我的应用程序时,速度太慢了。
与我的常规画廊不同。
我的手机是三星 Galaxy s5(棒棒糖)。
这是代码。
package com.thesis.juandirection.juandirectionfinale.fragments;
/**
* Created by Galvez on 11/5/2015.
*/
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import java.util.ArrayList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.thesis.juandirection.juandirectionfinale.Holder.GlobalVar;
import com.thesis.juandirection.juandirectionfinale.R;
import com.thesis.juandirection.juandirectionfinale.Constants;
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
listView = (GridView) rootView.findViewById(R.id.grid);
((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
private static class ImageAdapter extends BaseAdapter {
ArrayList<String> IMAGE_URLS = new ArrayList<>();
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context) {
inflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.abc_ab_share_pack_mtrl_alpha)
//.showImageForEmptyUri(R.drawable.abc_ab_share_pack_mtrl_alpha)
//.showImageOnFail(R.drawable.abc_ab_share_pack_mtrl_alpha)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
@Override
public int getCount() {
return Constants.IMAGES.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
IMAGE_URLS = (ArrayList<String>) Constants.IMAGES.clone();
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.imageView.setId(position);
holder.checkBox = (CheckBox) view.findViewById(R.id.cbImage);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
if (GlobalVar.getPosition.size() > 0) {
Constants.IMAGES_SELECTED.clear();
for (int x = 0; x < GlobalVar.getPosition.size(); x++) {
if (position == GlobalVar.getPosition.get(x)) {
holder.checkBox.setChecked(true);
}
}
}
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!holder.checkBox.isChecked()) {
holder.checkBox.setChecked(true);
GlobalVar.getPosition.add(position);
} else {
GlobalVar.getPosition.remove(GlobalVar.getPosition.indexOf(position));
holder.checkBox.setChecked(false);
}
}
});
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(IMAGE_URLS.get(position), holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
CheckBox checkBox;
ProgressBar progressBar;
}
}
有时它会在加载图像时使应用程序崩溃。