[Android] Use FrameLayout for root layout instead RelativeLayout

Andrew Churilo git at videolan.org
Tue May 24 06:25:15 UTC 2022


vlc-android | branch: master | Andrew Churilo <AndrewChuriloMinsk at gmail.com> | Wed May 18 19:06:24 2022 +0300| [f1a65a7f516545fab395c2379d47c5b6bd4cf395] | committer: Andrew Churilo

Use FrameLayout for root layout instead RelativeLayout

`FrameLayout` avoid double measuring that `RelativeLayout` do.
When UI appearing time is about ~40 ms, hard to see result of small optimizations. Based on logs It looks the same as was before. But the result of removing `RelativeLayout` can be visible inside profiler. Sections with measuring is much less without `RelativeLayout`. I'll add trace-file and screenshot to merge request.
UI appearing time (landscape orientation, video paused, animation disabled):
Samsung Note 4, Android 6.0.1 (release): ~58 ms.

> https://code.videolan.org/videolan/vlc-android/commit/f1a65a7f516545fab395c2379d47c5b6bd4cf395
---

 application/vlc-android/res/layout/player.xml      | 20 +++++++----------
 .../vlc/gui/video/VideoPlayerOverlayDelegate.kt    | 26 +++++++++++-----------
 2 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/application/vlc-android/res/layout/player.xml b/application/vlc-android/res/layout/player.xml
index 3177c2b42..7fa95bbc8 100644
--- a/application/vlc-android/res/layout/player.xml
+++ b/application/vlc-android/res/layout/player.xml
@@ -1,10 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:id="@+id/player_root"
-        android:layout_width="fill_parent"
-        android:layout_height="fill_parent"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
         android:keepScreenOn="true">
 
     <!--
@@ -30,8 +30,7 @@
             android:id="@+id/player_brightness_stub"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_alignParentRight="true"
-            android:layout_centerVertical="true"
+            android:layout_gravity="center_vertical|end"
             android:layout_marginBottom="@dimen/default_margin"
             android:layout_marginRight="@dimen/tv_overscan_horizontal"
             android:layout="@layout/player_overlay_brightness" />
@@ -40,16 +39,15 @@
             android:id="@+id/player_volume_stub"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_alignParentLeft="true"
+            android:layout_gravity="center_vertical"
             android:layout_marginLeft="@dimen/tv_overscan_horizontal"
-            android:layout_centerVertical="true"
             android:layout_marginBottom="@dimen/default_margin"
             android:layout="@layout/player_overlay_volume" />
 
     <View
             android:layout_width="match_parent"
             android:layout_height="130dp"
-            android:layout_alignParentBottom="true"
+            android:layout_gravity="bottom"
             android:id="@+id/hud_background"
             android:background="@drawable/gradient_hud_player"
             android:visibility="gone"/>
@@ -57,7 +55,6 @@
     <View
             android:layout_width="match_parent"
             android:layout_height="150dp"
-            android:layout_alignParentTop="true"
             android:id="@+id/hud_right_background"
             android:background="@drawable/gradient_title_player"
             android:visibility="gone"/>
@@ -136,9 +133,8 @@
 
     <androidx.constraintlayout.widget.ConstraintLayout
             android:layout_width="match_parent"
-
             android:maxWidth="480dp"
-            android:layout_centerHorizontal="true"
+            android:layout_gravity="center_horizontal"
             android:background="@color/playerbackground"
             android:id="@+id/video_playlist_container"
             android:visibility="gone"
@@ -224,4 +220,4 @@
             android:layout_height="match_parent"
             android:layout="@layout/player_resize" />
 
-</RelativeLayout>
+</FrameLayout>
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt
index 9608ce066..b6e3b04c8 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt
@@ -31,6 +31,7 @@ import android.content.Intent
 import android.content.pm.ActivityInfo
 import android.content.res.Configuration
 import android.os.Build
+import android.view.Gravity
 import android.view.View
 import android.view.ViewGroup
 import android.view.WindowManager
