Android 尺寸单位转换和屏幕适配相关
Android 尺寸单位转换和屏幕适配相关
各种尺寸单位的意义
资源类型
尺寸单位转换 工具类
可以写工具类对尺寸单位进行转换,比如:
- package com.mengdd.dimen;
- import android.content.Context;
- public class DimenUtils {
- public static int sp2px(Context context, float spValue) {
- float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
- return (int) (spValue * fontScale + 0.5f);
- }
- public static int px2sp(Context context, float pxValue) {
- float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
- return (int) (pxValue / fontScale + 0.5f);
- }
- public static int dip2px(Context context, int dipValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dipValue * scale + 0.5f);
- }
- public static int px2dip(Context context, float pxValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (pxValue / scale + 0.5f);
- }
- }
Android中的DisplayMetrics这个类描述了关于显示的各种信息,可以利用它查看设备的状态,上述关于屏幕密度的标准的常量也是从这个类中看到的。
DisplayMetrics的toString()方法如下:
- @Override
- public String toString() {
- return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
- ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
- ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
- }
其中各个变量解释如下:
- /**
- * The absolute width of the display in pixels.
- */
- public int widthPixels;
- /**
- * The absolute height of the display in pixels.
- */
- public int heightPixels;
- /**
- * The logical density of the display. This is a scaling factor for the
- * Density Independent Pixel unit, where one DIP is one pixel on an
- * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
- * providing the baseline of the system's display. Thus on a 160dpi screen
- * this density value will be 1; on a 120 dpi screen it would be .75; etc.
- *
- * <p>This value does not exactly follow the real screen size (as given by
- * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
- * the overall UI in steps based on gross changes in the display dpi. For
- * example, a 240x320 screen will have a density of 1 even if its width is
- * 1.8", 1.3", etc. However, if the screen resolution is increased to
- * 320x480 but the screen size remained 1.5"x2" then the density would be
- * increased (probably to 1.5).
- *
- * @see #DENSITY_DEFAULT
- */
- public float density;
- /**
- * The screen density expressed as dots-per-inch. May be either
- * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
- */
- public int densityDpi;
- /**
- * A scaling factor for fonts displayed on the display. This is the same
- * as {@link #density}, except that it may be adjusted in smaller
- * increments at runtime based on a user preference for the font size.
- */
- public float scaledDensity;
- /**
- * The exact physical pixels per inch of the screen in the X dimension.
- */
- public float xdpi;
- /**
- * The exact physical pixels per inch of the screen in the Y dimension.
- */
- public float ydpi;
实际设备参数
小米2S,DisplayMetrics中的toString()方法输出如下:
- DisplayMetrics{density=2.0, width=720, height=1280, scaledDensity=2.0, xdpi=345.0566, ydpi=342.23157}
参考资料
Android 尺寸单位转换和屏幕适配相关的更多相关文章
- Android 浅谈 设计与屏幕适配 【1.6235449734285716】
extends: http://www.ui.cn/detail/45435.html http://www.2cto.com/kf/201501/372699.html http://www.cnb ...
- Android屏幕尺寸单位转换
最近在看Android群英传这本书,书中有一节涉及到了,屏幕尺寸与单位.觉得以后可能会用到,做个笔记. PPI(pixels per inch) ,又称为DPI,它是由对角线的像素点数除以屏幕的大小得 ...
- [转]android – 多屏幕适配相关
1.基本概念 屏幕大小(screen size) – 屏幕的实际大小,用屏幕对角线长度来衡量(比如3.4寸,3.8寸).android把屏幕分为以下4种:small,normal,large,extr ...
- android技巧(三)屏幕适配
屏幕适配策略: 1.控件使用wrap_content.match_parent控制某些视图组件的宽度和高度,而不是硬编码的尺寸. “wrap_content”系统就会将视图的宽度或高度设置成所需的最小 ...
- Android尺寸单位
px:pixels(像素),1px的长度对应屏幕一个像素点的大小. dp/dip:(density-independent pixels,设备无关像素) sp:scaled pixels(可缩放像素) ...
- iOS屏幕适配-iOS笔记
学习目标 1.[了解]屏幕适配的发展史 2.[了解]autoResizing基本用法 3.[掌握]autoLayout 的基本用法 4.[掌握]autoLayout代码实现 5.[理解]sizeCla ...
- Android屏幕适配全攻略(最权威的官方适配指导)屏幕尺寸 屏幕分辨率 屏幕像素密度 dpdipdpisppx mdpihdpixdpixxdpi
Android屏幕适配全攻略(最权威的官方适配指导)原创赵凯强 发布于2015-05-19 11:34:17 阅读数 153734 收藏展开 转载请注明出处:http://blog.csdn.net/ ...
- android屏幕适配之度量单位、屏幕分类、图标尺寸归类分析
好久没有做android项目UI的适配了,好多基本概念都已经模糊了,于是萌生了将屏幕分辨率.常用单位.常用图标尺寸等信息规整的想法,一下就是通过查询资料,自己验证的一些随笔,如有失误之处,望大家及时予 ...
- 【Android 应用开发】Android屏幕适配解析 - 详解像素,设备独立像素,归一化密度,精确密度及各种资源对应的尺寸密度分辨率适配问题
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19698511 . 最近遇到了一系列的屏幕适配问题, 以及 ...
随机推荐
- 基于caffe的艺术迁移学习 style-transfer-windows+caffe
这个是在去年微博里面非常流行的,在git_hub上的代码是https://github.com/fzliu/style-transfer 比如这是梵高的画 这是你自己的照片 然后你想生成这样 怎么实现 ...
- ado.net 用c#与数据库连接实现增删改查
ADO.NET: 数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 是所有数据访问技术的基础 ...
- petapoco sql语句参数化 插入邮箱地址
直接上代码,我是这样插入信息的 string sql = string.Format(@" INSERT INTO T_Log ( UserId , ProValue ) VALUES ( ...
- Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)
很久没有写博客了,一些读者也经常问问一些问题,不过最近我确实也很忙,除了处理日常工作外,平常主要的时间也花在了继续研究微软的实体框架(EntityFramework)方面了.这个实体框架加入了很多特性 ...
- WebForm 基础
IIS安装 webForm需要IIS安装 1.安装:控制面板--程序或功能--打开或关闭windows功能--Internet信息服务(打上勾)--确定 2.让vs和IIS相互认识vs:vs2012- ...
- 点/边 双连通分量---Tarjan算法
运用Tarjan算法,求解图的点/边双连通分量. 1.点双连通分量[块] 割点可以存在多个块中,每个块包含当前节点u,分量以边的形式输出比较有意义. typedef struct{ //栈结点结构 保 ...
- Wijmo 2016年蓝图
2015年很快就过去了,这是 Wijmo 重要的一年,尤其是对 Wijmo5.脱离传统的小部件,重新写一套 JS 控件,现在看来这个决定是正确的.用 TypeScript 写 Wijmo5,意味着我们 ...
- 非阻塞同步算法与CAS(Compare and Swap)无锁算法
锁(lock)的代价 锁是用来做并发最简单的方式,当然其代价也是最高的.内核态的锁的时候需要操作系统进行一次上下文切换,加锁.释放锁会导致比较多的上下文切换和调度延时,等待锁的线程会被挂起直至锁释放. ...
- Firemonkey TEdit 切换不同 KeyboardType 样式
用代码切换 Edit 不同的键盘样式: procedure TForm1.Button1Click(Sender: TObject); begin Edit1.KeyboardType := TVir ...
- PagerTabStrip及自定义的PagerTab
如图是效果图 开发中经常会用到上面是一个Tab下面是一个ViewPager(ViewPager再包含几个Fragment),当点击Tab或是滑动ViewPager,Tab及ViewPager ...