使用图像视图滚动滚动视图时出现内存不足错误

outofmemoryerror when scrolling scrollview with imageview

请问我的应用程序有问题。

我的问题是内存不足错误。

我试着解释一下我的方法

当滚动 scrollview 时出现此错误,其中它是 imageview,imageview 的大小与项目的大小有关(它是带有文本项目 0-x 行的图像)当有 23< 项目并且滚动应用程序大大增加了内存使用量。但是不知道怎么预防?

我将插入位图(canvas) 的部分 class 粘贴到图像视图中。

try {
        Bitmap b = Bitmap.createBitmap(srk, vsk, Bitmap.Config.ARGB_8888);

        c = new Canvas(b);
        imageview.setImageBitmap(b);

    } catch (IllegalArgumentException e) {
    }

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);


    for (int i=1;i<(prvy.getNasledujuciitemindex(pacage));i++) {
        if (prvy.citajItem(pacage,i)!=null) {

            if (prvy.CiZbalene(pacage, i)) {
                paint.setColor(Color.argb(255, 130, 130, 130));
                c.drawRect(zarovnanieXZACDelLine, zarovnanieYDelLine + (i - 1) * vskconst, zarovnanieXKONDelLine, zarovnanieYDelLine + 2 + (i - 1) * vskconst, paint);

                c.drawBitmap(cross, zarovnanieXX, (float) (context.getResources().getDimensionPixelSize(R.dimen.KorekciaLine) + (int) (i - 1) * vskconst), paint);
                paint.setColor(Color.argb(255, 130, 130, 130));
                DalsiPrvok(paint, c, prvy.citajItem(pacage, i), vsk2 + (i - 1) * vskconst, sizetext, font);
            } else {
                paint.setColor(Color.BLACK);
                DalsiPrvok(paint, c, prvy.citajItem(pacage, i), vsk2 + (i - 1) * vskconst, sizetext, font);
            }
        } else {break;}

    }
    imageview.invalidate();

只有这段代码写入imageview。

这是我的 xml 图像视图。

            <ScrollView
                        android:id="@+id/scrollView2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:scrollIndicators="none"
                        android:scrollbars="none">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <FrameLayout
                                android:id="@+id/Pack1"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:background="#e1e1e1"
                                android:paddingBottom="@dimen/Pack_Bottom"
                                android:visibility="visible">

                                <LinearLayout
                                    android:id="@+id/linear1"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:orientation="vertical">



                                    <ImageView
                                        android:id="@+id/listPrvy"
                                        android:layout_width="match_parent"
                                        android:layout_height="0dp"
                                        android:clickable="true"
                                        android:longClickable="true"
                                        android:scaleType="fitXY"/>
                                </LinearLayout>

                            </FrameLayout>
                            </LinearLayout>
                            </ScrollView>

我调用方法↑(将位图写入imageview)

itemimg = new ItemsInPacagesImageView(imglist1, tentocontext, nazovtripu, 0, prvy); - 这个 class 这是我的,它是将位图放入 imglist1 (imageview) 的方法。

当我尝试输入 b.recycle();在位图 b = Bitmap.createBitmap(srk, vsk, Bitmap.Config.ARGB_8888);

之后
        c = new Canvas(b);
        imageview.setImageBitmap(b);

应用程序因使用回收位图等问题而崩溃。

感谢任何想法。

Image

开始滚动后的楼梯...(25 项宽度 1080,高度 5040px)

位图占用大量内存。一些提示是:

  1. 避免Bitmap.Config.ARGB_8888。对于不透明图像,使用 Bitmap.Config.RGB_565
  2. 为您的图片设置BitmapFactory.Options.inPurgeable = true。它在最近的 Android 版本中已被弃用,但在旧版本中工作正常

android 上的一个常见问题是当您开始处理图像时,它会导致大量内存不足错误并专门处理高分辨率图片。

如果您想避免 android 上由 Image 引起的任何内存不足错误问题,我建议您使用 Picasso : Link