package com.test.scrolltransptoolbar;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.view.WindowManager;
/**
* Created by jameson on 12/19/15.
*/
public class ScreenUtil {
/**
* 获取屏幕宽度
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point p = new Point();
wm.getDefaultDisplay().getSize(p);
return p.x;
}
/**
* 获取屏幕高度
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point p = new Point();
wm.getDefaultDisplay().getSize(p);
return p.y;
}
/**
* dip 转 px
* @param context
* @param dpValue
* @return
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px 转 dip
* @param context
* @param pxValue
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
分享一个获取屏幕的宽高的工具类
最新推荐文章于 2024-03-18 01:36:05 发布