2012-09-27 179 views
0

我想用5張圖片做Horizo​​ntalScrollView。 我實現了這樣的XMLHorizo​​ntalScrollView OutOfMemoryError

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gallery" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:spacing="40dp" 
     android:scrollbars="horizontal" 
     android:fadeScrollbars="false" 
     android:layout_marginBottom="50dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background"/> 

</HorizontalScrollView> 

和代碼是這樣的

package com.infobest.praiser.activity; 

import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.widget.HorizontalScrollView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

import com.infobest.praiser.R; 
import com.infobest.praiser.actionbar.ActionBarActivity; 

public class TutorialActivity extends ActionBarActivity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tutorial); 
     Integer[] mImageIds = {R.drawable.tutorial1, R.drawable.tutorial2, R.drawable.tutorial3, 
      R.drawable.tutorial4, R.drawable.tutorial5}; 
     HorizontalScrollView h = (HorizontalScrollView) this.findViewById(R.id.gallery); 
     LinearLayout topLinearLayout = new LinearLayout(this); 
     topLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 


     for (int i = 0; i < 5; i++) 
     { 
      final ImageView imageView = new ImageView(this); 

      imageView.setImageResource(mImageIds[i]); 
      topLinearLayout.addView(imageView); 

     } 
     h.addView(topLinearLayout); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.menu_tutorial, menu); 

     // Calling super after populating the menu is necessary here to ensure 
     // that the 
     // action bar helpers have a chance to handle this event. 
     return super.onCreateOptionsMenu(menu); 
    } 


} 

我究竟在做什麼錯的,因爲我得到的OutOfMemoryError

logcat的

 FATAL EXCEPTION: main java.lang.OutOfMemoryError  at 
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at 
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:483) 
    at 
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351) 
    at 
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773) 
    at android.content.res.Resources.loadDrawable(Resources.java:1937) 
    at android.content.res.Resources.getDrawable(Resources.java:664) at 
android.widget.ImageView.resolveUri(ImageView.java:542) at 
android.widget.ImageView.setImageResource(ImageView.java:315)  at 
com.infobest.praiser.activity.TutorialActivity.onCreate(TutorialActivity.java:30) 
    at android.app.Activity.performCreate(Activity.java:4465) at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 
    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 
    at android.app.ActivityThread.access$600(ActivityThread.java:122) 
    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 
    at android.os.Handler.dispatchMessage(Handler.java:99)  at 
android.os.Looper.loop(Looper.java:137) at 
android.app.ActivityThread.main(ActivityThread.java:4340)  at 
java.lang.reflect.Method.invokeNative(Native Method) at 
java.lang.reflect.Method.invoke(Method.java:511) at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)  at 
dalvik.system.NativeStart.main(Native Method) 
+0

什麼尺寸的圖像,你要添加? – Michaeldcooney

+0

800 x 850.如果這是原因,我還有什麼可以使用,知道該畫廊已棄用:P – gabi906

回答

0

你的圖像佔用太多內存,我有同樣的問題和內容固定它,我是跟着導遊在這裏找到:

http://developer.android.com/training/displaying-bitmaps/index.html

它包含如何最佳顯示位圖不會耗盡內存4個教程。它可能有點令人困惑,但它運作得很好。另一方面,如果你想要一個快捷方式,你可以隨時在GIMP中手動縮放圖像或其他東西。儘管如果用戶將爲您的應用選擇自己的圖片,這並不能解決長期的問題。

0

使用較低分辨率的圖像,將definatelly工作... 例如圖像尺寸
width=320通過height=320width=200通過height=200