java - 在 Android 中调整图像大小

标签 java android bitmap

我想调整我的图像大小(原始大小为 1080p),但它们无法正确调整大小,我不知道为什么。有时图像的尺寸不正确。在我的模拟器和旧的 800*480 智能手机上,它工作正常,但在我的 1280*768 的 Nexus 4 上,情况看起来不太正确。读取正确的屏幕分辨率是没有问题的。我的调整大小程序有一个错误。请帮助我。

private float smaller;
smaller = height/1080;   //height is screenheight; in my case its 768 because of landscape

object.bitmap = Bitmap.createScaledBitmap(bitmap,(int)(smaller*bitmap.getWidth()) ,(int)(smaller*bitmap.getHeight()), true);

最终高度没有调整为 768/1080*bitmapheight,我不知道为什么。

编辑:

这些是我的程序的屏幕截图,显示图像的高度不同

第一张图片:

imgur.com/STSgAOd,Wh3fVdX

第二:

imgur.com/STSgAOd,Wh3fVdX#1

正如您所看到的,图像的高度不相等。在我的模拟器和旧智能手机上,它们看起来是正确的。图像不应触及底部,但在我的 Nexus 4 上,它们会触及底部。

还尝试过双倍:

private double factor;
factor = ((double)screenheight/(double)1080);
objekte.bitmap1 = Bitmap.createScaledBitmap(bitmap,(int)(factor*bitmap.getWidth()) ,(int)(factor*bitmap.getHeight()), true);

同样的糟糕结果

最佳答案

您假设高度最需要调整大小(看看您的高度/1080)。我可能是宽度必须调整最多。我用它来缩放它们:

//Calculate what scale is needed
double xFactor = (double)image.Width/(double)ScreenWidth;
double yFactor = (double)image.Height/(double)ScreenHeight;
double factor = xFactor;
if(yFactor>xFactor){
    factor = yFactor;
}

int imageWidth = Convert.ToInt32(im.Width / factor);
int imageHeight = Convert.ToInt32(im.Height / factor);

注意:这是用 C# 编写的。它需要一些改变才能工作。 注意2:这可以确保图像全屏。(尽可能多,因为它被称为)

关于java - 在 Android 中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23550357/

相关文章:

java - 如何在dynamodb查询中使用保留关键字 'year'

java - 我最初如何使用 EthereumJ 连接到 Android 中的以太坊网络?

android如何删除创建的bimap

android - 如何使用 Intent.ACTION_SEND 将图像发送到 Android 上的 Facebook Messenger?

android - 如何在 asynctask onPostExecute 上从服务器的 imageview 上显示图像?

java - 无法从子框架调用小程序中的重绘

Java 启动时无法正常工作

android - 有没有办法在 PendingIntent 被触发时终止 Activity ?

java - 像 google inbox 一样将 java 翻译成 javascript

java - 将字符串转换为目录名