How to make addView() cheaper

169 Views Asked by At

I have an activity that measures how long does animate/layout/measure/etc of layout takes.

class RenderingMeasureActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.skeleton)

        val view = layoutInflater.inflate(R.layout.simple, root, false)
        root.postDelayed({
            root.addView(view)
        }, 5000)
    }
}

Skeleton layout is nothing but FrameLayout with match_parent and id root. Simple layout is just:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="ahoj"
        android:textSize="32sp" />

</FrameLayout>

In the activity, I have delayed addView() so I am sure I am measuring just that and no other influence is there.

Now, whatever I do, the measurements are off the charts. It takes at least 17ms (average more like 25ms) which is definite source of JANK (>16.7ms). Longest duration is Command Issue and I get same results from FrameMetrics, gpu profiling bars and similar duration also from systrace.

What am I doing wrong? Is it possible that addView (or inflate) of any layout is so expensive that Android SDK apps will always have JANK? Why, if the same layout (merged into one) is set in setContentView(), it takes less time (not by much though)?

P.S.: I tried other layouts, such as constraint, linear, relative. Merge tag helps a little, but is not always usable.
P.P.S.: Creating layout programmatically has no effect as I have separated inflating part.

0

There are 0 best solutions below