I have a GridView
that shows images from a sdcard. I use AsyncTask
in the getview()
method (getview loads the bitmap in AsyncTask) from this tutorial.
GridView
scrolling is fast and loads the images in Gridview
fast and positions it correctly but my problem is:
When I scroll the GridView , items that are not visible on the screen can not be held
.
And if returned to previous position, the items load again with getview()
method.
With the above tutorial when I scroll GridView
all the images positions have changed.
I changed getview()
(if_else
that shows with comment in my code) and resolved it, but it does not save the previous image and when fast scrolling GridView
, at first the default image (ic_launcher
) is displayed and after a while, the right image is loaded.
How to resolve this problem? (hold all images in GridView
without reducing scroll speed)
@Override
public View getView( final int position, View view, ViewGroup parent) {
final ViewHolder holder;
//if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.showitems,parent, false);
holder.myimage=(ImageView) view.findViewById(R.id.imagess);
holder.myimage.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.myimage.setPadding(8, 8, 8, 8);
view.setTag(holder);
// }
/* else {
holder = (ViewHolder) view.getTag();
}*/
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
Bitmap bm = decodeSampledBitmapFromUri(mylist.get(position).getmypath(), 220, 220);
return bm;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
v.myimage.setImageBitmap(result);
}
}
.execute(holder);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String this_id= mylist.get(position).getmyid();
String this_path=mylist.get(position).getmypath();
Intent ss=new Intent(mContext,show.class);
ss.putExtra("id", this_id);
mContext.startActivity(ss);
}
});
return view;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public Bitmap decodeSampledBitmapFromUri_VID(String path, int reqWidth,int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height/ (float) reqHeight);
}
else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}