Android performance optimization is mainly divided into two points
1, layout optimization
2, memory optimization
Layout optimization
First look at the layout optimization, the system in the rendering UI will consume a lot of resources, so, the layout of the optimization is particularly important
Avoid overdraw
Which is to avoid excessive rendering, excessive rendering will waste more resources, for example, the Android system will default to draw Activity
the background, this time we set a background, so the default background is over drawn, in the "developer Tool" in the "debug GPU over Draw" option, We can use color to judge the number of times we're over drawn.
As shown in figure:
So we can increase the blue area as much as possible and reduce the red area .
Optimize layout hierarchy
In the Android system, the system of view measurement, layout, rendering, through the traversal of the View
tree, so that a layout of too many levels, will seriously affect the measurement, layout, drawing speed, so to reduce the height of the view tree, Google recommends that the layout level not exceed 10 levels in the document, so you also need to avoid nesting without using the layout
Using the Include tag
I believe that everyone will encounter toolbar in the project, or a UI in the use of multiple interfaces, when encountering this problem we will generally use include (after all, the great God "first line of code" teaching good), which reduces the code redundancy, but also conducive to late changes
Using Viewstub to implement view delay loading
ViewStub
The label is View.GONE
similar to the way it is, but it ViewStub
will only be rendered at the time of the display, and GONE
it is finished when initialized, so it is ViewStub
more efficient than that.
Here's how to use the codeViewStub
First we define a layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical" Android:layout_width= "Match_parent"
android:layout_height= "match_parent" >
<textview
Android:id= "@+id/tv"
android:layout_width= "wrap_content" android:layout_height= "wrap_content"
/>
</LinearLayout>
There's only one in here TextView
, and then we're quoting him in our own layout. ViewStub
<button
android:id= "@+id/btn"
android:layout_width= wrap_content "android:layout_height=" Wrap_
Content "/>
<viewstub
android:id=" @+id/vs "android:layout_width=" Wrap_content "
android: layout_height= "Wrap_content"
android:layout= "@layout/view_stub"/>
The logic here is to click on the button and then appear ViewStub
the referenced layout (that is, the layout file we wrote above)
Findviewbyid (R.ID.BTN). Setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick ( View view) {
Viewstub vs = (Viewstub) Findviewbyid (R.id.vs);
View Vsview = Vs.inflate ();
TextView TV = (TextView) Vsview.findviewbyid (r.id.tv);
Tv.settext ("hahaha");
}
);
ViewStub
There are two ways to show the layout of the reference, one is the ViewStub.setVisibility();
other is the ViewStub.inflate();
difference between the two methods is inflate()
to return the layout of the reference, and then you can use this layout to find the inside of the child View
to operate
Run the program click the button, you can see TextView
that has been shown, and has been assigned to "hahaha"
Hierarchy Viewer
This tool can display our layout, but can only be used in simulators and factory demo machine, but Google's great God provides an open source project Viewserver, interested to see.
This is not too much elaborated, interested can check the relevant information on their own
Memory optimization
We all know that the latest Android manufacturers are in the spelling, which we are most concerned about the memory of a few g, but the Android phone has a large memory, but it is very card, this is because some mobile phone software in the low memory of the phone can not be killed, that is, we say "kill"
We know that memory is generally divided into these parts
1, register
2, Stack
3, Heap
4. Static Storage Area
5, Chang
The register is the fastest, but we can't control it in the program.
Store basic data types in stacks
The heap holds objects and arrays, and is generally managed by the Java GC in the heap
A static store stores data that is always present at run time in a fixed location, and Java specifically divides a region to manage static variables
A constant pool is a collection of constants used by that type
Optimization of bitmap
We all know that the biggest threat to Oom is in Android because it takes up a lot of memory, so we should:
1, use the appropriate resolution size of the picture
Because Android matches the graphics in different resolution folders when it is fit, it can cause more resources to be consumed if the resolution of the picture does not match the resolution of the resource.
2, using the cache
Generally have memory cache and hard disk cache
Code optimization
1. Use of constantsstatic
2, using static method, static method than normal method to improve the access speed of about 15%
3, reduce unnecessary member variables, if it can be defined as local variables is best defined as local variables
4. Reduce unnecessary objects
5, less use of enumerations, iterators
6, to Cursor
, Receiver
, and File
other objects, pay attention to recycling and cancellation of registration
7, use SurfaceView
to replace the view for a large number of and frequent drawing operations
8, try to use the view cache, not every time inflate()
to perform to resolve the view
9. Avoid using reflection
10, use RenderScript
, OpenGL
to carry out very complex drawing operations
Summarize
These are some of the things that are optimized for Android, and if you use Androidstudio, Androidstudio provides a memory monitoring tool that works well if you don't use Androidstudio, Then I suggest you use androidstudio~.
In addition, optimization is not absolutely perfect, each optimization is based on the current environment to do, to understand that communication is the best optimization, not blindly follow, not casually, think twice before the line. Hopefully this article will help you develop Android.