android 根据固定的宽度或高度等比例缩放图片

来源:互联网 发布:自动化前景怎么样知乎 编辑:程序博客网 时间:2024/06/06 08:53

根据固定的宽度或高度缩放图片,当图片的宽度大于高度时,按照宽度的固定值等比例缩放,当图片的高度大于宽度时,按高度的固定值等比例缩放;

public static Bitmap getImageThumbnail(String imagePath, int width,                                           int height) {        Bitmap bitmap = null;        BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        // 获取这个图片的宽和高,注意此处的bitmap为null        bitmap = BitmapFactory.decodeFile(imagePath);        options.inJustDecodeBounds = false; // 设为 false//        bitmap = adjustPhotoRotation(bitmap,1);        //解决三星手机获取系统照片自动横屏显示的问题        int degree = readPictureDegree(imagePath);        bitmap = rotaingImageView(degree,bitmap);        float scale;        if(bitmap.getWidth()>=bitmap.getHeight()){            scale = width/(float)bitmap.getWidth();        }else{            scale = height/(float)bitmap.getHeight();        }        Matrix matrix = new Matrix();        matrix.setScale(scale, scale);        bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);        return bitmap;    }

0 0
原创粉丝点击