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.

相关文章
|
2月前
|
自然语言处理 安全 API
2025阿里云双11特惠:文本短信低至 0.02 元/条,短信认证套餐包3.99元/年
2025年阿里云双11金秋云创季开启!短信认证套餐包新人专享7折,免资质签名即用即发;短信服务新客低至0.02元/条,老客最高减5850元,验证短信秒级触达率99%。
345 1
|
6月前
|
JavaScript 前端开发 中间件
重新定义状态管理:Zustand 如何以极简之力征服复杂应用
重新定义状态管理:Zustand 如何以极简之力征服复杂应用
284 1
|
2月前
|
机器学习/深度学习 存储 并行计算
大模型推理加速技术:FlashAttention原理与实现
本文深入解析大语言模型推理加速的核心技术——FlashAttention。通过分析传统注意力机制的计算瓶颈,详细阐述FlashAttention的IO感知算法设计、前向反向传播实现,以及其在GPU内存层次结构中的优化策略。文章包含完整的CUDA实现示例、性能基准测试和实际部署指南,为开发者提供高效注意力计算的全套解决方案。
421 10
|
2月前
|
人工智能 运维 安全
2025年工作流自动化的15个趋势,如何影响企业的业务?
越来越多企业正通过自动化与智能化升级工作模式,聚焦科技、制造、医疗三大领域。从RPA、AI到低代码平台,技术赋能提升效率、保障安全;智能制造优化运维;智慧医疗减轻负担。超自动化推动流程互联,让员工更专注创新与核心事务,实现高效协同与可持续发展。
284 1
|
2月前
|
存储 监控 API
Python datetime模块:时间处理的瑞士军刀
免费Python编程教程,深入讲解datetime模块核心用法:时间对象创建、格式化、运算及时区处理,附实战案例与避坑指南,助你高效掌握Python时间操作。链接:https://pan.quark.cn/s/2c17aed36b72
627 0
|
3月前
|
存储 C语言 C++
& 符号的含义和用法
在C语言中,`&`符号常用于取地址,如`scanf`中传递变量地址以存储输入数据。示例中`&a`和`&x`获取变量内存地址,确保数据正确读入。省略会导致未定义行为。此外,`&`还用于指针声明、按位与运算等,是C/C++中的关键操作符之一。
1179 0
|
测试技术 调度 C++
六年打磨!阿里开源混沌工程工具 ChaosBlade
减少故障的最好方法就是让故障经常性的发生。通过不断重复失败过程,持续提升系统的容错和弹性能力。今天,阿里巴巴把六年来在故障演练领域的创意和实践汇浓缩而成的工具进行开源,它就是 “ChaosBlade”。如果你想要提升开发效率,不妨来了解一下。
12309 0
|
5月前
|
运维 数据可视化 数据挖掘
生产现场巡检怎么做才有效?一线点检、异常上报、统计报表的最佳实践
本文介绍了如何通过草料二维码构建轻量、易用且闭环的巡检机制。针对巡检员、维保人员和管理员三类角色,分别阐述了扫码点检、异常即时上报与自动提醒、任务监督与数据分析的具体实现方式,助力企业提升巡检效率与执行效果,实现设备管理的透明化与智能化。
抖音快手小红书养号脚本,全自动刷视频养号插件,自动看视频看广告软件
这是一款用于抖音账号自动养号的脚本源码,可实现自动化操作,如搜索、关注和随机观看视频等功能。通过界面设置延迟时间和操作次数