As a company's Android Application, the function is very simple. In viewflipper, gesturedetector is used to implement slide switching between images. Override the onfling () method to determine the sliding direction of the finger, load the bitmap to an imageview, call the addview () method of viewflipper, and then load the imageview to viewflipper. The worst solution is bitmap's out
The memory overflow error is reported when many graphs are loaded. Regarding this problem, I found a lot of solutions on the Internet and did not have a good solution. I happened to have encountered this problem in the group. The solution I found is summarized as follows, I will provide my own solutions later.
1. Set options of bitmapfactoryThe Code is as follows:
BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 3;Bitmap bitmap = BitmapFactory.decodeStream(is, null, ops);
The insamplesize is used to compress the length and width of an image. setting this parameter to 3 means that the length and width of the generated image are 1/3 of the original length. This method only optimizes the performance and does not fundamentally solve the memory problem. When too many bitmaps are loaded, there will still be a memory overflow problem.2.
Use softreference to cache ImagesThe code is not pasted, here there is a detailed implementation method, as well as the introduction of soft reference: http://www.cnblogs.com/dwinter/archive/2012/01/30/2331556.html implementation principle is to establish a softreference used to store pictures and indexes, because softreference is easy to be recycled by GC, to release the occupied memory. But implementing it in person does not solve the problem of OOM very well. I am careful to guess that viewflipper occupies the imageview containing bitmap, so the reference always exists and GC will not be automatically recycled.3.
Call the recycle () method of Bitmap to reclaim bitmapDetermine the isrecycled () of the bitmap whose ID number is current-2 and current + 2. If not, call recycle () to forcibly recycle the image. In fact, this method works very well, but when you want to browse the image, the error "trying to load the bitmap that has been recycled" will appear. I think there is no problem with the program writing (it is estimated that there is still a problem), and finally I gave up this method.4.
Create lrucache to make image loading more efficientThis method is available in Google's official documentation. The link is as follows: the principle of http://developer.android.com/training/displaying-bitmaps/index.html implementation is to establish an lrucache, which can display and use images efficiently. Softreference is mentioned in the second method. This is not recommended in this article. You can refer to the following for detailed reasons. Lrucache is in the API
What should I do if the Android 2.2 magic horse is added in level 11? It doesn't matter. Manually write an lrucache or find one. I want to paste it out.
/** * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package android.guju.service;import java.lang.ref.ReferenceQueue;import java.lang.ref.WeakReference;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.Map;/*** * An LRU cache which stores recently inserted entries and all entries ever * inserted which still has a strong reference elsewhere. */public class LruCache<K, V> { private final HashMap<K, V> mLruMap; private final HashMap<K, Entry<K, V>> mWeakMap = new HashMap<K, Entry<K, V>>(); private ReferenceQueue<V> mQueue = new ReferenceQueue<V>(); @SuppressWarnings("serial") public LruCache(final int capacity) { mLruMap = new LinkedHashMap<K, V>(16, 0.75f, true) { @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > capacity; } }; } private static class Entry<K, V> extends WeakReference<V> { K mKey; public Entry(K key, V value, ReferenceQueue<V> queue) { super(value, queue); mKey = key; } } @SuppressWarnings("unchecked") private void cleanUpWeakMap() { Entry<K, V> entry = (Entry<K, V>) mQueue.poll(); while (entry != null) { mWeakMap.remove(entry.mKey); entry = (Entry<K, V>) mQueue.poll(); } } public synchronized V put(K key, V value) { cleanUpWeakMap(); mLruMap.put(key, value); Entry<K, V> entry = mWeakMap.put( key, new Entry<K, V>(key, value, mQueue)); return entry == null ? null : entry.get(); } public synchronized V get(K key) { cleanUpWeakMap(); V value = mLruMap.get(key); if (value != null) return value; Entry<K, V> entry = mWeakMap.get(key); return entry == null ? null : entry.get(); } public synchronized void clear() { mLruMap.clear(); mWeakMap.clear(); mQueue = new ReferenceQueue<V>(); }}
You can use it directly ~ But remember to keep the copyright statement of others, which is the minimum. In fact, I tried all of the above methods and the problem has not been solved-even after creating an lrucache. After a question is raised on stackoverflow, a personal answer is to call removeview (
) Release bitmap. Suddenly realized! Okay, the problem is solved.
imageView.setImageBitmap(bitmap);imageView.setScaleType(ImageView.ScaleType.CENTER);viewFlipper.removeAllViews();viewFlipper.addView(imageView);
Each time before addview, remove the previous view. Of course, setting the options of bitmapfactory and setting up lrucache are also essential to provide a better image browsing experience. However, a new problem is the problem of adding animation. If removeview is called directly, the animation effect of that view is not easy to set. I hope to help some users.