调整大小的位图 [英] Resizing a bitmap

查看:112
本文介绍了调整大小的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code以下调整大小的位图,并保持宽高比。 我想知道是否有调整的一个更有效的方法,因为我也有,我正在写code的想法,已经是Android的API中。

 私人位图resizeImage(位图位图,诠释newSize){
    INT宽度= bitmap.getWidth();
    INT高= bitmap.getHeight();

    INT newWidth = 0;
    INT newHeight = 0;

    如果(宽>高度){
        newWidth = newSize;
        newHeight =(newSize *高)/宽度;
    }否则如果(宽度LT;高度){
        newHeight = newSize;
        newWidth =(newSize *宽)/身高;
    }否则,如果(宽==高){
        newHeight = newSize;
        newWidth = newSize;
    }

    浮动scaleWidth =((浮点)newWidth)/宽度;
    浮动scaleHeight =((浮点)newHeight)/身高;

    字模=新的Matrix();
    matrix.postScale(scaleWidth,scaleHeight);

    位图resizedBitmap = Bitmap.createBitmap(位图,0,0,
            宽度,高度,矩阵,真);

    返回resizedBitmap;
}
 

解决方案

使用方法<一href="http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap%28android.graphics.Bitmap,%20int,%20int,%20boolean%29"><$c$c>Bitmap.createScaledBitmap() :)

The code below resizes a bitmap and keeps the aspect ratio. I was wondering if there is a more efficient way of resizing, because i got the idea that i'm writing code that is already available in the android API.

private Bitmap resizeImage(Bitmap bitmap, int newSize){
    int width = bitmap.getWidth();
    int height = bitmap.getHeight(); 

    int newWidth = 0;
    int newHeight = 0;

    if(width > height){
        newWidth = newSize;
        newHeight = (newSize * height)/width;
    } else if(width < height){
        newHeight = newSize;
        newWidth = (newSize * width)/height;
    } else if (width == height){
        newHeight = newSize;
        newWidth = newSize;
    }

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
            width, height, matrix, true); 

    return resizedBitmap;
}

解决方案

Use the method Bitmap.createScaledBitmap() :)

这篇关于调整大小的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