0

I am implementing a grid view of images which is opened using a button. The issue I am facing is regarding lagging/time delay for opening the gridview. Here is some of code snippets

    import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class Gallery extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        ActionBar ab=getActionBar();
        Resources r=getResources();
        Drawable d=r.getDrawable(R.color.quotescolor);
        ab.setBackgroundDrawable(d);
        GridView gv=(GridView)findViewById(R.id.gridview);
        gv.setAdapter(new ImageAdapter2(this));
        final FragmentManager fm=getFragmentManager();
        gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                if(position==0)
                {
                    ImageDial1 id1=new ImageDial1();
                    id1.show(fm,"image_title");
                    //Intent i=new Intent(Gallery.this,ImageFrag1.class);
                    //startActivity(i);
                }

and for the images i am using an adapter like this

package com.example.sachinapp;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter2 extends BaseAdapter {

    private Context ctx;

    public ImageAdapter2(Context c)
    {
        ctx=c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return pics.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView iv;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            iv = new ImageView(ctx);
            iv.setLayoutParams(new GridView.LayoutParams(150,150));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setPadding(8, 8, 8, 8);
        } else {
            iv = (ImageView) convertView;
        }

        iv.setImageResource(pics[position]);
        return iv;

    }

    private Integer[] pics={
            R.drawable.s1,R.drawable.s2,
            R.drawable.s3,R.drawable.s4,
            R.drawable.s6,R.drawable.s7,
            R.drawable.s8,R.drawable.s9,
            R.drawable.s10,R.drawable.s11,
            R.drawable.s2,R.drawable.s13,
            R.drawable.s4,R.drawable.s15,
            R.drawable.s6,R.drawable.s7,
            R.drawable.s18,R.drawable.s19,
    };
    }

I am also facing lagging when scrolling through the gridview. How can i improve the performance and make the gridview load faster. I have read that we can use async task for that, but I am not very familiar about how to use it in this case . Is there any other solution as well? Thanks

Shivam Bhalla
  • 1,879
  • 5
  • 35
  • 63
  • How large are the drawables? If they are too large and your imageview has to scale them down to your 150dp, that could hurt performance. – Patrick Jul 12 '13 at 12:02
  • they are all about 800pixel(width) and 1100pixel(height). Does that have to be scaled down a lot to get to 150dp ? If yes, what size would improve the performance ? – Shivam Bhalla Jul 12 '13 at 14:15
  • That seems be be not a trivial downscaling. While android does cache resources, I am not sure that it caches the rescaled version but more likely the original one. Now imagine that every time you scroll, multiple of these have to be rescaled once a new element scrolls into the screen. If you can, provide the resource at the size you actually need it in the app. (Here, 150dp) See: http://developer.android.com/training/multiscreen/screendensities.html – Patrick Jul 12 '13 at 19:53

1 Answers1

0

If you have a static set of images to use, as said in a comment, you should provide different drawables for different screen densities. Also you should use nine-patch bitmaps for different screen sizes. If this isn't sufficient, carefully read here:

http://developer.android.com/training/displaying-bitmaps/index.html

Depending on the resolution of your images, to achieve the best performance you should: - resize your images so that they don't fill up all your memory and are easier to render - do the resize process in a worker thread so that the UI Thread is not blocked. - cache your resized images on disk

You can use AsyncTask to do your background work.

type-a1pha
  • 1,891
  • 13
  • 19