Android糟糕的滚动性能

时间:2017-01-19 22:02:20

标签: android performance scrollview

我在Android活动中遇到滚动性能很差的问题。 ScrollView包含许多带图像按钮的LinearLayouts。图像重量低于50KB。我该如何解决?

活动布局:

<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".categories.AllSoundsActivity"
android:orientation="vertical"
android:background="@drawable/main_menu"
android:layout_alignParentEnd="false"
app:layout_scrollFlags="scroll"
android:clipToPadding="false"
android:clipChildren="false"
android:clickable="false"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_centerVertical="false"
android:soundEffectsEnabled="true"
android:verticalScrollbarPosition="right"
android:visibility="visible"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="false"
android:layout_alignParentStart="false"
android:fadeScrollbars="true"
android:baselineAligned="false"
android:addStatesFromChildren="false">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <!-- This is the 'faux' ToolBar I've been telling you about. This is the part that will be overlaid by the content view below. -->

    <FrameLayout
        android:id="@+id/v_toolbar_extension"
        android:layout_width="match_parent"
        android:layout_height="78dp"
        android:layout_below="@+id/tb_toolbar"
        android:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_gravity="top"
            android:layout_height="match_parent"
            android:minHeight="0dp"
            android:gravity="right"
            android:background="@drawable/toolbar_image">


            <ImageButton
                android:src="@drawable/buton_favourite_image"
                android:layout_width="55dp"
                android:layout_height="53dp"
                android:layout_gravity="right"
                android:layout_marginRight="8dp"
                android:background="?selectableItemBackground"
                android:id="@+id/favouriteButton"
                android:scaleType="centerInside"
                android:elevation="24dp"
                android:visibility="visible" />

            <ImageButton
                android:src="@drawable/main_menu_icon_image"
                android:layout_width="51dp"
                android:layout_height="61dp"
                android:layout_gravity="right"
                android:layout_marginRight="8dp"
                android:background="?selectableItemBackground"
                android:id="@+id/mainMenuButton"
                android:scaleType="centerInside"
                android:elevation="24dp"
                android:visibility="visible" />


        </android.support.v7.widget.Toolbar>
    </FrameLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollview">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:drawingCacheQuality="auto"
                android:alwaysDrawnWithCache="true"
                android:persistentDrawingCache="all">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button2"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button2"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button2"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button3"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button3"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button3"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button4"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button4"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button4"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button5"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button5"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button5"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button6"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button6"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button6"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button7"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button7"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button7"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button8"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button8"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button8"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false"
                android:clipToPadding="false"
                android:contextClickable="false"
                android:gravity="center"
                android:weightSum="1">

                <ImageButton
                    android:src="@drawable/share_button"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/share_button9"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="0.18"/>

                <ImageButton
                    android:src="@drawable/example_sound"
                    android:layout_width="276dp"
                    android:layout_height="130dp"
                    android:background="?selectableItemBackground"
                    android:id="@+id/all_sounds_button9"
                    android:scaleType="centerInside"
                    android:cropToPadding="false"
                    android:layout_weight="0.44" />

                <ImageButton
                    android:src="@drawable/unliked"
                    android:layout_width="48dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:background="?selectableItemBackground"
                    android:id="@+id/unliked_button9"
                    android:scaleType="centerInside"
                    android:visibility="visible"
                    android:layout_marginRight="30dp"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.18" />

            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

问题是许多嵌套的LinearLayouts必须反复绘制。看一下使用Android的hierarchyviewer,你会看到我的意思。

我建议你做的是取消ScrollView并用ListView替换它。通过这样做,Android将能够使用ImageButtons重新使用LinearLayout,而不必每次都重新绘制,因此可以使滚动更加平滑。