Example usage for android.view Window getDecorView

List of usage examples for android.view Window getDecorView

Introduction

In this page you can find the example usage for android.view Window getDecorView.

Prototype

public abstract View getDecorView();

Source Link

Document

Retrieve the top-level window decor view (containing the standard window frame/decorations and the client's content inside of that), which can be added as a window to the window manager.

Usage

From source file:Main.java

private static View findActionBar(Window w) {
    return getFirstChildByClassName((ViewGroup) w.getDecorView(),
            "com.android.internal.widget.ActionBarContainer");
}

From source file:Main.java

public static void prepare(Activity activity, int id, int width) {
    if (sCoverBitmap != null) {
        sCoverBitmap.recycle();//from  w ww.  j a  va 2 s.co m
    }
    Rect rectgle = new Rect();
    Window window = activity.getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int statusBarHeight = rectgle.top;

    ViewGroup v1 = (ViewGroup) activity.findViewById(id).getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap source = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    if (statusBarHeight != 0) {
        sCoverBitmap = Bitmap.createBitmap(source, 0, statusBarHeight, source.getWidth(),
                source.getHeight() - statusBarHeight);
        source.recycle();
    } else {
        sCoverBitmap = source;
    }
    sWidth = width;
}

From source file:Main.java

/**
 * get a screen shot with size : width X height.
 */// w w w . j  a va  2s .  com
public static Bitmap getScreenShot(Activity activity, int width, int height) {
    if (activity == null || width < 1 || height < 1) {
        return null;
    }
    Window window = activity.getWindow();
    if (window == null) {
        return null;
    }
    View decorView = window.getDecorView();
    if (decorView == null) {
        return null;
    }
    decorView.setDrawingCacheEnabled(true);
    Bitmap screenShot = decorView.getDrawingCache(true);
    if (screenShot == null) {
        return null;
    }
    Matrix matrix = new Matrix();
    matrix.postScale((float) width / screenShot.getWidth(), (float) height / screenShot.getHeight());
    Bitmap drawingCache = Bitmap.createBitmap(screenShot, 0, 0, screenShot.getWidth(), screenShot.getHeight(),
            matrix, true);
    decorView.destroyDrawingCache();
    screenShot.recycle();
    return drawingCache;
}

From source file:de.vanita5.twittnuker.util.SwipebackActivityUtils.java

/**
 * //from  w  ww . jav a  2  s.  co m
 * May cause OutOfMemoryError
 * 
 * @param activity
 * @param cacheQuality
 * @return Activity screenshot
 */
private static Bitmap getActivityScreenshotInternal(final Activity activity, final int cacheQuality) {
    if (activity == null)
        return null;
    final Window w = activity.getWindow();
    final View view = w.getDecorView();
    final int width = view.getWidth(), height = view.getHeight();
    if (width <= 0 || height <= 0)
        return null;
    final Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    final Rect frame = new Rect();
    view.getWindowVisibleDisplayFrame(frame);
    // Remove window background behind status bar.
    final Canvas c = new Canvas(b);
    view.draw(c);
    final Paint paint = new Paint();
    paint.setColor(Color.TRANSPARENT);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    c.drawRect(frame.left, 0, frame.right, frame.top, paint);
    return b;
}

From source file:Main.java

@TargetApi(19)
public static void transparencyBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = activity.getWindow();
        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);
        window.setStatusBarColor(Color.TRANSPARENT);
        window.setNavigationBarColor(Color.TRANSPARENT);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window window = activity.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }/*  w ww.  j av a2  s. co  m*/
}

From source file:Main.java

/**
 * Detects and toggles immersive mode (also known as "hidey bar" mode).
 *//*from   w  w w.j  a v  a 2 s  .  c  om*/
public static void toggleHideyBar(Window window, String TAG) {

    // The UI options currently enabled are represented by a bitfield.
    // getSystemUiVisibility() gives us that bitfield.
    int uiOptions = window.getDecorView().getSystemUiVisibility();
    int newUiOptions = uiOptions;
    boolean isImmersiveModeEnabled = ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
    if (isImmersiveModeEnabled) {
        Log.i(TAG, "Turning immersive mode mode off. ");
    } else {
        Log.i(TAG, "Turning immersive mode mode on.");
    }

    // Navigation bar hiding:  Backwards compatible to ICS.
    if (Build.VERSION.SDK_INT >= 14) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    }

    // Status bar hiding: Backwards compatible to Jellybean
    if (Build.VERSION.SDK_INT >= 16) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
    }

    // Immersive mode: Backward compatible to KitKat.
    // Note that this flag doesn't do anything by itself, it only augments the behavior
    // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
    // all three flags are being toggled together.
    // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
    // Sticky immersive mode differs in that it makes the navigation and status bars
    // semi-transparent, and the UI flag does not get cleared when the user interacts with
    // the screen.
    if (Build.VERSION.SDK_INT >= 18) {
        newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    }

    window.getDecorView().setSystemUiVisibility(newUiOptions);
}

