- 浏览: 48200 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
d19890415:
[i][/i]引用[*][img][/img][url][/u ...
ImageView ScaleType 属性值的意义 -
j67065:
还是谢谢!我知道怎么用了,就是对那个颜色的解析函数的返回式子的 ...
android 调色板小练习 -
蓝月儿:
<div class="quote_title ...
android 调色板小练习 -
j67065:
有点看不懂
android 调色板小练习 -
abin103:
如何解决的啊?
再向模拟器上添加MP4文件时出现的问题
Android中dip, dp, px, sp之间的区别
(文章来自网络)
Android中dip, dp, px, sp之间的区别:
dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。
px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。
pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;
sp: scaled pixels(放大像素). 主要用于字体显示best for textsize,根据 google 的建议,TextView 的字号最好使用 sp 做单位,
过去,程序员通常以像素为单位设计计算机用户界面。例如,定义一个宽度为300像素的表单字段,列之间的间距为5个像素,图标大小为16×16像素 等。这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上运行该程序,则用户界面会显得很小。在有些情况下,用户界面可能会小到难以看清 内容。
与分辨率无关的度量单位可以解决这一问题,Android支持下列所有单位:
px(像素):屏幕上的点。
in(英 寸):长度单位。
mm(毫米):长度单位。
pt(磅):1/72英寸。
dp(与密度无关的像素):一种基于屏幕密度的抽象 单位。在每英寸160点的显示器上,1dp = 1px。
dip:与dp相同,多用于android/ophone示例中。
sp(与 刻度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放。
我来补充两句:说说dp和sp,dp也就是dip。这个和sp基本类似。如果设置表示长度、高度等属性时可以使用dp或sp。但如果设置字体,需要使用 sp。dp是与密度无关,sp除了与密度无关外,还与scale无关。如果屏幕密度为160,这时dp和sp和px是一样的。1dp=1sp=1px,但 如果使用px作单位,如果屏幕大小不变(假设还是3.2寸),而屏幕密度变成了320。那么原来TextView的宽度设成160px,在密度为320的 3.2寸屏幕里看要比在密度为160的3.2寸屏幕上看短了一半。但如果设置成160dp或160sp的话。系统会自动将width属性值设置成 320px的。也就是160 * 320 / 160。其中320 / 160可称为密度比例因子。也就是说,如果使用dp和sp,系统会根据屏幕密度的变化自动进行转换。
Android下获取字体宽度的方法
第1种方法:
Rect rect=newRect();
paint.getTextBounds("abcd",0,1, rect);
Log.v("a:","height:"+rect.height()+"width:"+rect.width());
第2种方法:
//字符宽度获取方法:
intwidth=(int)paint.measureText("a",0,1);
Log.v("width:","width:"+width);
Android获取屏幕宽高的两种方法
Display display = getWindowManager().getDefaultDisplay();
Log.i("view" , "height:" +display.getHeight());
Log.i("view" , "width:" +display.getWidth());
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
Log.i("view" , "height" +displayMetrics.heightPixels);
Log.i("view" , "width" +displayMetrics.widthPixels);
Android可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题。
手机的分辨率信息是手机的一项重要信息,很好的是,Android已经提供DisplayMetircs类可以很方便的获取分辨率。下面简要介绍DisplayMetics类:
Andorid.util包下的DisplayMetrics类提供了一种关于显示的通用信息,如显示大小,分辨率和字体。
为了获取DisplayMetrics成员,首先初始化一个对象如下:
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics;
注:构造函数DisplayMetrics不需要传递任何参数;调用getWindowManager()之后,会取得现有Activity的Handle,此时,getDefaultDisplay()方法将取得的宽高维度存放于DisplayMetrics对象中,而取得的宽高维度是以像素为单位(Pixel),“像素”所指的是“绝对像素”而非“相对像素”。
Android获取字体高度和设置行高
文章分类:移动开发 网络中好多资料介绍获取字体高度的方法如下:
Java 代码publicintgetFontHeight(floatfontSize)
{
Paint paint =newPaint();
paint.setTextSize(fontSize);
FontMetrics fm = paint.getFontMetrics();
return(int) Math.ceil(fm.descent - fm.top) +2;
}
public int getFontHeight(float fontSize) { Paint paint = new Paint(); paint.setTextSize(fontSize); FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.top) + 2; }
个人更倾向于以下方式获取字体实际高度:
Java 代码Math.ceil(fm.descent - fm.ascent)
Math.ceil(fm.descent - fm.ascent)
通过实际的截图对文字高度的确定,后者更准确一些。
有了字体高度信息,就可以添加行与行之间的空隙,调整行高。
个人实现方式如下:
Java 代码paint.setTextSize(fFontWidth);
FontMetrics fm = paint.getFontMetrics();
fFontHeight = (float)Math.ceil(fm.descent - fm.ascent);
if(fFontHeight > fLineHeight)
{
fMulValue = fLineHeight / fFontHeight;
fAddValue = -1;
}
else
{
fMulValue =1;
fAddValue = fLineHeight - fFontHeight;
}
textViewLeft.setTextSize(fFontWidth);
textViewLeft.setLineSpacing(fAddValue, fMulValue);
paint.setTextSize(fFontWidth); FontMetrics fm = paint.getFontMetrics(); fFontHeight = (float)Math.ceil(fm.descent - fm.ascent); if(fFontHeight > fLineHeight) { fMulValue = fLineHeight / fFontHeight; fAddValue = -1; } else { fMulValue = 1; fAddValue = fLineHeight - fFontHeight; } textViewLeft.setTextSize(fFontWidth); textViewLeft.setLineSpacing(fAddValue, fMulValue);
实践验证这种方式对多种分辨率的屏幕的适应性较强。
以上是个人在实际的功能实现过程中的总结,水平较浅,若看官有不同的看法或者见解,望不惜赐教!
Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标。
它的思路和java.awt.FontMetrics的基本相同。
FontMetrics对象
它以四个基本坐标为基准,分别为:
・FontMetrics.top
・FontMetrics.ascent
・FontMetrics.descent
・FontMetrics.bottom
Java代码Paint textPaint =newPaint( Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(35);
textPaint.setColor( Color.WHITE);
// FontMetrics对象
FontMetrics fontMetrics = textPaint.getFontMetrics();
String text ="abcdefghijklmnopqrstu";
// 计算每一个坐标
floatbaseX =0;
floatbaseY =100;
floattopY = baseY + fontMetrics.top;
floatascentY = baseY + fontMetrics.ascent;
floatdescentY = baseY + fontMetrics.descent;
floatbottomY = baseY + fontMetrics.bottom;
// 绘制文本
canvas.drawText( text, baseX, baseY, textPaint);
// BaseLine描画
Paint baseLinePaint =newPaint( Paint.ANTI_ALIAS_FLAG);>
baseLinePaint.setColor( Color.RED);
canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);
// Base描画
canvas.drawCircle( baseX, baseY,5, baseLinePaint);
// TopLine描画
Paint topLinePaint =newPaint( Paint.ANTI_ALIAS_FLAG);
topLinePaint.setColor( Color.LTGRAY);
canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);
// AscentLine描画
Paint ascentLinePaint =newPaint( Paint.ANTI_ALIAS_FLAG);
ascentLinePaint.setColor( Color.GREEN);
canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);
// DescentLine描画
Paint descentLinePaint =newPaint( Paint.ANTI_ALIAS_FLAG);
descentLinePaint.setColor( Color.YELLOW);
canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);
// ButtomLine描画
Paint bottomLinePaint =newPaint( Paint.ANTI_ALIAS_FLAG);
bottomLinePaint.setColor( Color.MAGENTA);
canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);
Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize( 35); textPaint.setColor( Color.WHITE); // FontMetrics对象 FontMetrics fontMetrics = textPaint.getFontMetrics(); String text = "abcdefghijklmnopqrstu"; // 计算每一个坐标 float baseX = 0; float baseY = 100; float topY = baseY + fontMetrics.top; float ascentY = baseY + fontMetrics.ascent; float descentY = baseY + fontMetrics.descent; float bottomY = baseY + fontMetrics.bottom; // 绘制文本 canvas.drawText( text, baseX, baseY, textPaint); // BaseLine描画 Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);> baseLinePaint.setColor( Color.RED); canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint); // Base描画 canvas.drawCircle( baseX, baseY, 5, baseLinePaint); // TopLine描画 Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); topLinePaint.setColor( Color.LTGRAY); canvas.drawLine(0, topY, getWidth(), topY, topLinePaint); // AscentLine描画 Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); ascentLinePaint.setColor( Color.GREEN); canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint); // DescentLine描画 Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); descentLinePaint.setColor( Color.YELLOW); canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint); // ButtomLine描画 Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); bottomLinePaint.setColor( Color.MAGENTA); canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);
以上摘自[url]
http://hi.baidu.com/wzc520168/item/bacd8fff8131f37c3d198b22
[/url]
dp和px相关转换的公式:
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
发表评论
-
android 应用的安装过程
2014-02-24 11:02 1187转载自 [url] http://blog.csd ... -
androidUI优化
2014-02-24 10:59 653很多时候,我们不想用android系统默认的风格或者属性,我们 ... -
android获取IMEI号码 手机版本信息
2012-12-11 12:40 1876转自 http://www.cnblogs.com/luxia ... -
android 图片的缩放
2012-12-11 12:30 771//获得Bitmap的高和宽 in ... -
ImageView ScaleType 属性值的意义
2012-12-11 12:28 1030http://www.cnblogs.com/llm-andr ... -
android 国际化 转自http://xiaobingandxiaoer.iteye.com/blog/1218411
2012-12-07 14:18 915android国际化 博客分类 ... -
android的回调机制(转自http://www.cnblogs.com/vtianyun/archive/2012/06/19/2555427.html)
2012-11-19 16:37 923一、回调函数 回 ... -
android graphic.paint 类的方法笔记
2011-05-19 16:13 0http://codinglu.blog.163.com/bl ... -
android 调色板小练习
2011-05-19 16:08 3629在开发中一个供取色的 ... -
启动其他的程序
2011-05-17 17:28 1410在一个自己的应用启动另外的程序。 首先要知道要启动的目的程序的 ... -
android工程下的文件资源类型(转载)
2011-05-09 17:00 1089原始地址:http://geniusvic.blog.hexu ... -
调用系统的应用程序信息程序,查看应用程序信息
2011-04-29 13:41 1301有的时候调用系统的东西比我们自己定义布局美观的多,使用起来 ... -
android:MIME 参考手册[转]
2011-04-20 16:38 1151转自:http://blog.sina.com.cn/s/bl ... -
content provider
2011-04-14 15:37 763引用 Android Content Providerandr ... -
popupwindow练习
2011-03-31 15:22 4022今天查资源练习了这个的使用,对于焦点问题,整的脑袋有点大,不知 ... -
地图练习
2011-03-29 14:43 1328今天练习了一下地图的编程,继承MapActivity类和Ove ... -
Android PreferenceActivity 使用练习
2011-03-17 16:01 1391源代码转自:http://blog.csdn.net/ichl ... -
startActivityForResult intent 跳转相关的
2011-03-17 15:46 1299今天见到两个方法:startActivityForResult ... -
ToggleButton的使用有感
2011-02-24 13:42 3891今天用到ToggleButton开关按钮,由于给他设置错了监听 ... -
再向模拟器上添加MP4文件时出现的问题
2011-01-19 13:45 2265再向模拟器上添加MP4格式文件时目前主要出现了三个问题 1、F ...
相关推荐
Android dp和px/sp等单位相互转换
描述android的dip dp px dp等单位及其转换关系
dip dp px sp区别 dip dp px sp区别 dip dp px sp区别
android布局单位sp,dp,pt,px的用法和区别。
Android的计量单位px,in,mm,pt,dp,dip,sp Android的计量单位px,in,mm,pt,dp,dip,sp
Android中的长度单位详解(dp、sp、px、in、pt、mm).pdf
在Android开发中dp和px,sp和px之间的转换时必不可少的。下面脚本之家小编给大家带来了android中px和dp,px和sp之间的转换方法,感兴趣的朋友一起看看吧
NULL 博文链接:https://1307111376.iteye.com/blog/1986752
015集-dp、px和sp视频教程, 适合初学者。
android 里面有个dp和px转换,sp字体的大小,对这些进行了详细的转换分析
Android开发中的常用尺寸单位有如下几种: dp (dip) px pt inch sp 算不知道确切含义,相信对于以上这几种尺寸单位大家也都比较脸熟,这里先让我们重新认识一下它们: dp (dip): 即设备无关像素(device ...
一个简单的用于Android的dp、sp和px单位之间的转换工具类
里面有详细的px值在不同分辨率下对应的sp值、dp值,用于UI适配,直接导入项目的资源文件目录下,通过R.dimen.xxx来进行引用,从此不用写多套布局文件,一套layout搞定。
本篇文章是对android中的dip,dp,px,sp和屏幕密度进行了详细的分析介绍,需要的朋友参考下
Android工具类代码,需要的下载,求好评
主要介绍了Android 图片切换器(dp、sp、px) 的单位转换器的相关资料,需要的朋友可以参考下