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也解决了我的应用程序中的一些内存问题。