我正在使用 ViewFlipper,我的 viewFlipper 工作正常,直到我增加了图片的高度。
它给我 OutOfMemoryError。
这是我的包含 viewFlipper 的 Activity 的 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 而关闭。
我该怎么办?
最佳答案
这是 android 中图像的常见问题:
http://developer.android.com/training/displaying-bitmaps/index.html
您使用了 android 限制,这就是您看到此错误的原因。重新缩放图像,不要以原始大小显示它们。
ViewFlipper 加载内存中的所有 View 。将所有图像加载到内存中是个坏主意。 看看下面的问题How many Views are possible in a view flipper in android?
由于 Gallery 已被弃用,因此请使用 ViewPager。 ViewPager 在支持库中可用。您还可以配置在前后两个方向上应加载多少个 View 。您将按需加载图像,并在当前不需要时销毁它们。它允许您重用 View 并仅加载内存中的几个 View :
http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/
在这里你可以找到一些很好的例子来实现它: Android Viewpager as Image Slide Gallery
关于安卓 ViewFlipper OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33955482/