• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2644
  • Last Modified:

How to increase Performance of gridview in android.

Hello friends ,
      I am working on android grid view , here I attach the code . Please tell me why my grid view is not scroll smoothly as like default grid view in device. Or if any other way to implement smooth image gallery then please help me.

Thank you.
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridView=(GridView)findViewById(R.id.gridView);
        String selection = MediaStore.Images.Thumbnails.KIND + "="  + // Select only mini's
        MediaStore.Images.Thumbnails.MINI_KIND;

        Cursor cursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, selection, null, null);
       
        cursor.moveToFirst();
        gridAdapter gAdapter=new gridAdapter(getApplicationContext(), cursor);
        gridView.setAdapter(gAdapter);
    }
    
    class gridAdapter extends BaseAdapter{
    	Context context;
    	Cursor cursor;
    	LayoutInflater inflater;
    	public gridAdapter(Context context,Cursor cursor){
    		this.context=context;
    		this.cursor=cursor;
    		inflater=LayoutInflater.from(context);
    	}
    	
		@Override
		public int getCount() {
			return cursor.getCount();
		}

		@Override
		public Object getItem(int arg0) {
			return cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView img;
			cursor.moveToPosition(position);
			String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
			if(convertView==null){
				img=new ImageView(context);
				img.setLayoutParams(new GridView.LayoutParams(90, 90));

				convertView=img;
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
			}else{
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
			}
			return convertView;
		}
    	
    }
}


       main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<GridView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/gridView"
    android:numColumns="auto_fit"
	android:columnWidth="90dip"
	android:horizontalSpacing="0dip"></GridView>
</LinearLayout>

Open in new window

0
jbajaj
Asked:
jbajaj
1 Solution
 
Dejan PažinCommented:

Change your getView method to print the time of execution for each of possible bottle necks. Here is an example of how you can do that.

Run the application with these print outs and post the result, so we can see where is the bottleneck.


public View getView(int position, View convertView, ViewGroup parent) {
			ImageView img;                       
                        long start = System.currentTimeMillis();
			cursor.moveToPosition(position);
                        long end = System.currentTimeMillis();
                        System.out.println("Time 1 = " + (end - start));

                        start = System.currentTimeMillis();
			String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
                        end = System.currentTimeMillis();
                        System.out.println("Time 2 = " + (end - start));

			if(convertView==null){
                                start = System.currentTimeMillis();
				img=new ImageView(context);
				img.setLayoutParams(new GridView.LayoutParams(90, 90));

				convertView=img;
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
                                 end = System.currentTimeMillis();
                                  System.out.println("Time 3 = " + (end - start));
			}else{
                                start = System.currentTimeMillis();
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
                                end = System.currentTimeMillis();
                        System.out.println("Time 4 = " + (end - start));
			}
			return convertView;
		}

Open in new window

0
 
jbajajAuthor Commented:
I work according to help get from  "dejanpazin" but I am not found any best solution to resolve my problem.

Thank you.
0
 
SanthanaTechnical LeadCommented:
Hi,
 I found some problem in above code. I guess that is affecting the performance of the grid view scrolling.

1) We can avoid creation of unwanted object .
" img.setLayoutParams(new GridView.LayoutParams(90, 90));"  -- In this you are creating GridView.LayoutParams object every time. Instead of that you can create static object or create only one object .
 GridView.LayoutParams mLayoutParam ;
if(null != mLayoutParam){
mLayoutParam = new GridView.LayoutParams(90, 90));
}
This will be effective. Because in your grid view if you have 10 visible item it will create 10 object. Through this we can avoid creation of 9 objects.

2) http://stackoverflow.com/questions/3720530/why-would-imageview-setimageuri-work-in-android-2-2-but-not-2-1

You are loading images from local location in getView().
" ((ImageView)convertView).setImageURI(Uri.parse(pathStr));".
 I guess this is big blunder. It is the main cause of grid view scrolling performance.
Check the above link. You can try the simillar way. Image loading we can do it in the backgroudn thread...or Through thread pool create limited size thread pool...and load photos for the backgroudn thread....It will increase the performance of grid view scrolling.


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now