Optimization of Android listview to improve Android application efficiency

Listview is a frequently used control. Each sub item item item in listview can be a string or a composite control. The adapter is the intermediary between the listview and the data source.

When each data enters the visible area, getview () of the adapter will be called to return the view representing the specific data. Called frequently when scrolling is touched. Support hundreds of data.

The following is an XML file that displays each piece of data:

1。 The simplest method, the slowest and least practical

2。 Using convertview to recycle views, the efficiency is improved by 200%.

3。 Using the view holder mode, the efficiency is improved by 50%

Comparison of update efficiency of adapter:

The update of 1 is less than 10 frames / second

The update of 2 is close to 30 frames / second

The update of 3 is close to 40 frames / second

Background and image

The view background image always fills the entire view area

1。 Improper image size can cause automatic scaling

2。 Avoid real-time scaling

3。 It is best to pre zoom to the view size

The efficiency of 1 is close to 25 frames / second

The efficiency of 2 is close to 50 frames / second

By default, windows have an opaque background

Sometimes not

-􁭱 the top view is opaque

-􁭱 the top view covers the entire window

Updating an invisible background is a waste of time

Delete window background:

1。 Modify code

2。 Modify XML

First, make sure your RES / values / styles.xml has

Then edit androidmainfest.xml

Update request

When the screen needs to be updated, it is simple and convenient to call the invalidate () method, but it is too expensive to update the whole view.

It is best to find invalid regions first and then call them.

Views and layouts

If a window contains many views, it starts too slowly, takes a long time to draw, and the user interface reacts very slowly

resolvent:

1。 Reduce hierarchy using textview's compound drawable

2。 Delayed view expansion using viewstuf

Define viewstuf in XML file

When you need to expand the view,

3。 Merge intermediate views with < merge >

By default, the root of the layout file is added to the parent view as a node. If merge is used, the root node can be avoided

4。 Use ralativelayout to reduce hierarchy

5. Use custom view

6 use custom layout

memory allocation

Avoid creating Java objects in performance sensitive code

1。 Measurement onmeasure ()

2。 Layout (onlayout)

3。 Drawing ondraw() dispatchdraw()

4。 Event handling ontouchevent() dispatchtouchevent()

5。 adapter: getview() bindview()

Forced restriction (applicable to debugging mode)

Manage objects:

1。 For soft references: the best choice for memory caching

2。 Applicable to weak references: avoid memory leakage

Memory cache:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>