如何减小图像大小并在Android的内存中滑动加载

问题描述 投票:0回答:2

我正在尝试使用滑行从Internet加载图像,但是该尺寸对于我的应用程序来说太大了(尺寸为400X400的图像尺寸和352 MB的内存分配,ImageView的尺寸与图像尺寸相同) ,我尝试在加载位图后对其进行解码,然后将其应用于ImageView,但是它不起作用,任何人都可以帮助我解决这个问题。

这是我的Glide代码:

Glide.with(this)
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        int byteCount = resource.getAllocationByteCount();

                        int sizeInKB = (resource.getRowBytes() * resource.getHeight()) / 1024;
                        int sizeInMB = sizeInKB / 1024;

                        Glide.with(TestActivity.this)
                                .asBitmap()
                                .load(decodeSampledBitmapFromResource(resource, 400, 400))
                                .into(ss2);

                        sizeText.setText(sizeInKB + " KB");
                    }
                });

这是用于解码位图的代码:

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Bitmap bitmap, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, blob);
    byte[] bitmapData = blob.toByteArray();
    BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    bitmap.compress(Bitmap.CompressFormat.PNG, 0, blob);
    byte[] bitmapData2 = blob.toByteArray();
    return BitmapFactory.decodeByteArray(bitmapData2, 0, bitmapData2.length, options);
}
java android android-glide android-bitmap
2个回答
1
投票

图像的大小取决于许多因素,其中之一就是图像的尺寸和质量;默认情况下,Glide V4中的质量格式为ARGB_8888,因此,如果使用它,则可以将其更改为较小的RGB_565。您可以找到此信息以及更多信息here


0
投票

在毕加索中,您可以使用.fit()进行操作;在Glide中,您具有centerCrop()fitCenter()此方法将图像调整为相关容器的大小。

© www.soinside.com 2019 - 2024. All rights reserved.