记bitmap内存优化

减小bitmap占内存大小的方案有两种:1,options.inSampleSize 设置采样率的值,原理是等比缩放宽高。缩放多少倍bitmap的内存大小占用久缩放多少倍。 2.options.inPreferredConfig = Bitmap.Config.RGB_565; 设置位深,也可以说是设置RGB格式,一般有三种RGB_8888,RGB_565,RGB_4444(现在用这种好像无效果了) 设置这个属性的话对png是无效的不能改变bitmap的大小,对jpg,webp是有效的。RGB_8888比RGB_565占的内存大一倍。
 options.inJustDecodeBounds = true;//设置这个属性的话可以拿到图片的宽和高。拿到这宽高可以自己设定一个值然后比较是否大于这个值,做一个采样率的缩放。


质量压缩是不能改变bitmap的内存占用大小的,但是它可以改变图片文件的大小。 尺寸压缩才可以改变bitmap的内存占用大小。

package com.example.myapplication.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.myapplication.R;


public class BitmapActivity extends Activity {
    TextView textView;
    ImageView imageView;
    int imgWidth;
    int imgHeight;
    boolean off = true;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap);

        textView = findViewById(R.id.add);
        imageView = findViewById(R.id.img);
        final BitmapFactory.Options options = new BitmapFactory.Options();
//        options.inJustDecodeBounds = true;//设置这个属性的话可以拿到图片的宽和高
//        options.inSampleSize = 1; //设置这个值可以放大缩小图片,可以按倍数缩放图片的宽高
//        options.inPreferredConfig = Bitmap.Config.ARGB_8888; //inPreferredConfig只对webp和jpg的图片有效,对png的图片无效
        Bitmap bitmap = getBitmap(R.drawable.jiwebp, options);
        imgWidth = options.outWidth;
        imgHeight = options.outHeight;

        textView.setText("宽=" + imgWidth + "高=" + imgHeight + "大小==" + bitmap.getByteCount());
        imageView.setImageBitmap(bitmap);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (off) {
                    options.inSampleSize = 2;
                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    off = false;
                } else {
                    options.inSampleSize = 4;
                    options.inPreferredConfig = Bitmap.Config.RGB_565;
                    off = true;
                }
//                options.inSampleSize = 4;
//                options.inPreferredConfig = Bitmap.Config.RGB_565;

                Bitmap bitmap = getBitmap(R.drawable.jiwebp, options);
                imageView.setImageBitmap(bitmap);
                imgWidth = options.outWidth;
                imgHeight = options.outHeight;
                textView.setText("宽=" + imgWidth + "高=" + imgHeight + "大小==" + bitmap.getByteCount());
            }
        });
    }

    private Bitmap getBitmap(int id, BitmapFactory.Options options) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id, options);
        return bitmap;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章