Solved

Scroll of Grid View in android

Posted on 2011-03-03
7
6,130 Views
Last Modified: 2012-05-11
Hello friend,

I am working on grid view for displaying an Media store images, In grid view images will be displayed but the scroll is not as smooth as device gallery grid . So please explain me how I  increase the performance of grid in my code, here I attach my code.

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
  • 2
7 Comments
 
LVL 17

Accepted Solution

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

You are already using convertView, which is there for performance reasons.


In order to see where is the bottleneck you should add some output int the getView method - so you can see which part of the code takes up too much time. Since the method is very short, you could measure time for practicaly each sentence in the method.

Next thing you can do is test the application on a real device, to see how it performs. Or have you already done that? Performance on a emulator is drasticaly slower.
0
 

Author Comment

by:jbajaj
ID: 35026863
Thank you dejanpazin.

I immediately check time to execute  each line in code by adding some output.
But is there any other faster way to see all the images from gallery. Because I also test it on device but the scroll is not as smooth as default gallery in device.
If any other way to do same thing then please help me.

Thank You.
0
 
LVL 17

Assisted Solution

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

You have to find the bottleneck - the point where it takes too long to execute the code. After that, you can improve that piece of code.

Usually this things are solved by caching images, but then you have to take care of the memory.
0
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.

 

Author Comment

by:jbajaj
ID: 35027419
Thank you dejanpazin,

I will try to find  bottleneck of my code and then improve it, and also try to caching images if necessary.

Thank you for help.
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35058973

I guess this question is requested to be closed by a mistake - the author stated the reason as 'excellent'.
0
 

Expert Comment

by:_alias99
ID: 35120694
All,
 
Following an 'Objection' by dejanpazin (at http://www.experts-exchange.com/Q_26868664.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
_alias99
Community Support Moderator
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

Suggested Solutions

Title # Comments Views Activity
Blackberry Hub on Android 4 75
Wireless Headphone with mike for web conference 4 62
app permissions on phone 2 39
Protecting a SKY 4.0 (Android) devise 15 101
Recently Amazon announced their entry in the tablet device called the Amazon Kindle Fire. Some of you might have used an Ipad or have considered buying one. While they're similar in form and function in that they both have color displays with multit…
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

910 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

22 Experts available now in Live!

Get 1:1 Help Now