//转换dip为px
public static int convertDipOrPx(Context context, int dip) {
float scale = context.getResources().getDisplayMetrics().density;
return (int)(dip*scale + 0.5f*(dip>=0?1:-1));
}
//转换px为dip
public static int convertPxOrDip(Context context, int px) {
float scale = context.getResources().getDisplayMetrics().density;
return (int)(px/scale + 0.5f*(px>=0?1:-1));
}
首先我要说一句:dip不是像素密度,像素密度是dpi,引用句原话dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。好了,入正题吧,先说说px,px就是像素,如果用px,就会用实际像素画,比个如吧,用画一条长度为240px的横线,在480宽的模拟器上看就是一半的屏宽,而在320宽的模拟器上看就是2/3的屏宽了。而dip,就是把屏幕的高分成480分,宽分成320分。
Resourcesresources=getResources();
floatfPx=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,300,resources.getDisplayMetrics());
intiPx=Math.round(fPx);
EditTexteditText=newEditText(this);
editText.setWidth(iPx);
editText.setHeight(LayoutParams.WRAP_CONTENT);
分享到:
相关推荐
android开发之dip,dp与px像素之间的转换工具,博文介绍:http://blog.csdn.net/qq_21376985/article/details/51704703
描述android的dip dp px dp等单位及其转换关系
px转dip小工具,界面类似于计算器。 快捷键说明: 0~9 键:呃……这个不用说了吧。 退格键:也不说了。 回车键:转换。 ESC 键:清零。 Ctrl+c:复制。 Ctrl+v:粘贴。 F1~F4 :ldpi~xhdpi. Alt+F4:好吧,这个不是我...
在xml布局文件中,我们既可以设置px...以下是一个应用类,方便进行px和dp之间的转换。 代码如下:import android.content.Context; public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
可以进行像素和dip之间的转化 ,方便开发者做一些单位的转换和频幕适配
根据手机的分辨率从 dip 的单位 转成为 px(像素) 根据手机的分辨率从 px(像素) 的单位 转成为 dp dp转px sp转px px转dp px转sp
>开发中常用的一些方法(如:Log,Toast,SnackBar,getScreenWidth,dip2px等等)。 注意:Log的打印应该在Debug模式下。建议这样使用: Utils.initialize(this); if(BuildConfig.DEBUG){ Utils.setDebug(true,...
前言:dp/dip代表独立像素,dpi代表屏幕每英寸像素点的个数,px与dp的转换公式为: px = dp *(dpi / 160) 一、效果图 二、源代码 AnimationActivity: package com.example.duoyi.clientaidl; import android....
* dip 转换成px * * @param dip * @return */ private int dipToPx(float dip) { float density = getContext().getResources().getDisplayMetrics().density; return (int) (dip * density + 0.5f * (dip >...
开关双键的输入:输入先输出高 一个按键 KEY_ON 接在 P1.6 与 GND 之间,另一个按键 KEY_OFF 接 P1.7 与 GND 之间,按 KEY_ON 后 LED 亮,按 KEY_OFF 后 LED 灭。同时按下 LED 半亮,LED 保持后松开键的状态,即 ON...