Android performance optimization skills & black technology, so that your App to achieve the ultimate fluency and stability, today we mainly explain the difficulties of performance optimization and tool use.
1. Learning performance optimization analysis tools
The System Trace,
Systrace is a tool that collects and detects time information, showing where CPU and time are being spent, and what each process and thread is doing in its CPU slice. It also indicates what went wrong and makes Fix recommendations. The resulting trace file is opened in HTML format, which is very convenient to view directly with a browser. Open method: After the DDMS is enabled, connect the mobile phone and click the SysTrace button in the upper row of the mobile phone. The opening effect is shown below:
· Hierarchy Viewer
The Hierarchy Viewer provides a visual interface to observe the Hierarchy of a layout, allowing us to optimize the Hierarchy
TraceView,
2. Layout optimization
Layout optimization in Android mainly includes the following three aspects: layout level and measurement times, layout excessive drawing, drawing process
1. Layout level and measurement times
(I) Reasonable selection of the parent container
FrameLayout, LinearLayuut without Layou_wight, RelativeLayout. Because the LinearLayout and RelativeLayout with Layot_weight measure twice.
Starting from Android Studio3.0, Google does not recommend using it, so we need to manually find it in tools under the SDK directory, then run your APK and select Hierarchy View to View the corresponding Hierarchy, as follows:
The purpose of the include tag is to refer directly to an existing layout without having to rewrite it. Instead, include is usually used in conjunction with the merge tag, which is described below.
The merge tag is usually used as an extension of the include tag to solve the problem of increasing the layout level caused by the introduction of include. After the merge tag is used, the View in the merged layout becomes the child View of the parent layout. As follows:
The ViewStub, which inherits from the View, is a lightweight, zero-width and zero-height component that does not itself participate in layout and drawing. Therefore, it can be used to not load when it is not needed and load when it is needed, thus improving performance. So how to do when you need to display?
setVisiable
Constaint Layout allows you to create complex layouts without using any nesting, and Relative Layout
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintTop_toTopOf=”parent”
app:layout_constraintLeft_toRightOf=”@+id/iv_image”
2. Overdraw
, the Android device monitor
Avoid assigning objects in the onDraw () method, because the onDraw method can be called multiple times, which can create a lot of unnecessary objects. Use ClipRect to specify a drawing area. When using a custom View, we can use this method to specify one visible area for drawing and the other areas are ignored. That is, only the region specified by clipRect is drawn. For example, when the image is stacked, we will not draw the overlap, only draw the non-overlap. Draw directly as follows:
3. Thread optimization
1. The View cannot be operated on by non-UI threads. Because Android’s UI is not secure, if the View can be accessed or modified by different threads, unexpected behavior or concurrent errors can occur during application execution.
2. When using threads, avoid using synchronization in round-tripping because the operation of acquiring and releasing locks is expensive. CPU resources are consumed.
3. When handling multithreading and interthread communication, use HandlerThread, which has Looper wrapped inside, to exit/release resources when not in use.
4. It is recommended to use AsyncTask when communicating between the worker thread and the UI thread (after Android 7.0, internal tasks become serial processing, which will no longer occur when the number of concurrent tasks exceeds the saturation strategy)
5. Loader can be used instead of AsyncTask in some cases, because the Loader life cycle is independent (related to the Application Context), it is still alive when the Activity/Fragment is destroyed and rebuilt, and it specifically uses asynchronous operations. For example, AsyncTaskLoader can replace AsyncTask to realize the latter function, but its life cycle is completely independent of Activity. Remember to destroy Loader after use.
6. When your Service does not need to interact, use an automatic stop IntentService.
7. When you want to extend the life of the BroadcastReceiver, for example, start a background thread, IntentService. OnReceiver invokes the BroadcastReceiver. GoAsync (), it returns a PendingResult object, at this point, the radio receiver will extend to the life cycle of continued to PendingResult. The finish () method call.
8. It’s best to manually create the thread pool by using the constructor instead of using Executors to invoke the factory method. If you use the wrong thread pool, avoid resource depletion.
9. Give the thread a nice name for debugging.
10. Thread pool Sets the thread lifetime to ensure accurate release of idle threads.
4. Memory leaks
5. Algorithm optimization
6. Other optimization points
In initialization methods such as Activity and Fragemnt’s onCreate, performing time-consuming operations (such as reading various data) can slow down the page load and make the APP feel slow to the user. These time-consuming tasks can be handled asynchronously, reducing the burden of application startup.
Despite the advantages of vector graphics, drawing vector graphics is performance-consuming. In the areas that affect user experience, such as initial loading of applications, Bitmap is still recommended to replace vector map to improve APP startup efficiency.
Regular expressions are time consuming, as you can see from your constant use of TraceView. So while regular expressions are very elegant, when it comes to performance, you can use other judgment methods to improve your APP’s performance.
Floating-point operations in Java are about twice as slow as integer data, so use them for any problems that integer data can solve.
Log Logs used for debugging during development should be deleted in time when the project is launched. Of course, useful logs should be left for later analysis.
Unused resources will increase the SIZE of APK, since it is no longer used, it should be deleted in time when online.
Lint Code checking Using static code checking tools such as Lint can help us find many hidden problems. The fewer problems Lint checks, the more formal the code, the less likely it is to have various problems, and the APP performance will naturally improve.
Global broadcasting is also a performance drain. For in-application communication, interface callbacks, EventBus, etc. are a better choice than broadcasting. When registering broadcasts dynamically, don’t forget to unregister broadcasts.
7,
【 material 】To obtain.