【问题标题】:Android ViewFlipper OutOfMemoryErrorAndroid ViewFlipper OutOfMemoryError
【发布时间】:2015-11-27 10:34:50
【问题描述】:

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

它给了我 OutOfMemoryError。

这是包含 viewFlipper 的活动的 XML 代码:

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

在这里我设置了 viewFlipper 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 个按钮放在脚蹼下方时,它不起作用,并且会因 OutOfMemoryError 而关闭。

我该怎么办?

【问题讨论】:

  • 您的布局不太可能导致 OOM 错误。这很可能是由您未发布的代码引起的。您还应该发布堆栈跟踪,否则任何人都只能猜测。另请参阅stackoverflow.com/help/how-to-ask
  • 简单的解决方案:在清单上使用 LargeHeap。

标签: android out-of-memory slideshow viewflipper


【解决方案1】:

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

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

您消耗了 android 限制,这就是您看到此错误的原因。重新缩放图像,不要以原始尺寸显示它们。

ViewFlipper 将所有视图加载到内存中。将所有图像加载到内存中是个坏主意。 看看下面的问题How many Views are possible in a view flipper in android?

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

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

在这里您可以找到一些如何实现它的好例子: Android Viewpager as Image Slide Gallery

【讨论】:

  • ViewPager 是从哪里来的??
  • @Kuffs 你是对的,对不起。我将 viewflipper 与 viewpager 混淆了。
  • 但我没有使用位图。我也想至少在尺寸上显示图像:600*420(我将图像尺寸设置为此宽度和高度)。我还缩小了每张图片的大小,每张图片大约 70KB。
  • @Ali_kabir 我已经更新了我的答案。希望它会有所帮助。
猜你喜欢
  • 2011-06-16
  • 2011-09-05
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
相关资源
最近更新 更多