codesw1tch
codesw1tch

Reputation: 720

Android: Gallery Widget performance is terrible, any reasons?

I have an Android Gallery widget that displays several ImageViews with pictures from the web. The images are stored as jpgs, and get converted to bitmap before added to the ImageViews. Each jpg is 8kb. I'm not doing any scaling on the images.

When I use the gallery with 1 or 2 pictures, it works fine, scrolling is smooth, etc. With 3, it starts to get a little choppy, and at 5 or 10 pictures the application is pretty much unusable.

Does anyone have any ideas as to why the performance is so bad? Does anyone have suggestions for alternatives? Thank you-

@elevine: my method to construct bitmap from jpg url:

private Bitmap getBitmapFromURL(String input) throws IOException {
        URL url = new URL(input);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        return bm;
    }

This is my getView method from my ImageAdapter. I'm beginning to suspect this is where my problem lies... Am I grabbing the images way too many times? Thanks for your help!

public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);

            Bitmap bm;
            try {
                imageView.setImageBitmap(getBitmapFromURL(urls.get(position)));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //mageView.
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setLayoutParams(new Gallery.LayoutParams(100,100));
            return imageView;
        }

Upvotes: 1

Views: 1094

Answers (3)

kingargyle
kingargyle

Reputation: 1239

One other item to consider as well is the animationDuration. You can set this to 0 and it will help with the scrolling performance. I needed to do this recently with a Google TV app I'm working on otherwise there was a second or two pause. Pre-caching your images is also recommended and reducing the number of images that are displayed at a time if possible.

Upvotes: 1

Necronet
Necronet

Reputation: 6813

Here are some tip itthat might come handy:

  • Try to cache the image first on memory with WeakReference and also on disk (if its possible) so you don't have to waste mobile resources on downloading the image all over again.
  • Performance could be bad if you override the GalleryAdapter and you are not helping the adapter to recycle your views List items
  • Also try to execute download operation on a different Thread, consider using AsyncTask.

Here is an interesting ImageManager you might take use

Upvotes: 1

Eric Levine
Eric Levine

Reputation: 13564

If you are calling getBitmapFromURL from your Activity, then it might be blocking the UI thread. Make sure this code runs on a separate Thread or inside an AsyncTask. You may also want to cache the Bitmaps inside a WeakHashmap. Check for the image inside the cache before grabbing it from the network.

Upvotes: 1

Related Questions