Roger RV
Roger RV

Reputation: 134

App spends too much RAM when I do large loops

I have this code and I want to do the same but spending less ram. Any idea to set a image loader in each of 300 ImageView that I generate or something similar to solve this issue?

DisplayMetrics lDisplayMetrics = getResources().getDisplayMetrics();
int widthPixels = lDisplayMetrics.widthPixels;
for (int getDrawables = 0; getDrawables < 300; getDrawables++)
{
    final ImageView icoView = new ImageView(ImageEditor.this);
    icoView.setImageResource(getResources().getIdentifier("icon_"+getDrawables, "drawable", getPackageName()));
    icoView.setLayoutParams(new LinearLayout.LayoutParams(widthPixels/7,widthPixels/7));
    gridlayout.addView(icoView);
    int idGen = View.generateViewId();
    icoView.setId(idGen);
    icoView.setTag("icon_"+getDrawables);
    gridlayout.setColumnCount(6);
}

Upvotes: 3

Views: 87

Answers (2)

Roger RV
Roger RV

Reputation: 134

Solution: Adding new drawables but with less resolution and using there for my stickers' selector. Using GridView as a Recycler I only reduce 500MB of RAM usage. Another way I see is converting drawables to bitmaps and then set a resize to the bitmaps.

Upvotes: 0

Jonas K&#246;ritz
Jonas K&#246;ritz

Reputation: 2644

Do not add so many ImageViews to your layout. They wont be visible all at once anyways i assume. Use something like a GridView or a RecyclerView that manages the recycling of views for you.

Upvotes: 2

Related Questions