@@ -152,10 +153,10 @@ class VideoPlayerOverlayDelegate (private val player: VideoPlayerActivity) {
              val halfScreenSize = player.getScreenWidth() - foldingFeature.bounds.right
              arrayOf(playerUiContainer, hudBackground, hudRightBackground, playlistContainer).forEach {
                  it?.let { view ->
-                     val lp = (view.layoutParams as RelativeLayout.LayoutParams)
+                     val lp = (view.layoutParams as FrameLayout.LayoutParams)
                      lp.width = halfScreenSize
-                     lp.addRule(if (onRight) RelativeLayout.ALIGN_PARENT_RIGHT else RelativeLayout.ALIGN_PARENT_LEFT)
-                     lp.removeRule(if (onRight) RelativeLayout.ALIGN_PARENT_LEFT else RelativeLayout.ALIGN_PARENT_RIGHT)
+                     lp.gravity = lp.gravity or (if (onRight) Gravity.END else Gravity.START)
+                     lp.gravity = lp.gravity and (if (onRight) Gravity.END else Gravity.START).inv()
                      view.layoutParams = lp
                  }
              }
@@ -163,16 +164,16 @@ class VideoPlayerOverlayDelegate (private val player: VideoPlayerActivity) {
          } else {
              //device is separated and half opened. We display the controls on the bottom half and the video on the top half
              if (foldingFeature.state == FoldingFeature.State.HALF_OPENED) {
-                 val videoLayoutLP = (player.videoLayout!!.layoutParams as RelativeLayout.LayoutParams)
+                 val videoLayoutLP = (player.videoLayout!!.layoutParams as ViewGroup.LayoutParams)
                  val halfScreenSize = foldingFeature.bounds.top
                  videoLayoutLP.height = halfScreenSize
                  player.videoLayout!!.layoutParams = videoLayoutLP
                  player.findViewById<FrameLayout>(R.id.player_surface_frame).children.forEach { it.requestLayout() }
 
                  arrayOf(playerUiContainer, playlistContainer).forEach {
-                     val lp = (it.layoutParams as RelativeLayout.LayoutParams)
+                     val lp = (it.layoutParams as FrameLayout.LayoutParams)
                      lp.height = halfScreenSize
-                     lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
+                     lp.gravity = lp.gravity or Gravity.BOTTOM
                      it.layoutParams = lp
                  }
                  arrayOf(hudBackground, hudRightBackground).forEach {
@@ -201,15 +202,14 @@ class VideoPlayerOverlayDelegate (private val player: VideoPlayerActivity) {
     private fun resetHingeLayout() {
         arrayOf(playerUiContainer, hudBackground, hudRightBackground, playlistContainer).forEach {
             it?.let { view ->
-                val lp = (view.layoutParams as RelativeLayout.LayoutParams)
-                lp.width = RelativeLayout.LayoutParams.MATCH_PARENT
+                val lp = (view.layoutParams as ViewGroup.LayoutParams)
+                lp.width = ViewGroup.LayoutParams.MATCH_PARENT
                 view.layoutParams = lp
             }
         }
         arrayOf(playerUiContainer, playlistContainer).forEach {
-            val lp = (it.layoutParams as RelativeLayout.LayoutParams)
-            lp.height = RelativeLayout.LayoutParams.MATCH_PARENT
-            lp.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
+            val lp = (it.layoutParams as ViewGroup.LayoutParams)
+            lp.height = ViewGroup.LayoutParams.MATCH_PARENT
             it.layoutParams = lp
         }
         if (::hudBinding.isInitialized) arrayOf(hudBackground, hudRightBackground).forEach {
@@ -217,8 +217,8 @@ class VideoPlayerOverlayDelegate (private val player: VideoPlayerActivity) {
         }
         hingeArrowLeft?.visibility = View.GONE
         hingeArrowRight?.visibility = View.GONE
-        val lp = (player.videoLayout!!.layoutParams as RelativeLayout.LayoutParams)
-        lp.height = RelativeLayout.LayoutParams.MATCH_PARENT
+        val lp = (player.videoLayout!!.layoutParams as ViewGroup.LayoutParams)
+        lp.height = ViewGroup.LayoutParams.MATCH_PARENT
         player.videoLayout!!.layoutParams = lp
         player.findViewById<FrameLayout>(R.id.player_surface_frame).children.forEach { it.requestLayout() }
     }



More information about the Android mailing list