封装一个工具类:回收任何布局中被Imageview吃掉的内存(附源码DEMO)
在android程序中,为了避免OOM的出现,最为直接的方式就是将Bitmap占用的内存Recycle掉。但是在一个布局或者控件中,图片非常多,来源又不一样。这个时候,写代码手动的释放,也蛮费事的。
所以我封装了一个工具类,只要将当前activity所在的布局传入工具类,工具类会自动释放布局中所有Imageview占用的图片资源。
说明:此工具类用于回收布局中被Imageview占用的图片资源,传入根布局,或者继承自ViewGroup类的子类。
适用范围: ViewGroup的直接或间接子类
常用的的布局类(如线性布局,相对布局,帧布局,绝对布局)都是继承自ViewGroup基类,为了弄明白适用范围,我们来看一下ViewGroup的直接和间接子类:
Known Direct Subclasses(儿子辈)
AbsoluteLayout,AdapterView<T extends Adapter>, FragmentBreadCrumbs, FrameLayout, GridLayout, LinearLayout,
PagerTitleStrip,RelativeLayout,SlidingDrawer,ViewPager
Known Indirect Subclasses(孙子辈)
AbsListView
Base class that can be used to implement virtualized lists of items.
AbsSpinner
An abstract base class for spinner widgets.
AdapterViewAnimator
Base class for a
AdapterView
that will perform animations when switching between its views.AdapterViewFlipper
Simple
ViewAnimator
that will animate between two or more views that have been added to it.AppWidgetHostView
Provides the glue to show AppWidget views.
CalendarView
This class is a calendar widget for displaying and selecting dates.
DatePicker
This class is a widget for selecting a date.
DialerFilter
ExpandableListView
A view that shows items in a vertically scrolling two-level list.
Gallery
This class was deprecated in API level 16. This widget is no longer supported. Other horizontally scrolling widgets include
HorizontalScrollView
andViewPager
from the support library.GestureOverlayView
A transparent overlay for gesture input that can be placed on top of other widgets or contain other widgets.
GridView
A view that shows items in two-dimensional scrolling grid.
HorizontalScrollView
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.
ImageSwitcher
ListView
A view that shows items in a vertically scrolling list.
MediaController
A view containing controls for a MediaPlayer.
NumberPicker
A widget that enables the user to select a number form a predefined range.
PagerTabStrip
PagerTabStrip is an interactive indicator of the current, next, and previous pages of a
ViewPager
.RadioGroup
This class is used to create a multiple-exclusion scope for a set of radio buttons.
ScrollView
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.
SearchView
A widget that provides a user interface for the user to enter a search query and submit a request to a search provider.
Spinner
A view that displays one child at a time and lets the user pick among them.
StackView
TabHost
Container for a tabbed window view.
TabWidget
Displays a list of tab labels representing each page in the parent's tab collection.
TableLayout
A layout that arranges its children into rows and columns.
TableRow
A layout that arranges its children horizontally.
TextSwitcher
Specialized
ViewSwitcher
that contains only children of typeTextView
.TimePicker
A view for selecting the time of day, in either 24 hour or AM/PM mode.
TwoLineListItem
A view group with two children, intended for use in ListViews.
ViewAnimator
Base class for a
FrameLayout
container that will perform animations when switching between its views.ViewFlipper
Simple
ViewAnimator
that will animate between two or more views that have been added to it.ViewSwitcher
ViewAnimator
that switches between two views, and has a factory from which these views are created.WebView
A View that displays web pages.
ZoomControls
The
ZoomControls
class displays a simple set of controls used for zooming and provides callbacks to register for events.
以上内容在ViewGroup的文档中都有介绍,这里不再赘述。
回收的内容:
说完了布局文件,接下来说一下需要回收Imageview中的哪些东东。
ImageView显示图片有两种方式:
1.代码或者xml文件中设置为背景
2.代码或者xml文件中设置Imageview的内容,如xml中的src,代码中的setImageResource等等。
总之,看起来背景图片和内容图片不一样,我们分开回收。默认回收内容图片,背景图片是否回收可以自行设置。
使用场景:
当Activity退出时,需要清理和释放图片资源时,使用此类。
好了,废话不多说了,上代码:
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == event.KEYCODE_BACK && event.getRepeatCount() == 0) { //回收图片 new RecycleBitmapInLayout(true).recycle(rootLayout); //退出时,请求杀死进程 android.os.Process.killProcess(android.os.Process.myPid()); } return super.onKeyDown(keyCode, event);}存在问题:释放的时机不恰当,会造成”trying to use a recycled bitmap android.graphics.Bitmap”的错误,请谨慎使用
总结:是一个管杀不管埋的工具类。
附Demo:点我下载
原文链接:http://www.67tgb.com/?p=517
欢迎访问:望月听涛