android - 如何在 android 中使用自定义 View 减少复杂布局的布局加载时间?

标签 android performance

我有一个 Activity 和一个 fragment 。 fragment 将在 Activity 中动态加载的位置。 Fragment 有大约 20 个 View ,其中大部分是自定义 View 。加载 fragment 大约需要 3 秒,这次只是加载布局,没有任何初始化或功能。我觉得 3 秒更多,想减少到 1 秒。实现这一目标的可能方法是什么。 我做的事情: ** 删除了权重和嵌套布局。 ** 删除了未使用的 View 属性 ** 扁平化布局层次结构。 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gauge="http://schemas.android.com/apk/res/com.test.app.testApp"
xmlns:customview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_light_grey" >

<RelativeLayout
    android:id="@+id/top_components_dummy"
    android:layout_width="match_parent"
    android:layout_height="135dp"
    android:orientation="horizontal" >
    <RelativeLayout
        android:id="@+id/ dummy_layout"
        android:layout_width="370dp"
        android:layout_toLeftOf="@+id/host_layout"
        android:layout_height="match_parent" >
        <LinearLayout
            android:id="@+id/dummy_parent"
            android:layout_width="280dp"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/water_and_drain_tactile"
            android:orientation="horizontal" >
            <FrameLayout
                android:id="@+id/dummy_parent_water"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="20dp" >
                <com.test.app.ui.components.CustomView
                    android:id="@+id/level_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="16dp"
                    android:src="@drawable/xyz" />
                <ImageView
                    android:layout_width="85dp"
                    android:layout_height="4dp"
                    android:layout_gravity="center|bottom"
                    android:layout_marginBottom="20dp"
                    android:background="@drawable/empty_indiactor"
                    android:contentDescription="@string/empty_string"
                    android:visibility="gone" />
            </FrameLayout>
            <FrameLayout
                android:id="@+id/dummy_parent_drain"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="40dp" >
                <com.test.app.ui.components.CustomView
                    android:id="@+id/drain_level_indicator"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="45dp"
                    android:src="@drawable/xyz" />

                <ImageView
                    android:layout_width="85dp"
                    android:layout_height="4dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="20dp"
                    android:background="@drawable/empty_indiactor"
                    android:contentDescription="@string/empty_string"
                    android:visibility="gone" />
            </FrameLayout>
        </LinearLayout>
        <TextView
            android:id="@+id/Waterdummy_TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginRight="30dp"
            android:layout_marginTop="10dp"
            android:layout_toLeftOf="@+id/dummyCenter"
            android:text="Water"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000"
            android:textStyle="bold" />
        <View
            android:id="@+id/dummyCenter"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:background="#00ffffff" />
        <TextView
            android:id="@+id/Draindummy_TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/dummyCenter"
            android:text="Drain"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000"
            android:textStyle="bold" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/host_layout"
        android:layout_width="400dp"
        android:layout_centerHorizontal="true"
        android:layout_height="match_parent" >
        <ImageView
            android:id="@+id/host_Stop_Background"
            android:layout_width="120dp"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/xyz"
            android:contentDescription="@string/empty_string" />
        <ImageView
            android:id="@+id/dull_stop_icon"
            android:layout_width="28dp"
            android:layout_height="28dp"
            android:layout_above="@+id/host_stop_text"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:contentDescription="@string/empty_string"
            android:scaleType="fitXY"
            android:src="@drawable/xyz" />
        <TextView
            android:id="@+id/host_stop_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="15dp"
            android:text="STOP"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000" />

        <RelativeLayout
            android:id="@+id/host_Rate_Layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/host_Stop_Background" >
            <ImageView
                android:id="@+id/host_Rate_Icon"
                android:layout_width="120dp"
                android:layout_height="80dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/xyz"
                android:contentDescription="@string/empty_string" />
            <TextView
                android:id="@+id/host_rate_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/host_rate_unit_textView"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="-5dp"
                android:gravity="bottom|center_horizontal"
                android:text="1.5"
                android:textAlignment="gravity"
                android:textColor="#000000"
                android:textSize="34sp" />
        </RelativeLayout>
        <ImageView
            android:id="@+id/host_system_Icon"
            android:layout_width="120dp"
            android:layout_height="80dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_toRightOf="@+id/host_Stop_Background"
            android:background="@drawable/host_system_top_tactile"
            android:contentDescription="@string/empty_string"
            android:paddingBottom="18dp"
            android:paddingTop="18dp"
            android:scaleType="fitCenter"
            android:src="@drawable/xyz" />
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/dull_rate_layout"
        android:layout_width="350dp"
        android:layout_toRightOf="@+id/host_layout"
        android:layout_height="match_parent" >
        <RelativeLayout
            android:id="@+id/dull_rate_layout"
            android:layout_width="175dp"
            android:layout_height="match_parent" >
            <TextView
                android:id="@+id/dull_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="@string/dull_rate"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="130dp"
                android:layout_height="80dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/xyz" />
            <TextView
                android:id="@+id/dull_rate_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/dull_rate_unit_textView"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="-5dp"
                android:gravity="bottom|center_horizontal"
                android:text="500"
                android:textAlignment="gravity"
                android:textColor="#000000"
                android:textSize="36sp" />
            <TextView
                android:id="@+id/dull_rate_unit_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="15dp"
                android:text="mL/min"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#000000" />
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/salad_layout"
            android:layout_width="175dp"
            android:layout_height="match_parent"
            android:layout_toRightOf="@+id/dull_rate_layout" >
            <TextView
                android:id="@+id/salad_textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="@string/salad"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000"
                android:textStyle="bold" />
            <ImageView
                android:id="@+id/salad_Icon"
                android:layout_width="130dp"
                android:layout_height="80dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/dull_salad_tactile"
                android:contentDescription="@string/empty_string"
                android:paddingBottom="18dp"
                android:paddingTop="18dp"
                android:scaleType="fitCenter"
                android:src="@drawable/xyz" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
