Android Layout optimization

Source: Internet
Author: User

Android affects layout performance primarily overdraw (over-drawn), and repeated rendering of overlapping invisible elements creates additional overhead.

The overdraw is graded in color: blue: Overdraw1 times, Green: Overdraw2 times, light red: Overdraw3 times, dull red, more than Overdraw4 times (optimization required).

Android Layout Optimization Solution:

1. Reasonable selection of controls

LinearLayout is easy to use and efficient, but has a limited range of uses.

The relativelayout is more complex, has a wide use range and is slightly less efficient.

2. Remove Windows default background

Removing the background of the window can be called after Setcontentview () in OnCreate ()getWindow().setBackgroundDrawable(null);或者在theme中添加android:windowbackground="null";

3. Remove unnecessary background

If the child view width is mach_parent, you can see that part of the layout is completely covered, where you can reduce repainting by setting the background separately. For example, if you are using the background of selector, setting the color of the normal state to "@android: Color/transparent" can also solve the problem.

4, ClipRect & Quickreject

We can use Canvas.cliprect () to help the system identify those areas that are visible. This method allows you to specify a rectangular area that will be drawn only within this area, and other areas will be ignored. This API can be very helpful for those custom view that has multiple sets of overlapping components to control the displayed area. While the Cliprect method can also help conserve CPU and GPU resources, drawing instructions outside of the Cliprect area will not be executed, and components that are part of the content within the rectangular region will still be drawn. In addition to the Cliprect method, we can also use Canvas.quickreject () to determine if a rectangle is not intersected, skipping the drawing operations in the non-rectangular areas.

5, Viewstub (high-efficiency placeholder) Viewstub is a lightweight view, an invisible, non-occupying layout location that occupies very small resources. You can specify a layout for viewstub, in inflate layout, only viewstub is initialized, and when Viewstub is set to visible, or when viewstub.inflate () is called, The layout viewstub is inflate and instantiated, and the layout properties of the Viewstub are passed to the layout that it points to. In this way, you can use viewstub to make it easier to run, or not to display a layout.
<viewstub    android:id= "@+id/stub_view"    android:inflatedid= "@+id/panel_stub"     android:layout= "@layout/progress_overlay"    android:layout_width= "Fill_parent"     Android:layout_height= "Wrap_content"    android:layout_gravity= "Bottom"/>

When you want to load a layout, you can use one of the following methods:

= ((viewstub) Findviewbyid (R.id.stub_view)). Inflate ();

6. Merge

Action: Kill a View level.

There are two cases in which we can use the merge tag to make container controls. The first seed view does not need to specify any layout properties for the parent view, that is, the parent container is just a container, and the child view needs to be added directly to the parent view for display on the row. The other is that if you need to embed a layout (or view) inside the LinearLayout, and the root node of the layout (or view) is also linearlayout, so that there is a layer of unused nesting, it is no doubt that this only slows down the program speed. And this time, if we use the merge root tag, we can avoid that problem. In addition, the merge can only be used as the root tag of the XML layout, and when inflate a layout file beginning with <merge/>, a parent viewgroup must be specified, and Attachtoroot must be set to true.

7, the use of Draw9patch to ImageView plus a border, you must have encountered this demand, usually in the back of the ImageView set a background map, revealing the border is the perfect solution to the problem, this imageview, set up a two-layer drawable, The bottom layer is just a frame for the picture. But the overlapping areas of the two-layer drawable were plotted two times, leading to Overdraw.
Optimization scenario: Make the background drawable into Draw9patch, and set the part that overlaps the foreground to transparent. This overdraw is optimized because Android's 2D renderer optimizes the transparent areas in the Draw9patch. But the background image must be made into Draw9patch, because the Android 2D renderer only has this optimization for draw9patch, otherwise, a normal PNG, even if you set the middle part to transparent, will not reduce this overdraw. 8, the use of caution with alpha if you do alpha conversion to a view, you need to draw the view first, and then do alpha conversion, and finally the converted effect is plotted on the interface. The popular point, do the alpha conversion needs to draw two times to the current view, imagine, drawing efficiency will be greatly reduced, time will double, so alpha is still used with caution.
If you must do alpha conversion, you can use caching.
View.setlayertype (Layer_type_hardware);d osmoething (); View.setlayertype (Layer_type_none);

By Setlayertype, the current interface can be cached in the GPU, so that it does not need to draw the original interface every time, but GPU memory is quite valuable, so it should be released immediately after use.

9, avoid "overdesign"

Reference: HTTP://WWW.JIANSHU.COM/P/145FC61011CD

Android Layout optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.