2

I'm trying to make a Level selector to my game, now it's working but, it's really slow on some phones and some times not working (FC), I realized that it's using too much heap memory.

I'm quite new on android dev, so here's my code, I hope you can help me.

ACTIVITY WITH THE VIEWPAGER:

   setContentView(R.layout.level_selector);
   ViewPagerAdapter adapter = new ViewPagerAdapter(this);
   ViewPager myPager = (ViewPager) findViewById(R.id.mypagerwontwork); //cool id huh
   myPager.setAdapter(adapter);
   myPager.setCurrentItem(0);

VIEW PAGER ADAPTER:

  public Object instantiateItem(View collection, int position) {
       LayoutInflater inflater = (LayoutInflater)    activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
     if(position == 0)
     {
         View layout = inflater.inflate(R.layout.world1, null);
         final GridView gridview = (GridView) layout.findViewById(R.id.gridLvl);
         gridview.setAdapter(new LevelAdapter(activity,"World1"));
         gridview.setOnItemClickListener(itemClickListener);
         ((ViewPager) collection).addView(layout); 
         return layout;
     }

GRID VIEW ADAPTER:

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
        if (mWorld.equals("World1"))
        {
            imageView.setImageResource(World1[position]);
        }

USEFUL INFORMATION:

  • There are 3 "Worlds" (View page) each with a different background
  • Each "World" has 15 Images, with number from 1 to 15.
  • The image size is: +- 12kb
  • The BG size is: +- 140kb

I hope it's clear.

thanks.

Moondustt
  • 864
  • 1
  • 11
  • 30

2 Answers2

2

I found out that my images were too big, not the size, but the resolution.

I had to resize them to ldpi, mdpi and hdpi.

these sizes can be found at:

Android activity image background size

Button Image size in android

Community
  • 1
  • 1
Moondustt
  • 864
  • 1
  • 11
  • 30
0

You should avoid creating objects in getView methods (like in your grid view adapter). Instead you can use ViewHolder pattern:

http://www.jmanzano.es/blog/?p=166

Michał Z.
  • 4,119
  • 1
  • 22
  • 32