public static Bitmap takeScreenShot(Activity activity, View viewFragment) { // View是你需要截图的View View view = activity.getWindow().getDecorView(); if (view == null) { System.out.println("view is null"); } else { System.out.println("view:" + view.toString()); } // View view = viewFragment; view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); // 获取状态栏高度 Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; // 获取屏幕长和高 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b; }
/** 设置statusbar的颜色 可见 */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void setTintStatusBarAvailable(Activity activity, boolean darkMode) { Window window = activity.getWindow(); if (ApiCompatibleUtil.hasKitKat() && (!ApiCompatibleUtil.hasLollipop() || sHassMIUI6)) { // 透明状态栏 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 透明导航栏 // // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); if (sHassMIUI6) { setStatusBarDarkMode(activity, darkMode); } } else if (ApiCompatibleUtil.hasLollipop()) { window.clearFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window .getDecorView() .setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); if (darkMode) { window.setStatusBarColor(Color.parseColor("#88888888")); } else { window.setStatusBarColor(Color.TRANSPARENT); } } }
// 设置屏幕亮度[0---255] public void setBrightnessValue(Activity activity, int brightnessValue) { if (brightnessValue < 0) brightnessValue = 0; if (brightnessValue > 255) brightnessValue = 255; WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes(); layoutParams.screenBrightness = Float.valueOf(brightnessValue * (1f / 255f)); activity.getWindow().setAttributes(layoutParams); }
/** * 为 DrawerLayout 布局设置状态栏透明 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout */ public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); // 内容布局不是 LinearLayout 时,设置padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); } // 设置属性 ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1); drawerLayout.setFitsSystemWindows(false); contentLayout.setFitsSystemWindows(false); contentLayout.setClipToPadding(true); drawer.setFitsSystemWindows(false); }
public static void setCurWindowBrightness(Context context, int brightness) { // 如果开启自动亮度,则关闭。否则,设置了亮度值也是无效的 if (IsAutoBrightness(context)) { stopAutoBrightness(context); } // context转换为Activity Activity activity = (Activity) context; WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); // 异常处理 if (brightness < 1) { brightness = 1; } // 异常处理 if (brightness > 255) { brightness = 255; } lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); }
/** * 设置透明状态栏(api大于19方可使用) * * <p>可在Activity的onCreat()中调用 * * <p>需在顶部控件布局中加入以下属性让内容出现在状态栏之下 * * <p>android:clipToPadding="true" * * <p>android:fitsSystemWindows="true" * * @param activity activity */ public static void setTransparentStatusBar(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 透明状态栏 activity.getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS); // 透明导航栏 activity.getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }
public static void setActivitySizePos(Activity activity, int x, int y, int width, int height) { WindowManager.LayoutParams p = activity.getWindow().getAttributes(); p.x = x; p.y = y; p.width = width; p.height = height; activity.getWindow().setAttributes(p); }
/** setBrightness */ public static void setBrightness(Activity activity, int brightness) { // Settings.System.putInt(activity.getContentResolver(), // Settings.System.SCREEN_BRIGHTNESS_MODE, // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); }
public static void applyAlwaysSwitched(Activity context) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean screen = prefs.getBoolean("letScreenAlwaysOn", false); Log.d("EditBattleActivity.onCreate", "Settings screen is " + screen); if (screen) { context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } else { context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }
/** Enable/disable full screen mode for an activity. */ public static void setFullScreenMode(Activity a, SharedPreferences settings) { boolean fullScreenMode = settings.getBoolean("fullScreenMode", false); WindowManager.LayoutParams attrs = a.getWindow().getAttributes(); if (fullScreenMode) { attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; } else { attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); } a.getWindow().setAttributes(attrs); }
public static void tide(Activity a) { if (a != null) { // a.requestWindowFeature(Window.FEATURE_NO_TITLE); a.getWindow() .setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); a.getWindow().setBackgroundDrawableResource(R.drawable.transparent); } }
/** 使状态栏透明 */ @TargetApi(Build.VERSION_CODES.KITKAT) private static void transparentStatusBar(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } }
/** * 设置状态栏颜色(5.0以下无半透明效果,不建议使用) * * @param activity 需要设置的 activity * @param color 状态栏颜色值 */ public static void setColorDiff(Activity activity, int color) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 生成一个状态栏大小的矩形 View statusView = createStatusBarView(activity, color); // 添加 statusView 到布局中 ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); decorView.addView(statusView); setRootView(activity); }
public final boolean onSingleTapConfirmed(MotionEvent e) { Activity activity = (Activity) mContext; if (mFromChatActivity) { activity.finish(); } else { RelativeLayout titleRl = (RelativeLayout) activity.findViewById(R.id.title_bar_rl); RelativeLayout checkBoxRl = (RelativeLayout) activity.findViewById(R.id.check_box_rl); WindowManager.LayoutParams attrs = activity.getWindow().getAttributes(); // 如果标题栏,菜单栏可见,单击后隐藏并设置全屏模式 if (mTitleBarVisible) { attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; activity.getWindow().setAttributes(attrs); activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); titleRl.setVisibility(View.GONE); checkBoxRl.setVisibility(View.GONE); mTitleBarVisible = false; // 否则显示标题栏、菜单栏,并取消全屏 } else { attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); activity.getWindow().setAttributes(attrs); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); titleRl.setVisibility(View.VISIBLE); checkBoxRl.setVisibility(View.VISIBLE); mTitleBarVisible = true; } } // ImageView imageView = getImageView(); // if (null != imageView) { // if (null != mPhotoTapListener) { // final RectF displayRect = getDisplayRect(); // // if (null != displayRect) { // final float x = e.getX(), y = e.getY(); // // // Check to see if the user tapped on the photo // if (displayRect.contains(x, y)) { // // float xResult = (x - displayRect.left) / displayRect.width(); // float yResult = (y - displayRect.top) / displayRect.height(); // // mPhotoTapListener.onPhotoTap(imageView, xResult, yResult); // return true; // } // } // } // if (null != mViewTapListener) { // mViewTapListener.onViewTap(imageView, e.getX(), e.getY()); // } // } return false; }
/** * If configured via debug flags, inhibit hardware graphics acceleration. Must be called early in * onCreate(). * * <p>NOTE: Currently, this only works if HW accel is *not* enabled via the manifest. */ public static void debugSetWindowFlags(Activity activity) { if (MailActivityEmail.sDebugInhibitGraphicsAcceleration) { // Clear the flag in the activity's window activity.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); } else { // Set the flag in the activity's window activity .getWindow() .setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); } }
private void showAsPopup(Activity activity) { activity.requestWindowFeature(Window.FEATURE_ACTION_BAR); activity .getWindow() .setFlags( WindowManager.LayoutParams.FLAG_DIM_BEHIND, WindowManager.LayoutParams.FLAG_DIM_BEHIND); LayoutParams params = activity.getWindow().getAttributes(); params.height = LayoutParams.MATCH_PARENT; params.width = 850; // fixed width params.alpha = 1.0f; params.dimAmount = 0.5f; activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); }
// only useful on miui system private static void setStatusBarDarkMode(Activity activity, boolean darkmode) { Class<? extends Window> clazz = activity.getWindow().getClass(); try { int darkModeFlag = 0; Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); darkModeFlag = field.getInt(layoutParams); Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag); } catch (Exception e) { e.printStackTrace(); } }
public static void setSpinnerState(Activity a) { if (isMediaScannerScanning(a)) { // start the progress spinner a.getWindow() .setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, Window.PROGRESS_INDETERMINATE_ON); a.getWindow() .setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); } else { // stop the progress spinner a.getWindow() .setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, Window.PROGRESS_VISIBILITY_OFF); } }
/** * 设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ public static void setColor(Activity activity, int color, int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 生成一个状态栏大小的矩形 View statusView = createStatusBarView(activity, color, statusBarAlpha); // 添加 statusView 到布局中 ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); decorView.addView(statusView); setRootView(activity); } }
public static String getInfosAboutDevice(Activity a) { String s = ""; try { PackageInfo pInfo = a.getPackageManager().getPackageInfo(a.getPackageName(), PackageManager.GET_META_DATA); s += "\n APP Package Name: " + a.getPackageName(); s += "\n APP Version Name: " + pInfo.versionName; s += "\n APP Version Code: " + pInfo.versionCode; s += "\n"; } catch (NameNotFoundException e) { } s += "\n OS Version: " + System.getProperty("os.version") + " (" + android.os.Build.VERSION.INCREMENTAL + ")"; s += "\n OS API Level: " + android.os.Build.VERSION.SDK; s += "\n Device: " + android.os.Build.DEVICE; s += "\n Model (and Product): " + android.os.Build.MODEL + " (" + android.os.Build.PRODUCT + ")"; // TODO add application version! // more from // http://developer.android.com/reference/android/os/Build.html : s += "\n Manufacturer: " + android.os.Build.MANUFACTURER; s += "\n Other TAGS: " + android.os.Build.TAGS; s += "\n screenWidth: " + a.getWindow().getWindowManager().getDefaultDisplay().getWidth(); s += "\n screenHeigth: " + a.getWindow().getWindowManager().getDefaultDisplay().getHeight(); s += "\n Keyboard available: " + (a.getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS); s += "\n Trackball available: " + (a.getResources().getConfiguration().navigation == Configuration.NAVIGATION_TRACKBALL); s += "\n SD Card state: " + Environment.getExternalStorageState(); Properties p = System.getProperties(); Enumeration keys = p.keys(); String key = ""; while (keys.hasMoreElements()) { key = (String) keys.nextElement(); s += "\n > " + key + " = " + (String) p.get(key); } return s; }
/** * 获取状态栏高度 * * @param activity * @return */ public int getStatusHeight(Activity activity) { int statusHeight = 0; Rect localRect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect); statusHeight = localRect.top; if (0 == statusHeight) { Class<?> localClass; try { localClass = Class.forName("com.android.internal.R$dimen"); Object localObject = localClass.newInstance(); int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString()); statusHeight = activity.getResources().getDimensionPixelSize(i5); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } return statusHeight; }
/** * 是否隐藏title,是否全屏等 * * @param act 需要隐藏界面 * @param strategy 策略类型 1-隐藏title,2-全屏,3-隐藏title并全屏 * @return void */ public static void strategyUi(Activity act, int strategy) { if (strategy == 1) { act.requestWindowFeature(Window.FEATURE_NO_TITLE); } else if (strategy == 2) { act.getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else if (strategy == 3) { act.requestWindowFeature(Window.FEATURE_NO_TITLE); act.getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } }
public static void hideSoftKeyboard(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getApplicationContext().getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow( activity.getWindow().getDecorView().getRootView().getWindowToken(), 0); }
@Test public void destroy_shouldCallClear_shouldRemoveListenerFromDecorView() throws Exception { Activity activity1 = mock(Activity.class); Window window = mock(Window.class); View decorView = mock(View.class); ViewTreeObserver viewTreeObserver = mock(ViewTreeObserver.class); when(activity1.getWindow()).thenReturn(window); when(window.getDecorView()).thenReturn(decorView); when(decorView.getViewTreeObserver()).thenReturn(viewTreeObserver); when(viewTreeObserver.isAlive()).thenReturn(true); subject = new VisibilityTracker(activity1, trackedViews, visibilityChecker, visibilityHandler); subject.addView(view, MIN_PERCENTAGE_VIEWED); subject.addView(view2, MIN_PERCENTAGE_VIEWED); assertThat(trackedViews).hasSize(2); subject.destroy(); assertThat(trackedViews).isEmpty(); verify(visibilityHandler).removeMessages(0); verify(viewTreeObserver).removeOnPreDrawListener(any(OnPreDrawListener.class)); assertThat(subject.mOnPreDrawListener).isNull(); }
/** * 使状态栏透明(5.0以上半透明效果,不建议使用) * * <p>适用于图片作为背景的界面,此时需要图片填充到状态栏 * * @param activity 需要设置的activity */ public static void setTranslucentDiff(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 设置状态栏透明 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); setRootView(activity); } }
/** * 获取当前屏幕截图,不包含状态栏 * * @param activity * @return */ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; }
@SuppressWarnings("deprecation") public void onActivityCreate() { mActivity.getWindow().setBackgroundDrawable(new ColorDrawable(0)); mActivity.getWindow().getDecorView().setBackgroundDrawable(null); mSwipeBackLayout = (SwipeBackLayout) LayoutInflater.from(mActivity).inflate(com.zxq.xmpp.R.layout.swipeback_layout, null); mSwipeBackLayout.addSwipeListener( new SwipeBackLayout.SwipeListener() { @Override public void onScrollStateChange(int state, float scrollPercent) { if (state == SwipeBackLayout.STATE_IDLE && scrollPercent == 0) { convertActivityFromTranslucent(); } } @Override public void onEdgeTouch(int edgeFlag) { convertActivityToTranslucent(); } @Override public void onScrollOverThreshold() {} }); }
@Kroll.method public void takeScreenshot(KrollFunction callback) { Activity a = TiApplication.getAppCurrentActivity(); if (a == null) { Log.w(TAG, "Could not get current activity for takeScreenshot.", Log.DEBUG_MODE); callback.callAsync(getKrollObject(), new Object[] {null}); return; } while (a.getParent() != null) { a = a.getParent(); } Window w = a.getWindow(); while (w.getContainer() != null) { w = w.getContainer(); } KrollDict image = TiUIHelper.viewToImage(null, w.getDecorView()); if (callback != null) { callback.callAsync(getKrollObject(), new Object[] {image}); } }
/** * Shows material view with fade in animation * * @param activity */ private void show(Activity activity) { if (preferencesManager.isDisplayed(materialIntroViewId)) return; ((ViewGroup) activity.getWindow().getDecorView()).addView(this); setReady(true); handler.postDelayed( new Runnable() { @Override public void run() { if (isFadeAnimationEnabled) AnimationFactory.animateFadeIn( MaterialIntroView.this, fadeAnimationDuration, new AnimationListener.OnAnimationStartListener() { @Override public void onAnimationStart() { setVisibility(VISIBLE); } }); else setVisibility(VISIBLE); } }, delayMillis); }
/** * 获取状态栏高度 * * @param activity * @return */ public static int getStatusBarHeight(Activity activity) { Rect rect = new Rect(); Window win = activity.getWindow(); win.getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; return statusBarHeight; }