我正在使用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>
的TextView
和HorizontalScrollView
塊使用大約15倍於我的佈局,並與這個設計我有一個OutOfMemoryError異常。
當我使用較少的塊時,錯誤不會出現。
我試圖儘量減少我的PNG文件的大小,但這並不能解決問題。
是否有任何其他佈局我可以使用並不是所有的ImageView
在應用程序的開始加載?
使用picasso庫在您的佈局中顯示圖像 –