如何缩放位图以使用不同的屏幕大小和密度获取相同百分比的屏幕



我正在尝试在Android的画布上缩放位图。

我目前在做什么:

int scaleFactor = 0.01;
int size = screenWidth * screenHeight * scaleFactor;
Rect rect = new Rect(x,y, x + size, y + size);
canvas.drawBitmap(bitmap, null, rect, null);

问题是密度较低的手机显示的图像比密度较高的手机小得多。

有谁知道策略或解决方案吗?

这就是我解决它的方式。

而不是用屏幕宽度*屏幕高度来计算屏幕上的所有像素。我计算了对角线,并使用基于对角线长度的因子缩放了所有因子。

float x1 = 0; //left of screen
float y2 = 0; //top of screen
float x2 = screenWidth; //right of screen
float y2 = screenHeight; //bottom of screen
double distance = Math.sqrt(Math.pow(Math.abs(x1 - x2),2) + Math.pow(Math.abs(y1 - y2), 2));
int scaleFactor = 0.01;
int size = distance * scaleFactor;
Rect rect = new Rect(xPos,yPos, xPos + size, yPos + size);
canvas.drawBitmap(bitmap, null, rect, null);

最新更新