简体   繁体   English

如何将位图重新缩放到不同的屏幕尺寸但保持正方形?

[英]How to re-scale bitmap to different screen sizes but keep it square?

Hello great people of stackoverflow.com, I have a stumbled upon a great difficulty in my code; 你好stackoverflow.com的人,我偶然发现我的代码很难; that being not being able to resize my images in-line with my screen size. 无法根据我的屏幕尺寸调整图像大小。

I have an image that is SAY 65 pixels by 65 pixels but I want this image to be a certain percentage of the screen say 6% while keeping square. 我有一个65像素乘65像素的图像,但我希望这个图像是屏幕的一定百分比说6%同时保持正方形。

My brain cannot process the mathematics for this (because it is slow :-( ) 我的大脑不能为此处理数学(因为它很慢:-()

(_width & _height is the screen width and height) (_width&_height是屏幕宽度和高度)

What I've got so far is: 到目前为止我得到的是:

int width = bitmap.getWidth();
int height = bitmap.getHeight();

int avg_screen_dimension = (_width + _height) / 2;

float scaleWidth =
    ((float) width + (avg_screen_dimension - _width)) / width;
float scaleHeight =
    ((float) height + (avg_screen_dimension - _height)) / 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(bitmap, 0, 0, width, height, matrix, false);

return resizedBitmap;

Any help would be greatly appreciated! 任何帮助将不胜感激!

It's little more than simple algebra - no complex math needed. 它只不过是简单的代数 - 不需要复杂的数学。

You'll have to choose between fraction of the width or height of your screen, unless they're equal. 您必须在屏幕宽度或高度的一小部分之间进行选择,除非它们相等。

Let h = screen height, w = screen width, f = fraction of screen dimension, x = image size. 设h =屏幕高度,w =屏幕宽度,f =屏幕尺寸的分数,x =图像尺寸。

Then x = h*f. 然后x = h * f。 If you want to make it a fraction of the width, substitute w for h. 如果你想把它变成宽度的一小部分,用w代替h。

(就像旁边一样 - 在你的粘贴代码中,你混淆了变量名称:你将它们声明为高度宽度,但将它们作为_width_height传递给它们

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

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