Android ViewFlipper内存不足错误



我正在使用ViewFlipper,我的ViewFlipper工作正常,直到我增加了图片的高度。

它给了我OutOfMemoryError。

这是我的XML代码,用于包含viewFlipper:的活动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.taktaz.activities.OneProductActivity"
>
<ViewFlipper
    android:id="@+id/flipper1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="450px"
    android:clipChildren="true"
    android:autoStart="true"
    android:flipInterval="4000"
    android:inAnimation="@anim/right_in"
    android:outAnimation="@anim/left_out"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true">

</ViewFlipper>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:layout_margin="4dp"
    >
    <Button
        android:id="@+id/btn_productDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_specifics_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_productCatalog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_catalog_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_productManual"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_manual_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_productWebsite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_product_website_style"
        android:layout_marginBottom="1dp"
        android:layout_weight="1"
        />
</LinearLayout>

在这里我设置了视图Flipper ImageResources:

 private void setUpFlipper() {
    TypedArray imgResource = getResources().obtainTypedArray(R.array.hayabusa_imgs);
    mFlipper = (ViewFlipper) findViewById(R.id.flipper1);
    for (int i = 0 ; i < imgResource.length() ; i++)
    {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(imgResource.getResourceId(i,-1));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setAdjustViewBounds(true);
        mFlipper.addView(imageView);
    }
}

我没有使用位图。

当我删除按钮时,viewFlipper可以正常工作,但当我将这4个按钮放在flipper下面时,它就不工作了,并将以OutOfMemoryError关闭。

我该怎么办?

这是android中图像的常见问题:

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

你消费android限制,这就是为什么你看到这个错误。重新缩放图像,不要以原始大小显示。

ViewFlipper加载内存中的所有视图。在内存中加载所有图像是个坏主意。看看下面的问题在android中的视图翻转器中有多少视图是可能的?

由于不赞成使用Gallery,请使用ViewPager。ViewPager在支持库中可用。您还可以配置应在前后两个方向上加载的视图数量。您可以根据需要加载图像,也可以销毁当前不需要的图像。它允许您重用视图,并在内存中只加载几个视图:

http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/

在这里,您可以找到一些如何实现它的好例子:Android Viewpager作为图像幻灯片库

最新更新