391_压缩Bitmap到指定宽高

来源:互联网 发布:学seo有用吗 编辑:程序博客网 时间:2024/04/30 02:27




压缩Bitmap到指定宽高


    public static Bitmap compressBitmapToGivenWidthAndHeight(Bitmap bitmap, double newWidth, double newHeight) {


        //获取这个图片的宽和高
        float width = bitmap.getWidth();
        float height = bitmap.getHeight();


        //创建操作图片用的matrix对象
        Matrix matrix = new Matrix();


        //计算宽高缩放率
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;


        //压缩Bitmap
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, (int) width, (int) height, matrix, true);
        
        return newBitmap;
    }



0 0
原创粉丝点击