安卓中ScrollView滚动缓慢问题

3
我有一个包含一些图片视图的ScrollView。然而,滚动非常缓慢,我不知道性能差的原因是什么。是否有任何方法可以加快滚动速度?或者这个问题可能是由什么原因引起的?
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                 <ImageView />

        <ImageView />

         <ImageView />

         <ImageView />

         <ImageView />

                <ImageView />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:gravity="center"
                android:orientation="vertical">

                <Button />

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>`

除了不必要的嵌套“RelativeLayout”之外,似乎没有任何可能引起问题的东西。你所说的“缓慢”,是指它在滚动时真正地滚动得很慢,还是指卡顿和滚动渲染时的问题?你加载的图片有多大?如果你正在加载大图像,可以尝试减小它们的文件大小。 - CTheDark
3个回答

4

问题出在图片的大小上。 因此,如果您在使用ScrollView和多个ImageView时遇到性能问题,请注意您的图片大小!在我的情况下,我将每张图片的大小缩小到大约20KB-50KB。


我有20张大小在17-25 KB之间的图片。为什么会出现卡顿? - Emon Hossain Munna

0

我英语不是很流利,但我会尽力向您解释!

您只需尝试像这样更改滚动视图位置

 **<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

             <ImageView />

    <ImageView />

     <ImageView />

     <ImageView />

     <ImageView />

            <ImageView />

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:orientation="vertical">

            <Button />

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


谢谢,我会尝试的。 - Vera
很遗憾,它没有帮助。 - Vera

0

如果你在子视图中有更多的子视图,系统的负担就会更大。我不明白为什么你会在父RelativeLayout中使用一个 RelativeLayout。你只发布了一小部分代码,但我看到你有太多子布局。

你可以在此处找到更多信息

这应该像这样,或者用RelativeLayout取代LinearLayout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="100dp" />
</LinearLayout>


即使我删除了所有的RelativeLayout,只剩下ScrollView和一个RelativeLayout,它仍然非常缓慢。 - Vera
为什么你有空的“ImageView”标签? - Mihai
我只是为了举例,删除了ImageView内部的代码。 - Vera

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接