0

I'm developing an Android app that has to simulate a sort of Pokédex. For now, what I want to do is simply have all 151 Pokémon printed on my device, so I can scroll them up and down.

The problem is that when I try this thing with such as 9 or 12 images there are no problems, but when I load all the 151 images (all .png), Android kills the app because it's draining too much system resources.

I've heard that there are Java methods that can (don't know how) "destroy" an object when it goes out of the display and then recreate it when it returns in the screen. Anyway if you have different suggestions on how to resolve my problem, every idea is welcome!

Here is my MainActivity:

package com.example.thefe.newsmartkedex;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                 int position, long id) {
                Toast.makeText(MainActivity.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });



    };
}

And here is my ImageAdapter class I use for Gridview:

package com.example.thefe.newsmartkedex;

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 ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.pkmn1, R.drawable.pkmn2,
            R.drawable.pkmn3, R.drawable.pkmn4,
            R.drawable.pkmn5, R.drawable.pkmn6,
            R.drawable.pkmn7, R.drawable.pkmn8,
            R.drawable.pkmn9, R.drawable.pkmn10,
            R.drawable.pkmn11, R.drawable.pkmn12,
            R.drawable.pkmn13, R.drawable.pkmn14,
            R.drawable.pkmn15, R.drawable.pkmn16,
            R.drawable.pkmn17, R.drawable.pkmn18,
            R.drawable.pkmn19, R.drawable.pkmn20,
            R.drawable.pkmn21, R.drawable.pkmn22,
            R.drawable.pkmn23, R.drawable.pkmn24,
            R.drawable.pkmn25, R.drawable.pkmn26,
            R.drawable.pkmn27, R.drawable.pkmn28,
            R.drawable.pkmn29, R.drawable.pkmn30,
            R.drawable.pkmn31, R.drawable.pkmn32,
            R.drawable.pkmn33, R.drawable.pkmn34,
            R.drawable.pkmn35, R.drawable.pkmn36,
            R.drawable.pkmn37, R.drawable.pkmn38,
            R.drawable.pkmn39, R.drawable.pkmn40,
            R.drawable.pkmn41, R.drawable.pkmn42,
            R.drawable.pkmn43, R.drawable.pkmn44,
            R.drawable.pkmn45, R.drawable.pkmn46,
            R.drawable.pkmn47, R.drawable.pkmn48,
            R.drawable.pkmn49, R.drawable.pkmn50,
            R.drawable.pkmn51, R.drawable.pkmn52,
            R.drawable.pkmn53, R.drawable.pkmn54,
            R.drawable.pkmn55, R.drawable.pkmn56,
            R.drawable.pkmn57, R.drawable.pkmn58,
            R.drawable.pkmn59, R.drawable.pkmn60,
            R.drawable.pkmn61, R.drawable.pkmn62,
            R.drawable.pkmn63, R.drawable.pkmn64,
            R.drawable.pkmn65, R.drawable.pkmn66,
            R.drawable.pkmn67, R.drawable.pkmn68,
            R.drawable.pkmn69, R.drawable.pkmn70,
            R.drawable.pkmn71, R.drawable.pkmn72,
            R.drawable.pkmn73, R.drawable.pkmn74,
            R.drawable.pkmn75, R.drawable.pkmn76,
            R.drawable.pkmn77, R.drawable.pkmn78,
            R.drawable.pkmn79, R.drawable.pkmn80,
            R.drawable.pkmn81, R.drawable.pkmn82,
            R.drawable.pkmn83, R.drawable.pkmn84,
            R.drawable.pkmn85, R.drawable.pkmn86,
            R.drawable.pkmn87, R.drawable.pkmn88,
            R.drawable.pkmn89, R.drawable.pkmn90,
            R.drawable.pkmn91, R.drawable.pkmn92,
            R.drawable.pkmn93, R.drawable.pkmn94,
            R.drawable.pkmn95, R.drawable.pkmn96,
            R.drawable.pkmn97, R.drawable.pkmn98,
            R.drawable.pkmn99, R.drawable.pkmn100,
            R.drawable.pkmn101, R.drawable.pkmn102,
            R.drawable.pkmn103, R.drawable.pkmn104,
            R.drawable.pkmn105, R.drawable.pkmn106,
            R.drawable.pkmn107, R.drawable.pkmn108,
            R.drawable.pkmn109, R.drawable.pkmn110,
            R.drawable.pkmn111, R.drawable.pkmn112,
            R.drawable.pkmn113, R.drawable.pkmn114,
            R.drawable.pkmn115, R.drawable.pkmn116,
            R.drawable.pkmn117, R.drawable.pkmn118,
            R.drawable.pkmn119, R.drawable.pkmn120,
            R.drawable.pkmn121, R.drawable.pkmn122,
            R.drawable.pkmn123, R.drawable.pkmn124,
            R.drawable.pkmn125, R.drawable.pkmn126,
            R.drawable.pkmn127, R.drawable.pkmn128,
            R.drawable.pkmn129, R.drawable.pkmn130,
            R.drawable.pkmn131, R.drawable.pkmn132,
            R.drawable.pkmn133, R.drawable.pkmn134,
            R.drawable.pkmn135, R.drawable.pkmn136,
            R.drawable.pkmn137, R.drawable.pkmn138,
            R.drawable.pkmn139, R.drawable.pkmn140,
            R.drawable.pkmn141, R.drawable.pkmn142,
            R.drawable.pkmn143, R.drawable.pkmn144,
            R.drawable.pkmn145, R.drawable.pkmn146,
            R.drawable.pkmn147, R.drawable.pkmn148,
            R.drawable.pkmn149, R.drawable.pkmn150,
            R.drawable.pkmn151
    };
}

