0

我正在使用NestedScrollView作爲我的佈局來顯示TextView作爲標題與HorizontalScrollView以顯示可滑動的圖片。
下面是一個例子:Android:如何解決OutOfMemoryError

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:scrollbars="none" 
    android:layout_weight="1"> 


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


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:textStyle="bold" 
      android:text="Cable Crunches" 
      android:id="@+id/cableCrunches" 
      android:gravity="center" 
      android:padding="3dp" 
      android:textSize="18sp"/> 

     <HorizontalScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/cableCrunches" 
      android:id="@+id/cableCrunchesPics"> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/cableCrunchesPics1" 
        android:src="@drawable/cable_crunches_1"/> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/cableCrunchesPics2" 
        android:src="@drawable/cable_crunches_2"/> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/cableCrunchesPics3" 
        android:src="@drawable/cable_crunches_3"/> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/cableCrunchesPics4" 
        android:src="@drawable/cable_crunches_4"/> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/cableCrunchesPics5" 
        android:src="@drawable/cable_crunches_5"/> 

      </LinearLayout> 
     </HorizontalScrollView> 

    </RelativeLayout> 

</android.support.v4.widget.NestedScrollView> 

TextViewHorizontalScrollView塊使用大約15倍於我的佈局,並與這個設計我有一個OutOfMemoryError異常。
當我使用較少的塊時,錯誤不會出現。

我試圖儘量減少我的PNG文件的大小,但這並不能解決問題。
是否有任何其他佈局我可以使用並不是所有的ImageView在應用程序的開始加載?

+0

使用picasso庫在您的佈局中顯示圖像 –

回答

2

您需要使用RecyclerViewListViewImageView作爲行項目而不是HorizontalScrollViewRecyclerView將創建足夠的ImageView s來填充屏幕。當用戶滾動它將重用現有的ImageView's。

還可以考慮使用圖像加載庫異步加載圖像。 GlidePicasso是非常好的圖書館。

+0

也有很好的教程:[picasso](http://www.101apps.co.za/index.php/articles/android-recyclerview-and-picasso-tutorial.html)[glide](http:/ /blog.grafixartist.com/image-gallery-app-android-studio-1-4-glide/) –

+0

謝謝我現在使用RecyclerView! :D –

+0

@LarsH。不用謝。如果此答案解決了您的問題,則可以使用答案分數下方的複選標記來接受此問題。 – LordRaydenMK

0

加入這一行中的表現在應用程序標籤

android:largeHeap="true" 

也許這將幫助你

而且是像其他人說。您可以使用不同的庫來加載圖像。我會推薦Universal image Loader。 Link

+0

這並不能解決OutOfMemoryError錯誤,這意味着它需要一段時間才能遇到相同的問題! – Zain

0

您可以使用庫來加載圖像。我最喜歡的是Glide 它使用起來很簡單https://github.com/bumptech/glide

所以你會以編程方式加載你的圖片。您還可以更好地控制加載圖像的方式,時間和位置。 Glide也解決了我的應用程序中的一些內存問題。