与背景图像的慢scrollview

时间:2016-05-03 08:31:23

标签: android xml

如何使用scroolview为布局设置背景图像,因为当我将imageView添加到下面的代码时,scroolview开始工作非常慢。图像有700kb。如果我将android:background="@drawable/background04"设置为relativeLayout scroolview也会非常慢。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.scrollview.MainActivity">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:src="@drawable/background04"
    android:alpha="0.8"
    android:layout_centerHorizontal="true" />

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" >

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView2" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView3" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView4" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView5" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView6" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView7" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView8" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView9" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView10" />

    </LinearLayout>

</ScrollView>

修改

为了测试我使用htc desire 820。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aapps.scrollview">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

logcat的:

05-03 10:48:04.750 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): bitmap = 0x55a971acb0
05-03 10:48:04.750 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): pixelRef ID = 415
05-03 10:48:05.250 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): bitmap = 0x55a971acb0
05-03 10:48:05.250 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): pixelRef ID = 415
05-03 10:48:05.850 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): bitmap = 0x55a971acb0
05-03 10:48:05.850 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): pixelRef ID = 415
05-03 10:48:06.330 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): bitmap = 0x55a971acb0
05-03 10:48:06.330 17843-17891/com.aapps.scrollview E/OpenGLRenderer: CacheDebug: generateTexture() in TextureCache::get(, if (!texture): pixelRef ID = 415
05-03 10:48:06.840 17843-17843/com.aapps.scrollview I/Choreographer: Skipped 30 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:0)

将背景放在父布局容器

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background04"
tools:context="com.example.scrollview.MainActivity">

并删除ImageView,因为它不再需要

您的代码中的另一个提示是,您使用的alignParent太多了,当您使匹配父级的宽度和高度时,您可以获得相同的输出,并删除所有的alignParent属性。见下面的代码更改

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:src="@drawable/background04"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.scrollview.MainActivity">

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

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView2" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView3" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView4" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView5" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView6" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView7" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView8" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView9" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:id="@+id/textView10" />

        </LinearLayout>

    </ScrollView>
</RelativeLayout>