绘图“drawables”以 dp 为单位的画布
我试图弄清楚是否有一种方法可以以 dp 单位而不是像素将位图绘制到画布上。例如:以下代码将可绘制对象缩放为 100 * 100 像素。我怎样才能将其更改为 100 * 100 dp?
int lengthPos = 10;
int heightPos = 10
mImage.setBounds(lengthPos, heightPos, (lengthPos + 100), (heightPos + 100));
mImage.draw(c);
此外,我不确定屏幕点击的测量单位是什么,但它们与屏幕上的像素不匹配,因此很难检测何时选择了可绘制对象(您必须猜测要点击的位置)我也可以将点击坐标缩放为 dp 吗?这似乎不太难,特别是因为屏幕点击坐标存储在本地变量中以便在我的应用程序中进行处理
。缩放以便点击坐标250, 250 选择在多个屏幕密度上以 250, 250 绘制的像素。
I'm trying to figure out if there is a way to draw bitmaps to the canvas in dp units instead of pixels. For example: the following code scales the drawable to 100 * 100 px. How can I instead change it do 100 * 100 dp?
int lengthPos = 10;
int heightPos = 10
mImage.setBounds(lengthPos, heightPos, (lengthPos + 100), (heightPos + 100));
mImage.draw(c);
Additionally, I'm not sure what the units of measurement for screen clicks are, but they don't match the pixels on the screen, making it hard to detect when a drawable has been selected (you kinda have to guess where to click to select a drawable. Can I also scale click coordinates to dp? doesn't seem like it would be too hard, especially since screen click coords are stored in a local variable for processing in my application.
In other words, I need to match the scaling so that clicking coordinates 250, 250 selects the pixels drawn at 250, 250 on multiple screen densities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
dp/dx 比率等于您的设备 dpi/160,因此您可以通过这种方式轻松计算倾角。
使用
更多详细信息请参阅参考:http://developer.android.com/reference /android/util/DisplayMetrics.html
The ratio dp/dx is equal to your device dpi/160, so you can calculate dips easily this way.
Use
See the reference for more details: http://developer.android.com/reference/android/util/DisplayMetrics.html
来源(@Romain Guy)
Source (@Romain Guy)
不能直接转换,但可以在 px 和 dp 之间进行转换。有关详细信息,请参阅此相关 StackOverflow 帖子中的答案。
You can't directly, but you can convert between px and dp. See the answer in this related StackOverflow post for details.