public class DisplayUtils {
/**
* 将px值转换为dp值
*/
public static int px2dp(Context context,float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale +0.5f);
}
/**
* 将dp值转换为px值
*/
public static int dp2px(Context context,float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale +0.5f);
}
/**
* 将px值转换为sp值
*/
public static int px2sp(Context context,float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale +0.5f);
}
/**
* 将sp值转换为px值
*/
public static int sp2px(Context context,float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale +0.5f);
}
/**
* 获取屏幕宽度
*/
public static int getScreenWidthPixels(Activity context) {
DisplayMetrics metric =new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}
/**
* 获取屏幕高度
*/
public static int getScreenHeightPixels(Activity context) {
DisplayMetrics metric =new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.heightPixels;
}
public static final void hideInputWhenTouchOtherView(Activity activity, MotionEvent ev, List excludeViews) {
if (ev.getAction() == MotionEvent.ACTION_DOWN){
if (excludeViews !=null && !excludeViews.isEmpty()){
for (int i =0; i < excludeViews.size(); i++){
if (isTouchView(excludeViews.get(i), ev)){
return;
}
}
}
View v = activity.getCurrentFocus();
if (DisplayUtils.isShouldHideInput(v, ev)){
InputMethodManager inputMethodManager = (InputMethodManager)
activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager !=null){
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}
}
}
public static final void hideInputWhenTouchOtherViews(Activity activity, MotionEvent ev, List excludeViews) {
if (ev.getAction() == MotionEvent.ACTION_DOWN){
if (excludeViews !=null && !excludeViews.isEmpty()){
for (int i =0; i < excludeViews.size(); i++){
if (isTouchView(excludeViews.get(i), ev)){
return;
}
}
}
View v = activity.getCurrentFocus();
if (DisplayUtils.isShouldHideInput(v, ev)){
InputMethodManager inputMethodManager = (InputMethodManager)
activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager !=null){
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}
}
}
public static final boolean isTouchView(View view, MotionEvent event){
if (view ==null || event ==null){
return false;
}
int[] leftTop = {0,0};
view.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + view.getHeight();
int right = left + view.getWidth();
if (event.getRawX() > left && event.getRawX() < right
&& event.getRawY() > top && event.getRawY() < bottom){
return true;
}
return false;
}
public static final boolean isShouldHideInput(View v, MotionEvent event){
if (v !=null && (vinstanceof EditText)){
return !isTouchView(v, event);
}
return false;
}
}