From source file:Main.java

public static void initSystemBar(Activity activity) {
    if (android.os.Build.VERSION.SDK_INT > 18 && android.os.Build.VERSION.SDK_INT < 21) {
        Window window = activity.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    } else if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = activity.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
    }/*w ww  .  j  ava 2s  .  c om*/
}

From source file:com.nttec.everychan.ui.theme.CustomThemeHelper.java

private static void processWindow(Context context, SparseIntArray attrs, int textColorPrimaryOriginal,
        int textColorPrimaryOverridden) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        return;//from www.ja  v  a 2s . co  m
    boolean isLollipop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;

    int materialPrimaryIndex = attrs.indexOfKey(R.attr.materialPrimary);
    int materialPrimaryDarkIndex = attrs.indexOfKey(R.attr.materialPrimaryDark);
    int materialNavigationBarIndex = attrs.indexOfKey(R.attr.materialNavigationBar);
    boolean overrideActionbarColor = materialPrimaryIndex >= 0;
    boolean overridePanelsColor = Math.max(materialPrimaryDarkIndex, materialNavigationBarIndex) >= 0;

    boolean overrideTextColor = textColorPrimaryOriginal != textColorPrimaryOverridden;
    if (!overrideTextColor && !overrideActionbarColor && !overridePanelsColor)
        return;

    Window window = ((Activity) context).getWindow();
    final View decorView = window.getDecorView();
    Resources resources = context.getResources();

    if (overrideActionbarColor) {
        try {
            Drawable background = new ColorDrawable(attrs.valueAt(materialPrimaryIndex));
            Object actionBar = context.getClass().getMethod("getActionBar").invoke(context);
            actionBar.getClass().getMethod("setBackgroundDrawable", Drawable.class).invoke(actionBar,
                    background);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }

    if (overrideTextColor) {
        int id = resources.getIdentifier("action_bar_title", "id", "android");
        if (id != 0) {
            View v = decorView.findViewById(id);
            if (v instanceof TextView)
                ((TextView) v).setTextColor(textColorPrimaryOverridden);
        }
    }

    if (isLollipop && overrideTextColor) {
        try {
            int id = resources.getIdentifier("action_bar", "id", "android");
            if (id == 0)
                throw new Exception("'android:id/action_bar' identifier not found");
            View v = decorView.findViewById(id);
            if (v == null)
                throw new Exception("view with id 'android:id/action_bar' not found");
            Class<?> toolbarClass = Class.forName("android.widget.Toolbar");
            if (!toolbarClass.isInstance(v))
                throw new Exception("view 'android:id/action_bar' is not instance of android.widget.Toolbar");
            toolbarClass.getMethod("setTitleTextColor", int.class).invoke(v, textColorPrimaryOverridden);
            setLollipopMenuOverflowIconColor((ViewGroup) v, textColorPrimaryOverridden);
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }

    if (isLollipop && overridePanelsColor) {
        try {
            if (materialPrimaryDarkIndex >= 0) {
                window.getClass().getMethod("setStatusBarColor", int.class).invoke(window,
                        attrs.valueAt(materialPrimaryDarkIndex));
            }
            if (materialNavigationBarIndex >= 0) {
                window.getClass().getMethod("setNavigationBarColor", int.class).invoke(window,
                        attrs.valueAt(materialNavigationBarIndex));
            }
        } catch (Exception e) {
            Logger.e(TAG, e);
        }
    }
}

From source file:Main.java

public static void hide(Context context, Window window, int smartBarHeight) {
    if (!hasSmartBar()) {
        return;//  w ww.jav  a  2 s.  c o m
    }
    if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        return;
    }

    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    int statusBarHeight = getStatusBarHeight(context);

    window.getDecorView().setPadding(0, statusBarHeight, 0, -smartBarHeight);
}

From source file:free.yhc.netmbuddy.utils.Utils.java

public static Rect getVisibleFrame(Activity activity) {
    Rect rect = new Rect();
    Window window = activity.getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rect);
    return rect;/* ww  w .ja va2  s  .co m*/
}