前言

有一定开发经验的小伙伴肯定会发现这样一个问题,当我们用xml来写布局的时候,通常用的是dp、sp。(相信大家都知道为什么这样用)。当我们用Java代码来创建View控件时,会发现方法接收的参数都是以px为单位的,当然我们不希望直接使用px的(相信大家都知道为什么不希望使用px为单位)。这个时候大家很自然的会想到转换一下就OK啦。dp、sp与px之间有一定的转换公式,但每用一次就写一次这不是程序员的风格。所以这里就总结了一个工具类,希望可以帮助到大家。

GitHub地址

代码

内容比较简单,话不多说,直接上代码

/**

* 常用单位转换的工具类

*/

public class DensityUtils {

private DensityUtils() {

}

/**

* dp转px

*

* @param context

* @return

*/

public static int dp2px(Context context, float dpVal) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources()

.getDisplayMetrics());

}

/**

* sp转px

*

* @param context

* @return

*/

public static int sp2px(Context context, float spVal) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources()

.getDisplayMetrics());

}

/**

* px转dp

*

* @param context

* @param pxVal

* @return

*/

public static float px2dp(Context context, float pxVal) {

final float scale = context.getResources().getDisplayMetrics().density;

return (pxVal / scale);

}

/**

* px转sp

*

* @param pxVal

* @return

*/

public static float px2sp(Context context, float pxVal) {

return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);

}

/**

* 得到屏幕宽度

*

* @param context

* @return

*/

public static int getDisplayWidth(Context context) {

return context.getResources().getDisplayMetrics().widthPixels;

}

/**

* 得到屏幕高度

*

* @param context

* @return

*/

public static int getDisplayHeight(Context context) {

return context.getResources().getDisplayMetrics().heightPixels;

}

}

实际上,核心内容还是Android API里面的内容,这里只不过是对Android API进行了一次封装,让自己更容易记忆,在开发中效率更高。

我们点进去TypedValue.applyDimension();这个方法,源码如下,源码很清晰,相信大家一眼就能看明白。

public static float applyDimension(int unit, float value,

DisplayMetrics metrics)

{

switch (unit) {

case COMPLEX_UNIT_PX:

return value;

case COMPLEX_UNIT_DIP:

return value * metrics.density;

case COMPLEX_UNIT_SP:

return value * metrics.scaledDensity;

case COMPLEX_UNIT_PT:

return value * metrics.xdpi * (1.0f/72);

case COMPLEX_UNIT_IN:

return value * metrics.xdpi;

case COMPLEX_UNIT_MM:

return value * metrics.xdpi * (1.0f/25.4f);

}

return 0;

}

补充

补充三个方法:

获取状态栏的高度

获取当前屏幕截图但不包含状态栏

获取当前屏幕截图包含状态栏。

这一类代码,并不需要死记硬背,收集好,用的时候可以快速找到即可。

/**

* 获得状态栏的高度

*

* @param context

* @return

*/

public static int getStatusHeight(Context context) {

int statusHeight = -1;

try {

Class> clazz = Class.forName("com.android.internal.R$dimen");

Object object = clazz.newInstance();

int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());

statusHeight = context.getResources().getDimensionPixelSize(height);

} catch (Exception e) {

e.printStackTrace();

}

return statusHeight;

}

/**

* 获取当前屏幕截图,包含状态栏

*

* @param activity

* @return

*/

public static Bitmap snapShotWithStatusBar(Activity activity) {

View view = activity.getWindow().getDecorView();

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bmp = view.getDrawingCache();

int width = getDisplayWidth(activity);

int height = getDisplayHeight(activity);

Bitmap bp = null;

bp = Bitmap.createBitmap(bmp, 0, 0, width, height);

view.destroyDrawingCache();

return bp;

}

/**

* 获取当前屏幕截图,不包含状态栏

*

* @param activity

* @return

*/

public static Bitmap snapShotWithoutStatusBar(Activity activity) {

View view = activity.getWindow().getDecorView();

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bmp = view.getDrawingCache();

Rect frame = new Rect();

activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

int statusBarHeight = frame.top;

int width = getDisplayWidth(activity);

int height = getDisplayHeight(activity);

Bitmap bp = null;

bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);

view.destroyDrawingCache();

return bp;

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