简体   繁体   English

如何在Android中以编程方式更改位图的高度和宽度?

[英]How to change bitmap height and width programmatically in android?

I want to resize the bitmap height & width into same as the user device width & height.So please help me if u know the code.Thank You ! 我想将位图的高度和宽度调整为与用户设备的宽度和高度相同。如果您知道代码,请帮助我。谢谢!

This is my code: 这是我的代码:

 Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.nature);

DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
width = metrics.widthPixels;
height=metrics.heightPixels;

But it was not working 但这没用

Try to use this method: 尝试使用此方法:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    // GET CURRENT SIZE
    int width = bm.getWidth();
    int height = bm.getHeight();
    // GET SCALE SIZE
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

Bitmap.createScaledBitmap works great. Bitmap.createScaledBitmap效果很好。 As the document : 作为文件

Creates a new bitmap, scaled from an existing bitmap, when possible. 如果可能,创建一个新的位图,从现有的位图缩放。 If the specified width and height are the same as the current width and height of the source bitmap, the source bitmap is returned and no new bitmap is created. 如果指定的宽度和高度与源位图的当前宽度和高度相同,则返回源位图,并且不会创建新的位图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM