Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:05:26+00:00 2026-05-26T06:05:26+00:00

I’m using GalleryView with ~40 images, and so slow because no recycling… Anyone can

  • 0

I’m using GalleryView with ~40 images, and so slow because no recycling…

Anyone can show me a basic recycling to GalleryView on getView method.

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };

    public ImageAdapter(Context c) {
        mContext = c;

        TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return mImageIds.length;
    }

     public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

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

        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:05:27+00:00Added an answer on May 26, 2026 at 6:05 am

    Instead of creating a new ImageView in getView you should convert the convertView to the view you want. Here is an example of one way to do it:

    public View getView(int position, View cv, ViewGroup parent) {
    
        if (! convertView istanceof ImageView)
        {
            ImageView cv = new ImageView(mContext);
            cv.setLayoutParams(new Gallery.LayoutParams(150, 100));
            cv.setScaleType(ImageView.ScaleType.FIT_XY);
            cv.setBackgroundResource(mGalleryItemBackground);
    
        }
        cv.setImageResource(mImageIds[position]);
    
        return cv;
    }
    

    Simply convert the convertView to match what you want, but first make sure its the proper view type.

    Update: You should also downsample the images before you display them. Lets assume that you have a 500×500 pixel image saved under res/drawable but the image is only going to take up 125×125 pixels on the screen. You need to downsample the image before you display it. To know how much you need to downsample the bitmap you must first get its size

    int maxSize = 125; // make 125 the upper limit on the bitmap size
    int resId; // points to bitmap in res/drawable
    
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true; // Only get the bitmap size, not the bitmap itself
    BitmapFactory.decodeResource(c.getResources(), resId, opts);
    
    int w = opts.outHeight, h = opts.outHeight;
    int maxDim = (w>h)?w:h; // Get the bigger dimension
    

    Now that we have the size calculate how much to downsample the image. If we have a 500×500 bitmap and we want a 125×125 bitmap we keep 1 out of every 4 pixels which we get from int inSample = 500/125;

    int inSample = maxDim/maxSize; 
    
    opts = new BitmapFactory.Options();
    opts.inSampleSize = inSample;
    

    Now simply decode the resources and we have our down-sampled bitmap.

    Bitmap b = BitmapFactory.decodeResource(c.getResources(), resId, opts);
    

    Keep in mind that the original bitmap is unaffected. You can decode the image again and set opts.inSampleSize to 1 and you will get the entire 500×500 bitmap image.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.