Solved

How to increase Performance of gridview in android.

Posted on 2011-03-15
3
2,523 Views
Last Modified: 2013-11-13
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
Comment
Question by:jbajaj
3 Comments
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 500 total points
ID: 35146024

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
 

Author Closing Comment

by:jbajaj
ID: 35432141
I work according to help get from  "dejanpazin" but I am not found any best solution to resolve my problem.

Thank you.
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 36045221
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now