<RelativeLayout
    android:id="@+id/midLinear"
    android:layout_width="match_parent"
    android:layout_height="460dp"
    android:layout_below="@+id/top_components_dummy" >
    <TextView
        android:id="@+id/boomSpeed_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="@string/boom_speed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/boomSpeed_Value_Text"
        android:layout_width="wrap_content"
        android:layout_height="75dp"
        android:layout_below="@+id/boomSpeed_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-5dp"
        android:gravity="center"
        android:maxLength="3"
        android:text="300"
        android:textColor="@android:color/black"
        android:textSize="50sp"
        android:textStyle="normal" />
    <ImageView
        android:id="@+id/imageView_down"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignBottom="@+id/boomSpeed_Value_Text"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/boomSpeed_Value_Text"
        android:scaleType="fitXY"
        android:src="@drawable/xyz" />
    <ImageView
        android:id="@+id/imageView_up"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_alignBottom="@+id/boomSpeed_Value_Text"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/boomSpeed_Value_Text"
        android:src="@drawable/xyz" />
    <TextView
        android:id="@+id/boom_Speed_Value_Unit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/boomSpeed_Value_Text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="-10dp"
        android:text="mL/min"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#444444" />
    <ImageView
        android:id="@+id/IV_boomSpeed"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_below="@+id/boom_Speed_Value_Unit"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:hapticFeedbackEnabled="true"
        android:src="@drawable/xyz" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:layout_toLeftOf="@+id/IV_boomSpeed"
        android:gravity="center_vertical" >
        <ImageView
            android:layout_width="380dip"
            android:layout_height="380dip"
            android:layout_centerInParent="true"
            android:src="@drawable/xyz" />
        <com.test.app.ui.components.CustomView1
            android:id="@+id/circularprogressbar2"
            android:layout_width="345dip"
            android:layout_height="345dip"
            android:layout_centerInParent="true"
            customview:cpb_backgroundColor="#EDEDED"
            customview:cpb_borderColor="#22B7B3"
            customview:cpb_maintitle="@string/time_remaining"
            customview:cpb_progressColor="#22B7B3" />
        <RelativeLayout
            android:id="@+id/clickablearea"
            android:layout_width="180dip"
            android:layout_height="80dip"
            android:layout_alignTop="@+id/circularprogressbar2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="100dip"
            android:background="#00fff000" >
        </RelativeLayout>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:layout_toRightOf="@+id/IV_boomSpeed"
        android:gravity="center_vertical" >
        <ImageView
            android:layout_width="380dip"
            android:layout_height="380dip"
            android:layout_centerInParent="true"
            android:src="@drawable/dialsbg" />
        <ImageView
            android:id="@+id/IV_StopML"
            android:layout_width="120dip"
            android:layout_height="80dip"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="54dp"
            android:src="@drawable/xyz" />
        <TextView
            android:layout_width="100dip"
            android:layout_height="80dip"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="40dp"
            android:layout_marginRight="15dp"
            android:gravity="center"
            android:text="STOP\n ML"
            android:textColor="@android:color/holo_red_dark"
            android:textStyle="bold" />
        <com.test.app.ui.components.CustomView1
            android:id="@+id/circularprogressbar3"
            android:layout_width="345dip"
            android:layout_height="345dip"
            android:layout_centerInParent="true"
            customview:cpb_borderColor="#E7AB0F"
            customview:cpb_progressColor="#e2a000"
            customview:backgroundColor="#EDEDED"
            customview:goal="@string/goal"
            customview:goal_unit="mL"
            customview:goal_value="2800"
            customview:ml_unit="mL"
            customview:rate="@string/rate"
            customview:rate_unit="mL/hr"
            customview:rate_value="700"
            customview:removed_title="@string/removed"
            customview:removed_value="1500" />
        <RelativeLayout
            android:id="@+id/editMLGoal_clickArea"
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_alignStart="@+id/circularprogressbar3"
            android:layout_marginLeft="60dip"
            android:layout_marginTop="210dip"
            android:background="#00fff000" >
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/editMLRate_clickArea"
            android:layout_width="100dip"
            android:layout_height="50dip"
            android:layout_alignEnd="@+id/circularprogressbar3"
            android:layout_marginRight="60dip"
            android:layout_marginTop="210dip"
            android:background="#00fff000" >
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
<RelativeLayout
    android:id="@+id/RL_DialView2"
    android:layout_width="300dp"
    android:layout_height="190dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginBottom="-40dp" >
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="170dp"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:scaleType="fitXY"
        android:src="@drawable/xyz" />
    <com.test.app.ui.components.CustomView2
        android:id="@+id/gauge_view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        gauge:isFloat="false"
        gauge:scaleEndValue="400"
        gauge:scaleMaxLimit="350"
        gauge:scaleMinLimit="150"
        gauge:scaleStartValue="70"
        gauge:showLimitText="true"
        gauge:showNeedle="true"
        gauge:showRanges="true"
        gauge:showScale="false"
        gauge:showText="true"
        gauge:textTitle="@string/ven"
        gauge:textUnit="mmHg"
        gauge:textUnitColor="#000000"
        gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
    android:id="@+id/RL_DialView1"
    android:layout_width="220dp"
    android:layout_height="140dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="center"
    android:layout_marginBottom="-20dp"
    android:layout_toLeftOf="@+id/RL_DialView2" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/xyz" />
    <com.test.app.ui.components.CustomView2
        android:id="@+id/gauge_view1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        gauge:isFloat="false"
        gauge:scaleEndValue="500"
        gauge:scaleMaxLimit="400"
        gauge:scaleMinLimit="-200"
        gauge:scaleStartValue="-300"
        gauge:showLimitText="true"
        gauge:showNeedle="true"
        gauge:showRanges="true"
        gauge:showScale="false"
        gauge:showText="true"
        gauge:textTitle="@string/art"
        gauge:textUnit="mmHg"
        gauge:textUnitColor="#000000"
        gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
    android:id="@+id/RL_DialView4"
    android:layout_width="220dp"
    android:layout_height="140dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="center"
    android:layout_marginBottom="-20dp"
    android:layout_toRightOf="@+id/RL_DialView2" >
    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/xyz" />
    <com.test.app.ui.components.CustomView2
        android:id="@+id/gauge_view4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        gauge:isFloat="true"
        gauge:scaleEndValue="40"
        gauge:scaleMaxLimit="38"
        gauge:scaleMinLimit="36"
        gauge:scaleStartValue="34"
        gauge:showLimitText="true"
        gauge:showNeedle="true"
        gauge:showRanges="true"
        gauge:showScale="false"
        gauge:showText="true"
        gauge:textTitle="@string/dt"
        gauge:textUnit="\u2103"
        gauge:textUnitColor="#000000"
        gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
    android:id="@+id/RL_DialView3"
    android:layout_width="220dp"
    android:layout_height="140dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="center"
    android:layout_marginBottom="-20dp"
    android:layout_toLeftOf="@+id/RL_DialView1" >
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/xyz" />
    <com.test.app.ui.components.CustomView2
        android:id="@+id/gauge_view3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        gauge:isFloat="false"
        gauge:scaleEndValue="320"
        gauge:scaleMaxLimit="300"
        gauge:scaleMinLimit="90"
        gauge:scaleStartValue="70"
        gauge:showLimitText="true"
        gauge:showNeedle="true"
        gauge:showRanges="true"
        gauge:showScale="false"
        gauge:showText="true"
        gauge:textTitle="@string/tmp"
        gauge:textUnit="mmHg"
        gauge:textUnitColor="#000000"
        gauge:textValueColor="#000000" />
