Android屏幕和尺寸
DisplayMetrics dm=new DisplayMetrics(); //获取的像素高度不包含虚拟键所占空间 ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay() .getMetrics(dm); int width=dm.widthPixels;//屏幕宽度(像素) int height=dm.heightPixels;//屏幕高度(像素) float density=dm.density;//dp缩放因子(0.75/1.0/1.5/3.0) int densityDpi=dm.densityDpi;//广义密度(120/160/240/480) float xdpi=dm.xdpi;//x轴方向的真实密度 float ydpi=dm.ydpi;//y轴方向的真实密度 09-20 22:35:35.246 6130-6130/? I/UiUtils: getScreenWidthPixels: widthPixels=1080, heightPixels=1776 09-20 22:35:35.246 6130-6130/? I/UiUtils: getScreenWidthPixels: density=3.0, densityDpi=480 09-20 22:35:35.246 6130-6130/? I/UiUtils: getScreenWidthPixels: xdpi=442.451, ydpi=443.345 getRealMetrics()和getMetrics()获取到的屏幕信息差别只在于widthPixels或heightPixels的值是否去除虚拟键所占用的像素,和是否全屏和沉浸模式无关。 来自 <http://xiaoyaozjl.iteye.com/blog/2178415> 使用getRealMetrics() 09-20 22:45:45.793 3190-3190/? I/UiUtils: getScreenWidthPixels: widthPixels=1080, heightPixels=1920 09-20 22:45:45.793 3190-3190/? I/UiUtils: getScreenWidthPixels: density=3.0, densityDpi=480 09-20 22:45:45.794 3190-3190/? I/UiUtils: getScreenWidthPixels: xdpi=442.451, ydpi=443.345
传递点击事件的同时拦截滑动事件
/*dispatch click event but intercept sliding event in the listView*/ recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { LinearLayout linearLayout; boolean mScrolling; boolean mIsTouchInListView; float touchDownx; @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { int x = (int) e.getRawX(); int y = (int) e.getRawY(); linearLayout = (LinearLayout) rv.findViewById(R.id.menu_layout); mIsTouchInListView = !isTouchPointInView(linearLayout, x, y); switch(e.getAction()){ case MotionEvent.ACTION_DOWN: touchDownx = e.getX(); mScrolling = false; break; case MotionEvent.ACTION_MOVE: if(Math.abs(touchDownx -e.getX()) >= ViewConfiguration.get(mActivity).getScaledTouchSlop()){ mScrolling = true; }else{ mScrolling = false; } break; case MotionEvent.ACTION_UP: mScrolling =false; break; } Log.i(TAG, "onInterceptTouchEvent: "+mIsTouchInListView+","+mScrolling); return (mIsTouchInListView && mScrolling); } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { Log.i(TAG, "onTouchEvent: "); } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} });
全屏
Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; window.setAttributes(params);
Dialog弹出时避免退出全屏
AlertDialog alertDialog = dialogBuilder.create(); alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); DialogViewManager.fullScreenImmersive(alertDialog.getWindow().getDecorView()); alertDialog.show(); alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
Android锁屏状态启动Activity
Window win = getWindow(); WindowManager.LayoutParams params = win.getAttributes(); params = win.getAttributes(); params.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; params.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON; PowerManager pm = ((PowerManager) getSystemService(POWER_SERVICE)); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); mWakeLock.acquire(); Log.d(TAG, "acquire wake lock"); win.setAttributes(params);