Android中由于需要适配不同分辨率的屏幕,所以我们需要对dp和px进行转换。在转换之前,首先我们需要计算屏幕的像素密度,再按像素密度,成比例转换。

public class DipUtil {public static int dip2px(Context context, float 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);}public static int getWindowWidth(Context context) {WindowManager wm = (WindowManager) context                .getSystemService(Context.WINDOW_SERVICE);int width = wm.getDefaultDisplay().getWidth();return width ;}public static int getWindowHeight(Context context) {WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);int height = wm.getDefaultDisplay().getHeight();return height ;}}

更多相关文章

  1. 【Android】android开发---实现屏幕旋转的两种方法
  2. android 当环境配置发生变化 例如语言 屏幕变化
  3. Android 屏幕的旋转 onConfigurationChanged方法
  4. Android根据屏幕宽度缩放图片
  5. gridlayout 子view撑出屏幕
  6. 如何让Android字体自适应屏幕分辨率
  7. Android屏幕禁止休眠的方法
  8. 如何让你的手机屏幕固定横屏和竖屏

随机推荐

  1. CHtmlEditCtrl (3): More HTML Editor Op
  2. JQuery函数不能用于初始触发器
  3. 百度地图Api进阶教程-实例高级操作8.html
  4. HTML5新增标签与属性
  5. 如何确定在web页面上呈现的字符串的长度(
  6. telnet建立http连接获取网页HTML内容
  7. jQuery:流体同位素仅在调整大小后才工作
  8. html中显示div的时候,超出浏览器的宽,怎么
  9. 用Jsoup实现html中标签替换
  10. 一步一步学Silverlight 2系列(3):界面布局