</RelativeLayout>
<RelativeLayout
    android:id="@+id/RL_DialView5"
    android:layout_width="220dp"
    android:layout_height="140dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="center"
    android:layout_marginBottom="-20dp"
    android:layout_toRightOf="@+id/RL_DialView4" >
    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/xyz" />
    <com.test.app.ui.components.CustomView2
        android:id="@+id/gauge_view5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        gauge:isFloat="true"
        gauge:scaleEndValue="17"
        gauge:scaleMaxLimit="15.9"
        gauge:scaleMinLimit="13.1"
        gauge:scaleStartValue="12"
        gauge:showInnerRim="false"
        gauge:showNeedle="true"
        gauge:showRanges="true"
        gauge:showScale="false"
        gauge:showText="true"
        gauge:textTitle="@string/cond"
        gauge:textUnit="mS/cm"
        gauge:textUnitColor="#000000"
        gauge:textValueColor="#000000" />
</RelativeLayout>

最佳答案

您可以查看 Layout Optimizating指南:

  1. 避免嵌套 ViewGroup。您应该使您的 Layout 尽可能低层级

Using nested instances of LinearLayout can lead to an excessively deep view hierarchy. Furthermore, nesting several instances of LinearLayout that use the layout_weight parameter can be especially expensive as each child needs to be measured twice. This is particularly important when the layout is inflated repeatedly, such as when used in a ListView or GridView.

  1. 在您的布局文件上运行 lint 工具以搜索可能的 View 层次结构优化始终是一个好习惯。

  2. 使用ViewStub:

Sometimes your layout might require complex views that are rarely used. Whether they are item details, progress indicators, or undo messages, you can reduce memory usage and speed up rendering by loading the views only when they are needed

关于android - 如何在 android 中使用自定义 View 减少复杂布局的布局加载时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36395623/

相关文章:

java - 使用 JSON 从 API 获取数据并将两个对象存储在数组适配器的单行中

android - View.getLocationOnScreen 无法正常工作

java - 在 Java Swing 中为特定组件设置 Tooltip 延迟时间

performance - 为什么 float 除法很慢?

css - 将 CSS 重置为默认状态

asp.net - 为什么 IIS 比 ASP.NET 开发服务器慢?

performance - 最长公共(public)子序列算法调试性能瓶颈

android - 以编程方式创建按钮或在 xml 中创建按钮之间是否存在性能差异?

android 日历事件每年重复重复规则

android - (Android Repository Pattern) 使用从 web 删除的项目更新电话数据库数据