dp如何转换成准确的分辨率

简介: Converting dp units to pixel units In some cases, you will need to express dimensions in dp and then convert them to pixels.

Converting dp units to pixel units

In some cases, you will need to express dimensions in dp and then convert them to pixels. Imagine an application in which a scroll or fling gesture is recognized after the user's finger has moved by at least 16 pixels. On a baseline screen, a user's must move by 16 pixels / 160 dpi, which equals 1/10th of an inch (or 2.5 mm) before the gesture is recognized. On a device with a high density display (240dpi), the user's must move by 16 pixels / 240 dpi, which equals 1/15th of an inch (or 1.7 mm). The distance is much shorter and the application thus appears more sensitive to the user.

To fix this issue, the gesture threshold must be expressed in code in dp and then converted to actual pixels. For example:

// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;

// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
mGestureThreshold
= (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);

// Use mGestureThreshold as a distance in pixels...

The DisplayMetrics.density field specifies the scale factor you must use to convert dp units to pixels, according to the current screen density. On a medium-density screen, DisplayMetrics.densityequals 1.0; on a high-density screen it equals 1.5; on an extra high-density screen, it equals 2.0; and on a low-density screen, it equals 0.75. This figure is the factor by which you should multiply the dp units on order to get the actual pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.) For more information, refer to the DisplayMetrics class.

However, instead of defining an arbitrary threshold for this kind of event, you should use pre-scaled configuration values that are available from ViewConfiguration.

Using pre-scaled configuration values

You can use the ViewConfiguration class to access common distances, speeds, and times used by the Android system. For instance, the distance in pixels used by the framework as the scroll threshold can be obtained with getScaledTouchSlop():

private static final int GESTURE_THRESHOLD_DP = ViewConfiguration.get(myContext).getScaledTouchSlop();

Methods in ViewConfiguration starting with the getScaled prefix are guaranteed to return a value in pixels that will display properly regardless of the current screen density.

相关文章
|
7月前
|
人工智能 UED
DynamiCrafter:可实现任意类型静态图像转化为逼真动态视频
【2月更文挑战第17天】DynamiCrafter:可实现任意类型静态图像转化为逼真动态视频
370 1
DynamiCrafter:可实现任意类型静态图像转化为逼真动态视频
|
7月前
|
机器学习/深度学习 缓存 人工智能
大语言模型中常用的旋转位置编码RoPE详解:为什么它比绝对或相对位置编码更好?
Transformer的基石自2017年后历经变革,2022年RoPE引领NLP新方向,现已被顶级模型如Llama、Llama2等采纳。RoPE融合绝对与相对位置编码优点,解决传统方法的序列长度限制和相对位置表示问题。它通过旋转矩阵对词向量应用角度与位置成正比的旋转,保持向量稳定,保留相对位置信息,适用于长序列处理,提升了模型效率和性能。RoPE的引入开启了Transformer的新篇章,推动了NLP的进展。[[1](https://avoid.overfit.cn/post/9e0d8e7687a94d1ead9aeea65bb2a129)]
1093 0
|
机器学习/深度学习
深度学习数据增强方法-内含(亮度增强,对比度增强,旋转图图像,翻转图像,仿射变化扩充图像,错切变化扩充图像,HSV数据增强)七种方式进行增强-每种扩充一张实现7倍扩)+ 图像缩放代码-批量
深度学习数据增强方法-内含(亮度增强,对比度增强,旋转图图像,翻转图像,仿射变化扩充图像,错切变化扩充图像,HSV数据增强)七种方式进行增强-每种扩充一张实现7倍扩)+ 图像缩放代码-批量
|
3月前
|
自然语言处理 数据可视化 API
优化采样参数提升大语言模型响应质量:深入分析温度、top_p、top_k和min_p的随机解码策略
本文详细解析了大语言模型(LLM)的采样策略及其关键参数,如温度和top_p。LLM基于输入提示生成下一个标记的概率分布,通过采样策略选择标记并附回输入,形成循环。文章介绍了对数概率(logprobs)、贪婪解码、温度参数调整、top-k与top-p采样等概念,并探讨了min-p采样这一新方法。通过调整这些参数,可以优化LLM输出的质量和创造性。最后,文章提供了实验性尝试的建议,帮助读者在特定任务中找到最佳参数配置。本文使用VLLM作为推理引擎,展示了Phi-3.5-mini-instruct模型的应用实例。
113 6
|
6月前
|
计算机视觉
OpenCV图像像素值统计
OpenCV图像像素值统计
|
计算机视觉
Opencv图像处理:判断图片里某个颜色值占的比例
Opencv图像处理:判断图片里某个颜色值占的比例
896 0
|
Java
剪裁NV21任意一部分的代码
剪裁NV21任意一部分的代码
125 0
|
算法 Java 计算机视觉
常用的像素操作算法:图像加法、像素混合、提取图像中的ROI
常用的像素操作算法:图像加法、像素混合、提取图像中的ROI
384 0
常用的像素操作算法:图像加法、像素混合、提取图像中的ROI
|
前端开发 Android开发
【Android 应用开发】Canvas 精准绘制文字 ( 测量文本真实边界 | 将文本中心点与给定中心点对齐 )
【Android 应用开发】Canvas 精准绘制文字 ( 测量文本真实边界 | 将文本中心点与给定中心点对齐 )
222 0
【Android 应用开发】Canvas 精准绘制文字 ( 测量文本真实边界 | 将文本中心点与给定中心点对齐 )