使用矩阵进行压缩,通过缩放图片尺寸,来达到压缩图片的效果,和采样率的原理一样.先用位图的option将位图压缩一半,再用matrix缩放0.3f
public class MainActivity extends AppCompatActivity { WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = findViewById(R.id.image); BitmapFactory.Options newOpts = new BitmapFactory.Options();//采样率缩放位图 newOpts.inJustDecodeBounds = false; newOpts.inSampleSize = 2;//设置缩放比例 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.icon8, newOpts); int width =bitmap.getWidth(); int height = bitmap.getHeight(); // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(0.3f, 0.3f); // 得到新的图片 Bitmap newBitMap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); imageView.setImageBitmap(newBitMap); } }