gpt4 book ai didi

android - 在 canvas.drawBitmap 中使用 dpi 而不是像素

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:59 25 4
gpt4 key购买 nike

我已阅读“屏幕支持 API 指南”(http://developer.android.com/guide/practices/screens_support.html) 和更多资源,但我无法理解 dpi 的工作原理。

我正在开发一款游戏,我没有使用任何布局(我将使用 canvas.drawbitmap 等函数自行绘制)。但是当我使用 Canvas.Drawbitmap 函数时,我需要指定要绘制图像的屏幕像素。

所以我现在使用固定分辨率 (1280x800) 并使用 drawable-nodpi 文件夹,如果手机屏幕更宽或更窄,我会稍后调整 Canvas 。问题在于,当分辨率不是原始分辨率 (1280x800) 时,图像看起来很糟糕。

我该怎么做才能解决这个问题?我在 3 天内阅读和阅读,但所有解释和示例都与布局、 9-patch 等相关。

最佳答案

获取正在使用的设备的密度,并将该密度乘以您选择的某个基本尺寸(您实际希望绘制多大或多小?)

例子:

float objectToDrawHeight = 50; //Specified in plain pixels
float objectToDrawWidth = 50; //Specified in plain pixels

float density = getResources().getDisplayMetrics().density;
objectToDrawHeight *= density;
objectToDrawWidth *= density;

//Draw your object using the new (scaled) height and width
//If you are worried about how the object will look on different aspect ratio devices
// you can get the screen dimensions and use that ratio as a multiplier
// just as you did with density

Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
float screenDimensionX = display.getWidth();
float screenDimensionY = display.getHeight();

使用密度和可能的屏幕尺寸应该允许您绘制任何东西并保持正确缩放。使用 Canvas 时,假设一切都以像素为单位,并且您必须进行 dpi 转换。

关于android - 在 canvas.drawBitmap 中使用 dpi 而不是像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com