Example usage for android.view View getWindowVisibleDisplayFrame

List of usage examples for android.view View getWindowVisibleDisplayFrame

Introduction

In this page you can find the example usage for android.view View getWindowVisibleDisplayFrame.

Prototype

public void getWindowVisibleDisplayFrame(Rect outRect) 

Source Link

Document

Retrieve the overall visible display size in which the window this view is attached to has been positioned in.

Usage

From source file:Main.java

/**
 * Check whether is keyboard showing or hiding
 * @param rootView/* w  w  w.  j  a  va  2  s.  co  m*/
 * @return
 */
public static boolean isKeyboardShown(View rootView) {
    Rect rect = new Rect();
    rootView.getWindowVisibleDisplayFrame(rect);
    /* 
     * 128dp = 32dp * 4
     * Minimum button height 32dp and generic 4 rows soft keyboard (can be more than 4 rows)
     */
    final int SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD = 128;
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    return dm.heightPixels - rect.bottom > SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD * dm.density;
}

From source file:Main.java

public static int getStatusBarHeight(View v) {
    if (v == null) {
        return 0;
    }/*w w  w  . j  ava  2s  .  c om*/
    Rect frame = new Rect();
    v.getWindowVisibleDisplayFrame(frame);
    return frame.top;
}

From source file:Main.java

public static int getStatusBarHeight(View v) {
    if (v == null) {
        return 0;
    }/*from  w  ww . j  a va  2s  .c om*/

    Rect frame = new Rect();
    v.getWindowVisibleDisplayFrame(frame);
    return frame.top;
}

From source file:Main.java

public static Bitmap getCrop(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.buildDrawingCache();//from ww w .ja v a2  s  .  c  o  m

    Rect rect = new Rect();
    view.getWindowVisibleDisplayFrame(rect);
    int stateBarHeight = rect.top;

    view.setDrawingCacheEnabled(true);

    Bitmap bmpCache = view.getDrawingCache();
    Bitmap bmp = Bitmap.createBitmap(bmpCache, 0, stateBarHeight, bmpCache.getWidth(),
            bmpCache.getHeight() - stateBarHeight);

    view.destroyDrawingCache();

    return bmp;
}

From source file:Main.java

public static boolean isKeyboardShown(View rootView) {
    // 128dp = 32dp * 4, minimum button height 32dp and generic 4 rows soft keyboard
    final int SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD = 128;

    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    // heightDiff = rootView height - status bar height (r.top) - visible frame height (r.bottom - r.top)
    int heightDiff = rootView.getBottom() - r.bottom;
    // Threshold size: dp to pixels, multiply with display density
    boolean isKeyboardShown = heightDiff > SOFT_KEYBOARD_HEIGHT_DP_THRESHOLD * dm.density;

    //        Log.d(TAG, "isKeyboardShown ? " + isKeyboardShown + ", heightDiff:" + heightDiff + ", density:" + dm.density
    //                + "root view height:" + rootView.getHeight() + ", rect:" + r);

    return isKeyboardShown;
}

From source file:Main.java

/**
 * Display a {@link Toast} letting the user know what an item does when long
 * pressed.//from   w  w w.j  av  a  2  s  .  c  o m
 * 
 * @param view The {@link View} to copy the content description from.
 */
public static void showCheatSheet(final View view) {

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g.
                                          // status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (48 * context.getResources().getDisplayMetrics().density);

    final Toast cheatSheet = Toast.makeText(context, view.getContentDescription(), Toast.LENGTH_SHORT);
    final boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                displayFrame.bottom - screenPos[1]);
    }
    cheatSheet.show();
}

From source file:Main.java

private static boolean showToolTip(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }/*from   w  w  w. j a va  2  s .  co  m*/

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
            * context.getResources().getDisplayMetrics().density);

    Toast cheatSheet = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
        // its height isn't factored in.
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    cheatSheet.show();
    return true;
}

From source file:Main.java

/**
 * Internal helper method to show the cheat sheet toast.
 *///  ww w .  ja  va  2s .c  o  m
private static boolean show(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
            * context.getResources().getDisplayMetrics().density);

    Toast cheatSheet = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
        // its height isn't factored in.
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    cheatSheet.show();
    return true;
}

From source file:com.dmcc.bid.util.AnimatorUtil.java

/**
 * ??/* w ww. j  a  va 2  s  . c  o  m*/
 * <p/>
 * ?view?
 * ?
 *
 * @param root
 */
public static void addGlobaLayoutListener(final View root, final View childView) {
    root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect rect = new Rect();
            // ?root?
            root.getWindowVisibleDisplayFrame(rect);
            // ?root??(View?)
            int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
            // ??100
            if (rootInvisibleHeight > 100) {
                //                            Logger.e("?");
                int[] location = new int[2];
                AnimatorUtil.scaleHide(childView, new ViewPropertyAnimatorListener() {
                    @Override
                    public void onAnimationStart(View view) {
                        childView.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationEnd(View view) {

                    }

                    @Override
                    public void onAnimationCancel(View view) {

                    }
                });
            } else {
                // ??
                //                            Logger.e("");
                AnimatorUtil.scaleShow(childView, new ViewPropertyAnimatorListener() {
                    @Override
                    public void onAnimationStart(View view) {

                    }

                    @Override
                    public void onAnimationEnd(View view) {

                        childView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationCancel(View view) {

                    }
                });
            }
        }
    });
}

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

/**
 * /*ww  w  .  jav  a 2  s.c  o 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;
}