当前位置: 代码迷 >> Android >> Android中dip(dp)与px其间单位转换 dip2px dp转px 无context算法(以及获取获取屏幕宽度和高度)
  详细解决方案

Android中dip(dp)与px其间单位转换 dip2px dp转px 无context算法(以及获取获取屏幕宽度和高度)

热度:533   发布时间:2016-04-28 02:22:13.0
Android中dip(dp)与px之间单位转换 dip2px dp转px 无context算法(以及获取获取屏幕宽度和高度)


废话不多说直接上代码:

1.dip2px  dp转px  无context算法


	public static int px2dip(int pxValue)	{		final float scale = Resources.getSystem().getDisplayMetrics().density;		return (int) (pxValue / scale + 0.5f);	}	public static float dip2px(float dipValue)	{		final float scale = Resources.getSystem().getDisplayMetrics().density;		return  (dipValue * scale + 0.5f);	}


2.获取屏幕区域



    /**     * 获取屏幕区域     */    public static Rect getScreenRect() {        DisplayMetrics displayMetric = new DisplayMetrics();        displayMetric = Resources.getSystem().getDisplayMetrics();        Rect rect = new Rect(0, 0, displayMetric.widthPixels, displayMetric.heightPixels);        return rect;    }    /**     * 获取屏幕宽度     *     */    public static int getScreenWidth() {        return getScreenRect().width();    }    /**     * 获取屏幕高度     *     */    public static int getScreenHeight() {        return getScreenRect().height();    }


  相关解决方案