数学/算法适合图像到屏幕保持宽高比 [英] math/algorithm Fit image to screen retain aspect ratio

查看:266
本文介绍了数学/算法适合图像到屏幕保持宽高比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用数学/算法,帮助把已知大小的图像,适合两种屏幕尺寸之一:

I need help with math / algorithm to take an image of known size and fit to one of two screen dimensions:

720×480或1280×1024。

720 x 480 or 1280 x 1024.

图片尺寸从XML文件来了,但是这些尺寸是幅尺寸,我还可以得到的选择的图像的从该可以更高和更低的分辨率比条材尺寸的XML。

The image dimensions are coming from an XML file, however those dimensions are the web dimensions, I also get a selection of images from the XML that may be of higher and lower resolution than the web dimensions.

我要的是使用网络尺寸的纵横比(如果可用)显示更高分辨率的图像,在高清(1280×720)屏幕,或者,如果用户是一个SD屏(720×480)上显示的图像在该屏幕上。

What I want is to use the aspect ration of the web dimensions to display the higher resolution image, if available, on an HD (1280x720) screen, or, if the user is on an SD screen (720x480) display the image on that screen.

这将是这个有用的,但较低的优先级,将是,如果我知道图像的分辨率在两个方向比SD画面(在这种情况下,较小的其他的事情,我所知道的是网络层面,图像文件的水平尺寸),以显示它作为在该屏幕上实际尺寸

Other things that would be useful for this, but lower priority, would be, if I know the resolution of the image is smaller in both dimensions than an SD screen (in this case, all I know is the web dimension, and the horizontal dimension of the image file), to display it as actual size on that screen.

希望这是很清楚。

谢谢!

推荐答案

普通的可以是:

图像数据:(W <子>我,H <子>我),并定义 - [R <子>我 = W <子>我 /小时<子>我
屏幕分辨率:(W <子>取值,H <子>取值),并定义 - [R <子>取值 = W <子>取值 / H <子>小号

Image data: (wi, hi) and define ri = wi / hi
Screen resolution: (ws, hs) and define rs = ws / hs

缩放图像尺寸:

rs > ri ? (wi * hs/hi, hs) : (ws, hi * ws/wi)

因此​​,例如:

So for example:

         20
|------------------|
    10
|---------|

--------------------     ---   ---
|         |        |      | 7   |
|         |        |      |     | 10
|----------        |     ---    |
|                  |            |
--------------------           ---

ws = 20
hs = 10
wi = 10
hi = 7

20/10 > 10/7 ==> (wi * hs/hi, hs) = (10 * 10/7, 10) = (100/7, 10) ~ (14.3, 10)

正如你可以清楚地看到扩展到屏幕大小,因为高度是屏幕,但显然保持纵横比,因为 14.3 / 10〜10/7

更新

中心相关图片如下:

通话(W <子>新,H <子>新)的新维度。

call (wnew, hnew) the new dimensions.

top = (hs - hnew)/2
left = (ws - wnew)/2

这篇关于数学/算法适合图像到屏幕保持宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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