Finally, this is the XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.thefe.newsmartkedex.MainActivity">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />
</RelativeLayout>

Thanks for help!

pooja
  • 393
  • 8
  • 16
Ale TheFe
  • 1,540
  • 15
  • 43

2 Answers2

0

Display limited images that can fit your screen and load the other images when scrolling Gridview.

That way system wont have do to do lots of work at once. Your application gets faster also.

You can refer this link:

i want grid view with loading by scroll i have image fetch from sever but i want only 10 images view other can load when scrolling grid view

Community
  • 1
  • 1
Shreyansh Patni
  • 401
  • 2
  • 17
  • OK, Ithink this can solve my problem...I just have to understand how to implement it (I'm not so expert in it). – Ale TheFe Sep 28 '16 at 09:04
  • AndroidStudio gives me a "Cannot resolve symbol" in LoadGigsTask at Line 35 of the class file "EndlessScrollListener"... I'm not sure on how do I have to fix this or where do I have to put the "setOnScrollListener()"...could you help me please? – Ale TheFe Sep 28 '16 at 09:12
0

First of all, I don't see any violation in your code so let's focus the pictures.

The problem is that when I try this thing with such as 9 or 12 images there are no problems, but when I load all the 151 images (all .png), Android kills the app because it's draining too much system resources.

What do you mean by 'load all the 151 images'? At the same time in the same screen? Or you just make quite a small numbers of them(like 9,12,16 etc.) seen in the view and others out of the screen?

I've heard that there are Java methods that can (don't know how) "destroy" an object when it goes out of the display and then recreate it when it returns in the screen. Anyway if you have different suggestions on how to resolve my problem, every idea is welcome!

You can't simply destroy an object by yourself and the Jvm will handle this for you when your objects are invalid or unused. As to this problem the recreation of objects that you implements in 'getView' seems no harm.

My question is: how many images did you show in one whole screen? And what size of them?

If you can provide your demo here, it will be the best to work on.

Benny Huo
  • 390
  • 1
  • 2
  • 11
  • 1. I make quite a small number of them seen in the view and the others are out of screen, yes! 2.Sry...how can I provide a demo here? – Ale TheFe Sep 28 '16 at 09:14
  • Could you tell me the size of your pictures? I can make some experiments. – Benny Huo Sep 28 '16 at 09:20
  • of course, they are all 475x475 pixels in png 32bit – Ale TheFe Sep 28 '16 at 09:21
  • After making some tests, I think the pictures may be a little larger than you may want. You can reduce the size of them to simply meet what you need. And, an asynchronously loading framework may help. You can try [UniversalImageLoader](https://github.com/nostra13/Android-Universal-Image-Loader) – Benny Huo Sep 28 '16 at 09:36
  • **Android does not use JVM** – Enzokie Sep 28 '16 at 09:41
  • You mean Dalvik or Dart. There is no difference to this problem. Anyway, you can called Dalvik the Android Java Virtual Machine....uh, is that a problem? – Benny Huo Sep 28 '16 at 09:59