问题
I am trying to load images in GridView
from assets folder with following adapter however gridview scrolling in not smooth. Can someone suggest how can I improve my this code?
And one more thing I am getting memory errors on some devices due to this. How can I optimize this program?
public class GridViewAdapter extends BaseAdapter {
Context mContext;
public GridViewAdapter(Context mContext){
this.mContext = mContext;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater1 = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater1.inflate(R.layout.gv_item, null);
holder.imageView = (GridImageView) convertView.findViewById(R.id.imageView);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.imageView.setImageBitmap(Helper.getBitmapFromAssets((position+1)+"_thum", mContext));
return convertView;
}
private static class Holder {
GridImageView imageView;
}
@Override
public int getCount() {
return Constants.TOTAL_IMAGES;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
}
And my Helper.getBitmapFromAssets
is following.
/**
* Reads Images from Assets Folder
* @param fileName
* @return
*/
public static Bitmap getBitmapFromAssets(String fileID, Context mContext) {
AssetManager assetManager = mContext.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open("images/"+fileID+".encrypted");
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
回答1:
- Create ArrayList contain path of asset images (e.g FolderName/1.png)
AssetManager assetManager = getResources().getAssets();
ArrayList listpath;
try {
String[] files = assetManager.list("foldername");
listpath==new ArrayList<String> ();
for (String strImageName : files) {
pathAssets = "foldername" + File.separator+ strImageName;
listpath.add(pathAssets );
}
- Call Adapter
try {
if (listBitmap!= null) {
adp = new GridViewAdapter(this,R.layout.grid_item,listBitmap);
gridView.setAdapter(adp);
}
} catch (Exception e) {
e.printStackTrace();
}
Adapter code (Load Images using UniversalImageLoder ) Download jar file from here
import java.io.File;
import java.util.ArrayList;
import android.app.Service;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class GridViewAdapter extends BaseAdapter {
Context context;
int layoutResourceId;
private ArrayList<String> griRowItems;
LayoutInflater vi;
ImageLoader imageLoader;
String[] imageUrls;
ArrayList<String> imagePath;
DisplayImageOptions options;
ViewHolder holder = null;
public GridViewAdapter(Context context,
int layoutResourceId, ArrayList<String> mApps) {
this.context = context;
this.griRowItems = mApps;
this.layoutResourceId = layoutResourceId;
vi = (LayoutInflater) context
.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
imageLoader = ImageLoader.getInstance();
this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = vi.inflate(R.layout.grid_item, parent,
false);
holder = new ViewHolder();
}
holder.imageView = (ImageView) convertView.findViewById(R.id.ivImage);
// holder.progressBar = (ProgressBar) convertView
// .findViewById(R.id.progress);
String tempStr = "assets://" + griRowItems.get(position);
lazyLoading(imageLoader, tempStr, holder.imageView,
options);
return convertView;
}
@Override
public int getCount() {
return griRowItems.size();
}
@Override
public Object getItem(int position) {
return griRowItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
ImageView imageView;
// ProgressBar progressBar;
TextView tvTitle;
// DisplayImageOptions options;
}
}
put this method inside adapter for load images smoothly
public static void lazyLoading(ImageLoader imageLoader, String tempStr, ImageView imageView,DisplayImageOptions options) {
imageLoader.displayImage(tempStr, imageView, options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view,Bitmap loadedImage) {
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view,int current, int total) {
}
});
}
来源:https://stackoverflow.com/questions/21515022/loading-images-from-assets-to-gridview-with-smooth-scrolling