Java tutorial
//package com.java2s; /* * ImageHelper.java * * Tigase Android Messenger * Copyright (C) 2011-2016 "Tigase, Inc." <office@tigase.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. Look for COPYING file in the top folder. * If not, see http://www.gnu.org/licenses/. */ import android.annotation.SuppressLint; import android.content.ComponentCallbacks2; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import android.util.Log; import java.util.HashSet; import java.util.Set; public class Main { private static final String TAG = "ImageHelper"; protected static LruCache<String, Bitmap> memCache = null; private static Set<Bitmap> placeHolders = new HashSet<Bitmap>(); @SuppressLint("NewApi") public static void onTrimMemory(int level) { int count = 0; if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { count = 10; if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { count = 0; } } else return; int size = 0; if (count > 0) { for (Bitmap b : placeHolders) { size += b.getByteCount(); } } int trimSize = placeHolders.size() == 0 ? 0 : ((count * size) / placeHolders.size()); Log.v(TAG, "trim image cache from " + memCache.size() + " to " + trimSize + " to reduce memory usage, max size " + memCache.maxSize()); memCache.trimToSize(trimSize); } }