前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android入门学习笔记(3) 像素换算dp

Android入门学习笔记(3) 像素换算dp

原创
作者头像
逆回十六夜
修改2020-01-13 10:55:44
1.5K0
修改2020-01-13 10:55:44
举报
文章被收录于专栏:逆回十六夜逆回十六夜

资料引用:https://www.jianshu.com/p/63aeb5ba85aa

摘抄:

分辨率(PX)

分辨率就是手机屏幕的像素点数。一般为屏幕的“宽×高”,例如分辨率有720×1280的手机设备,表示此屏幕在宽度方向有720个像素点,在高度方向有1280个像素点。

屏幕尺寸(英寸inch)

按屏幕对角测量的实际物理尺寸。为简便起见,Android 将所有实际屏幕尺寸分组为四种通用尺寸:小、 正常、大和超大,以英寸(inch)为单位。例如有个5寸的手机设备,是指对角线的尺寸,5寸×2.54厘米/寸=12.7厘米。

屏幕密度(DPI)

就是每英寸的像素点数,数值越高当然显示越清晰,通常 与“正常”或“高”密度屏幕相比,“低”密度屏幕在给定物理区域的像素较少。

密度无关像素 (dp)

在定义 UI 布局时应使用的虚拟像素单位,用于以密度无关方式表示布局维度 或位置。 密度无关像素等于 160 dpi 屏幕上的一个物理像素,这是 系统为“中”密度屏幕假设的基线密度。在运行时,系统 根据使用中屏幕的实际密度按需要以透明方式处理 dp 单位的任何缩放 。dp 单位转换为屏幕像素很简单: px = dp * (dpi / 160)。 例如,在 240 dpi 屏幕上,1 dp 等于 1.5 物理像素。在定义应用的 UI 时应始终使用 dp 单位 ,以确保在不同密度的屏幕上正常显示 UI。

代码语言:javascript
复制
六种通用的密度:
ldpi(低)~120dpi
mdpi(中)~160dpi
hdpi(高)~240dpi
xhdpi(超高)~320dpi
xxhdpi(超超高)~480dpi
xxxhdpi(超超超高)~640dpi

密度独立性

大多数情况下,确保应用中的屏幕独立性很简单,只需以适当的密度独立像素(dp 单位)或 "wrap_content" 指定所有 布局尺寸值。系统然后根据适用于当前屏幕密度的缩放比例适当地缩放位图可绘制对象,以 适当的大小显示。

实践验证:

现有一个搬运的layout界面:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation = "vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:orientation = "horizontal">

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#b2dfdb"/>

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#80cbc4"/>

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#4db6ac"/>

        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#26a69a"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation = "vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#b2dfdb"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#80cbc4"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#4db6ac"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#26a69a"/>

    </LinearLayout>

</LinearLayout>
参考图
参考图

但是搬运到AndroidStudio中像素位置发生了偏移

实际图
实际图

查看AndroidStudio用于调试手机的像素大小

420dpi,1080*1920

根据以上知识进行计算总共的长宽dp:

横向:1080/(420/160)=411.428

纵向: 1920/(420/160)=731.428

更改以上的代码:

96-->(411.428/4)=103

68-->(731.428-250)/4=120

修改后的图
修改后的图

大致符合参考图的样子,验证成功。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 资料引用:https://www.jianshu.com/p/63aeb5ba85aa
    • 摘抄:
      • 分辨率(PX)
      • 屏幕尺寸(英寸inch)
      • 屏幕密度(DPI)
      • 密度无关像素 (dp)
      • 密度独立性
  • 实践验证:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档