Solved

Scroll of Grid View in android

Posted on 2011-03-03
7
6,158 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 365 setup on Android phone 6 80
app permissions on phone 2 61
Samsung TAB S upgrade - just showing SAMSUNG 5 33
tablet  Brightness & contrast level in sunlight. 3 26
I tend toward trying the newest hardware and software.  Thiss sometimes works out to my benefit, and sometimes not.  Because I downloaded and installed Android 5.x (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.htm…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
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.

821 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