You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.4 KiB
Java
64 lines
1.4 KiB
Java
package com.rehome.zhdcoa.completionUtil;
|
|
|
|
import android.content.Context;
|
|
import android.util.TypedValue;
|
|
|
|
/**
|
|
* Created by ruihong on 2018/1/29.
|
|
*/
|
|
|
|
public class DensityUtils {
|
|
private DensityUtils()
|
|
{
|
|
/* cannot be instantiated */
|
|
throw new UnsupportedOperationException("cannot be instantiated");
|
|
}
|
|
|
|
/**
|
|
* dp转px
|
|
* @param context
|
|
* @param dpVal
|
|
* @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
|
|
* @param spVal
|
|
* @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 context
|
|
* @param pxVal
|
|
* @return
|
|
*/
|
|
public static float px2sp(Context context, float pxVal){
|
|
|
|
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
|
|
}
|
|
}
|