android 按比例缩放图片(适屏)

private Bitmap reSize(Bitmap bitmaporg) {
    int widthold = bitmaporg.getWidth();//eg:1024
    int heightold = bitmaporg.getHeight();//eg:819
    //获取屏幕的宽高
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenWidth = dm.widthPixels;  //eg:1440
    int screenHeigh = dm.heightPixels;  //eg:2392
    float density = dm.density;  // 密度(0.75 / 1.0 / 1.5 //eg: 1dp=3.5px
    //int densityDpi = dm.densityDpi;  // 密度DPI120 / 160 / 240    float scale = (float) screenWidth / ((density + 0.2f) * widthold);
    Matrix matrix = new Matrix();
    if (scale < 1) {
        //将大图缩小至适屏
        float scaleWidth = (float) scale;
        float scaleHeight = (float) scale;
        matrix.postScale(scaleWidth, scaleHeight);
    } else {
        float scaleWidth = 1;
        float scaleHeight = 1;
        matrix.postScale(scaleWidth, scaleHeight);
    }

    Log.d("Main", "screenWidth:" + screenWidth);
    Log.d("Main", "screenHeight:" + screenHeigh);
    Log.d("Main", "widthold:" + widthold);
    Log.d("Main", "heightold:" + heightold);


    Bitmap bitmapnew = Bitmap.createBitmap(bitmaporg, 0, 0, widthold, heightold, matrix, true);
    int widtnew = bitmapnew.getWidth();
    int heightnew = bitmapnew.getHeight();
    Log.d("Main", "widthnew:" + widtnew);
    Log.d("Main", "heightnew:" + heightnew);
    return bitmapnew;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值