0

我正在尝试用 5 张图片做一个 Horizo​​ntalScrollView。我像这样实现了xml

  <?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

日志猫

      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)
4

2 回答 2

0

您的图像占用了太多内存,我遇到了同样的问题,为我解决的问题是按照此处找到的指南进行操作:

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

它包含 4 个关于如何在不耗尽内存的情况下以最佳方式显示位图的教程。这可能有点令人困惑,但它确实很好用。

另一方面,如果你想要一个快捷方式,你总是可以在 GIMP 或其他东西中手动缩小图像。尽管如果用户将为您的应用选择自己的图片,这并不能解决长期问题。

于 2012-09-28T14:36:20.707 回答
0

使用可以肯定工作的较低分辨率的图像......例如图像尺寸
width=320 by height=320width=200 by height=200

于 2012-12-29T09:21:53.793 回答