0

I need to make level select for game , I used custom gallery. (are there any better way to do it) My images are in PNG format and gallery scroll very slowly what I have to change to work it better?

my code:

public class LevelSelectActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.level);

    ImageButton back = (ImageButton) findViewById(R.id.back);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            LevelSelectActivity.this.finish();
        }
    });

    Gallery levels = (Gallery) findViewById(R.id.levels);
    levels.setAdapter(new ImageAdapter(this));


}

public class ImageAdapter extends BaseAdapter {
    /** The parent context */
    private Context myContext;

    private int[] myImageIds = { R.drawable.level_select_1, R.drawable.level_select_2,
            R.drawable.level_select_3, R.drawable.level_select_4, R.drawable.level_select_5 };

    /** Simple Constructor saving the 'parent' context. */
    public ImageAdapter(Context c) {
        this.myContext = c;
    }

    /** Returns the amount of images we have defined. */
    public int getCount() {
        return this.myImageIds.length;
    }

    /* Use the array-Positions as unique IDs */
    public Object getItem(int position) {
        return position;
    }

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

    /**
     * Returns a new ImageView to be displayed, depending on the position
     * passed.
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = null;

        if (convertView == null) {

            i = new ImageView(this.myContext);

            i.setBackgroundResource(this.myImageIds[position]);

            i.setScaleType(ImageView.ScaleType.FIT_XY);

            i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

        } else {

            i = (ImageView) convertView;

        }

        return i;
    }
}

}

Androider
  • 121
  • 1
  • 11
  • By "scrolling very slowly" do you mean that it's jerky and doesn't scroll smoothly or just unresponsive to drag & fling input? – Jens Dec 30 '11 at 12:17
  • also when I test it on nexus one it make "java.lang.OutOfMemoryError: bitmap size exceeds VM budget" and on other HTC there is no that exception – Androider Dec 30 '11 at 12:25
  • Well, Gallery is a heaping piece of shit courtesy of Google, it does not reuse views (so caching is _hard_) - loading the resources **repeatedly** is going to be jerky since it takes a few milliseconds to pull a large png. If you want to use the default Gallery you could consider using smaller images so you can load them all into memory instead of loading them on-the-fly. – Jens Dec 30 '11 at 12:32
  • now I load all bitmaps at the first time, after every getView I set that loaded bitmap, it scroll now better but on nexus one it throw OutOfMemoryError on load bitmaps – Androider Dec 30 '11 at 12:47
  • well. if your bitmaps are too large, loading them all will cause an OOM. I guess the Nexus One has less available memory than your other target device. Simply put, Gallery _stinks_. This has however been discussed in length here on StackOverflow, [and a replacement is available](http://stackoverflow.com/questions/5789879/does-a-replacement-for-gallery-widget-with-view-recycling-exist) – Jens Dec 30 '11 at 12:52

0 Answers0