2016-05-03 83 views
1

我應該如何爲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. 
+0

嘗試添加該機器人:largeHeap =在你的應用程序標籤「真」清單中 –

+0

和閱讀本http://developer.android.com/training/displaying-bitmaps/cache-bitmap .html –

回答

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> 
+0

和關閉父母的佈局容器與'' – hrskrs

+0

@ hrskrs ofcourse,這是不需要說的了ahahaha –

+0

正如我所說的問題,如果我這樣做也工作緩慢。 –