1

我试图在保持相同纵横比的同时缩小位图。以下代码工作正常,但我知道它不是最好的方法。我绝对不喜欢创建一个持有人位图,然后再创建另一个来缩小它。任何有关如何改进这一点的建议表示赞赏。

//width and height are passed in as params

    Bitmap viewBM = view.getBitMap();

    float aspectRatio = (float) width/height;        
    int newWidth= viewBM.getWidth();
    int newHeight = viewBM.getHeight();        

    if (newHeight > newWidth) {         
        newHeight= (int)Math.round(newWidth * aspectRatio);
    } else {
        newWidth= (int)Math.round(newHeight * aspectRatio);
    }        

    Bitmap holderBitmap =  Bitmap.createBitmap(viewBM, 0, 0, newWidth, newHeight);               
    final Bitmap finalBitmap = Bitmap.createScaledBitmap(holderBitmap, width, height, true);
4

0 回答 0