darshan
darshan

Reputation: 4569

GridView scroll not smooth

I am using Gridview in my App to show Images from a folder on Sdcard... My issue is the Scrolling in the GridView, it is not as smooth as the Gallery App. Here's the Adapter code -

public class GridViewAdapter extends BaseAdapter {

    // Declare variables
    private Activity activity;
    private String[] filepath;
    private String[] filename;

    private static LayoutInflater inflater = null;

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
        activity = a;
        filepath = fpath;
        filename = fname;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return filepath.length;

    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)

            vi = inflater.inflate(R.layout.gridview_item, null);

        // Locate the ImageView in gridview_item.xml
        ImageView img = (ImageView) vi.findViewById(R.id.image);


        // Set file name to the TextView followed by the position
        TextView txt = (TextView) vi.findViewById(R.id.name);

        // Decode the filepath with BitmapFactory followed by the position
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);

        // Set the decoded bitmap into ImageView
        img.setImageBitmap(bmp);
       txt.setText(filename[position]);
        return vi;
    }
}

How to solve this Issue and make the scrolling smooth?

Upvotes: 0

Views: 1202

Answers (3)

Ankur1994a
Ankur1994a

Reputation: 2122

BitmapFactory.decodeFile is heavy operation to perform on ui thread again and again. you have use some cache library.

You can use some image loading library like Universal image loader , Glide or picasso .

Please try this using a holder.

class Holder{
ImageView img ;
TextView txt;
}


public View getView(int position, View convertView, ViewGroup parent) {      
    Holder holder;
    if (convertView == null)
 {
     convertView = inflater.inflate(R.layout.gridview_item, null);
       holder=new Holder();

    holder.img = (ImageView) convertView.findViewById(R.id.image);

   holder.txt = (TextView) convertView.findViewById(R.id.name);

     convertView.setTag(holder)
    }
  else
{
  holder=(Holder)convertView.getTag();
 }


   Picasso.with(activity) .load(filepath[position]) .into(holder.img);
   txt.setText(filename[position]);
    return convertView;
}

Upvotes: 1

Gopal
Gopal

Reputation: 1784

You can do this.

  1. Implemented Asynchronous image loading so that every image get loaded asynchronously
  2. Make use of third party ImageLoaders and load images in Thumbnails size. For.ex UniversalImageLoader. check this more details
  3. You can make use of RecyclerView with GridLayoutManager instead of GridView, it is more optimized control. for ex:http://www.android-examples.com/android-recyclerview-with-gridview-gridlayoutmanager/

additional reference:http://www.technotalkative.com/android-select-multiple-photos-from-gallery/

Upvotes: 1

Emerson Dallagnol
Emerson Dallagnol

Reputation: 1269

You need to execute BitmapFactory.decodeFile in a separate thread, like this tutorial: https://developer.android.com/training/displaying-bitmaps/process-bitmap.html

I suggest you use a cache, to not reload the images every time the user scroll: https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Upvotes: 1

Related Questions