如何显示多张图片?

How to display many images?

我想创建一个 activity,用户可以在其中设置背景。 不幸的是,当我加载大约 7 张图片时,图片是 'too big' 它抛出异常(Failed to allocate a 74649612 byte allocation with 7804240 free bytes and 7MB until OOM)。 有没有办法在 Android Studio 中制作图像 'smaller',而不用在 photoshop 中缩小图像?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    (...)
    ImageView image_1 = (ImageView) findViewById(R.id.image_1);
    ImageView image_2 = (ImageView) findViewById(R.id.image_2);
    ...

    image_1.setImageDrawable(getDrawable(R.drawable.background_1));
    image_2.setImageDrawable(getDrawable(R.drawable.background_2));
    ...

我已经在 AndroidManifest.xml 中启用了 android:largeHeap="true"

感谢您的帮助。

我想这对你有帮助。

Handling Bitmaps

或者你可以使用 Glide 库。

您可以通过减小可绘制图像的大小来解决此问题,为此您需要先将图像转换为位图,然后使用以下代码减小位图的大小

  public static Bitmap makeBitmap(String fn, int minSideLength, int maxNumOfPixels) {
BitmapFactory.Options options;
try {
    options = new BitmapFactory.Options();

    options.inPurgeable = true;
    options.inJustDecodeBounds = true;
    BitmapFactory.BitmapFactory.decodeResource(getResources(), R.drawable.large_icon,options);
    if (options.mCancel || options.outWidth == -1
            || options.outHeight == -1) {
        return null;
    }
    options.inSampleSize = computeSampleSize(
            options, minSideLength, maxNumOfPixels);
    options.inJustDecodeBounds = false;
    //Log.e(LOG_TAG, "sample size=" + options.inSampleSize);

    options.inDither = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    return BitmapFactory.decodeFile(fn, options);
} catch (OutOfMemoryError ex) {
    Log.e(LOG_TAG, "Got oom exception ", ex);
    return null;
}
}

private static int computeInitialSampleSize(BitmapFactory.Options options,
    int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;

int lowerBound = (maxNumOfPixels == UNCONSTRAINED) ? 1 :
        (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == UNCONSTRAINED) ? 128 :
        (int) Math.min(Math.floor(w / minSideLength),
        Math.floor(h / minSideLength));

if (upperBound < lowerBound) {
    // return the larger one when there is no overlapping zone.
    return lowerBound;
}

if ((maxNumOfPixels == UNCONSTRAINED) &&
        (minSideLength == UNCONSTRAINED)) {
    return 1;
} else if (minSideLength == UNCONSTRAINED) {
    return lowerBound;
} else {
    return upperBound;
}
}

您可以使用 Glide 加载图像并缩小图像。您可以将此添加到 Gradle 文件中:

repositories {
  mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
  compile 'com.github.bumptech.glide:glide:3.7.0'
  compile 'com.android.support:support-v4:25.3.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0-RC1'
}

这可能是您如何使用它的方法:

Glide.with(context)
            .load(images.get(position))
            .override((int)(thumbnailHeight * IMAGE_SCALE_FACTOR), (int)(thumbnailHeight * IMAGE_SCALE_FACTOR))
            .into(imageView);

其中 images 是包含图像(本地或远程)URL 的 ArrayList<String>thumbnailHeight 是包含该图像的视图的高度(我也使用高度值宽度使其平方),IMAGE_SCALE_FACTOR 是一个常量,指示图像将按比例缩小多少(如果你想要它更小,只需使用 0.1 和 0.9 之间的值)