美文网首页
屏幕相关的辅助类-ScreenUtil

屏幕相关的辅助类-ScreenUtil

作者: EraJieZhang | 来源:发表于2018-12-11 14:10 被阅读0次
    
    /**
     *屏幕相关的辅助类
     */
    public class ScreenUtil {
    
        private ScreenUtil() {
            /* cannot be instantiated */
            throw new UnsupportedOperationException("cannot be instantiated");
        }
    
    
        private static int mStatusHeight = -1;
    
        public static int dp2px(Context ctx, float dpValue) {
            final float density = ctx.getResources().getDisplayMetrics().density;
            return (int) (dpValue * density + 0.5f);
        }
    
        public static int sp2px(Context ctx, float spValue) {
            final float scaledDensity = ctx.getResources().getDisplayMetrics().scaledDensity;
            return (int) (spValue * scaledDensity + 0.5f);
        }
    
        /**
         * 获取屏幕的宽度
         * @param context
         * @return
         */
        public static int getScreenWidth(Context context) {
            WindowManager manager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            return display.getWidth();
        }
    
        /**
         * 获取屏幕的高度
         * @param context
         * @return
         */
        public static int getScreenHeight(Context context) {
            WindowManager manager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            return display.getHeight();
        }
    
        /**
         * 获得状态栏的高度
         * @param context
         * @return mStatusHeight
         */
        public static int getStatusHeight(Context context) {
            if (mStatusHeight != -1) {
                return mStatusHeight;
            }
            try {
                int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    mStatusHeight = context.getResources().getDimensionPixelSize(resourceId);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return mStatusHeight;
        }
    
    
        /**
         * 获取当前屏幕截图,不包含状态栏
         * @param activity
         * @return bp
         */
        public static Bitmap snapShotWithoutStatusBar(Activity activity) {
            View view = activity.getWindow().getDecorView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap bmp = view.getDrawingCache();
            if (bmp == null) {
                return null;
            }
            Rect frame = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int statusBarHeight = frame.top;
            Bitmap bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, bmp.getWidth(), bmp.getHeight() - statusBarHeight);
            view.destroyDrawingCache();
            view.setDrawingCacheEnabled(false);
    
            return bp;
        }
    
        /**
         * 获取actionbar的像素高度,默认使用android官方兼容包做actionbar兼容
         *
         * @return
         */
        public static int getActionBarHeight(Context context) {
            int actionBarHeight=0;
            if(context instanceof AppCompatActivity &&((AppCompatActivity) context).getSupportActionBar()!=null) {
                Log.d("isAppCompatActivity", "==AppCompatActivity");
                actionBarHeight = ((AppCompatActivity) context).getSupportActionBar().getHeight();
            }else if(context instanceof Activity && ((Activity) context).getActionBar()!=null) {
                Log.d("isActivity","==Activity");
                actionBarHeight = ((Activity) context).getActionBar().getHeight();
            }else if(context instanceof ActivityGroup){
                Log.d("ActivityGroup","==ActivityGroup");
                if (((ActivityGroup) context).getCurrentActivity() instanceof AppCompatActivity && ((AppCompatActivity) ((ActivityGroup) context).getCurrentActivity()).getSupportActionBar()!=null){
                    actionBarHeight = ((AppCompatActivity) ((ActivityGroup) context).getCurrentActivity()).getSupportActionBar().getHeight();
                }else if (((ActivityGroup) context).getCurrentActivity() instanceof Activity && ((Activity) ((ActivityGroup) context).getCurrentActivity()).getActionBar()!=null){
                    actionBarHeight = ((Activity) ((ActivityGroup) context).getCurrentActivity()).getActionBar().getHeight();
                }
            }
            if (actionBarHeight != 0)
                return actionBarHeight;
            final TypedValue tv = new TypedValue();
            if(context.getTheme().resolveAttribute( android.support.v7.appcompat.R.attr.actionBarSize, tv, true)){
                if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true))
                    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
                    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
            }else {
                if (context.getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true))
                    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
            }
            Log.d("actionBarHeight","===="+actionBarHeight);
            return actionBarHeight;
        }
    
    
        /**
         * 设置view margin
         * @param v
         * @param l
         * @param t
         * @param r
         * @param b
         */
        public static void setMargins (View v, int l, int t, int r, int b) {
            if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
                p.setMargins(l, t, r, b);
                v.requestLayout();
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:屏幕相关的辅助类-ScreenUtil

          本文链接:https://www.haomeiwen.com/subject/jnnshqtx.html