简体   繁体   English

java函数确定具有给定宽度的图像的高度?

[英]java function to determine height of an image with the given width?

i'm having an image(some dimensions) i set the width of some pixel position. 我有一个图像(某些尺寸),我设置了一些像素位置的宽度。 i need a java method to calculate the height of the image to display properly. 我需要一个Java方法来计算图像的高度以正确显示。

Do you mean you want to maintain the original aspect ratio, you're changing the width and you want to know the height? 您是说要保持原始的宽高比,要更改宽度还是要知道高度吗? If so, it should be something like: 如果是这样,应该是这样的:

int newHeight = (oldHeight * newWidth) / oldWidth;

Performing multiplication then division will avoid some rounding errors, but may fail if the image is huge (ie if it overflows). 执行乘法然后除法将避免一些舍入误差,但是如果图像很大 (即图像溢出),则可能会失败。 An alternative is to use floating point: 一种替代方法是使用浮点数:

int newHeight = (int) (oldHeight * ((double) newWidth / oldWidth));

您是否在BLackberry appln中进行此操作,如果使用Display.getWidth()进行计算

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

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