Solved

How to increase Performance of gridview in android.

Posted on 2011-03-15
3
2,577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Santhana
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

695 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