Example usage for android.graphics Rect Rect

List of usage examples for android.graphics Rect Rect

Introduction

In this page you can find the example usage for android.graphics Rect Rect.

Prototype

public Rect() 

Source Link

Document

Create a new empty Rect.

Usage

From source file:Main.java

private static boolean showToolTip(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }//from ww w  . j  ava  2 s  .  com

    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

public static Rect getBitmapRectFromImageView(ImageView imageView) {
    Drawable drawable = imageView.getDrawable();
    Bitmap bitmap = null;/*  w  w  w  . j  av a  2 s. co m*/
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    }

    Rect rect = new Rect();
    boolean isVisible = imageView.getGlobalVisibleRect(rect);
    if (!isVisible) {
        int[] location = new int[2];
        imageView.getLocationOnScreen(location);

        rect.left = location[0];
        rect.top = location[1];
        rect.right = rect.left + imageView.getWidth();
        rect.bottom = rect.top + imageView.getHeight();
    }

    if (bitmap != null) {

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();

        int imageViewWidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
        int imageviewHeight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();

        float startScale;
        if ((float) imageViewWidth / bitmapWidth > (float) imageviewHeight / bitmapHeight) {
            // Extend start bounds horizontally
            startScale = (float) imageviewHeight / bitmapHeight;
        } else {
            startScale = (float) imageViewWidth / bitmapWidth;
        }

        bitmapHeight = (int) (bitmapHeight * startScale);
        bitmapWidth = (int) (bitmapWidth * startScale);

        int deltaX = (imageViewWidth - bitmapWidth) / 2;
        int deltaY = (imageviewHeight - bitmapHeight) / 2;

        rect.set(rect.left + deltaX, rect.top + deltaY, rect.right - deltaX, rect.bottom - deltaY);

        return rect;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * @param activity/*w  ww.  j  a  v a2s. co  m*/
 * @return > 0 success; <= 0 fail
 */
public static 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 | IllegalAccessException | InstantiationException
                | IllegalArgumentException | SecurityException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:Main.java

public static Bitmap duplicateBitmap(Bitmap bmpSrc, int width, int height) {
    if (null == bmpSrc) {
        return null;
    }//w  ww  .j a  v  a2  s. c om

    int bmpSrcWidth = bmpSrc.getWidth();
    int bmpSrcHeight = bmpSrc.getHeight();

    Bitmap bmpDest = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    if (null != bmpDest) {
        Canvas canvas = new Canvas(bmpDest);
        Rect viewRect = new Rect();
        final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);
        if (bmpSrcWidth <= width && bmpSrcHeight <= height) {
            viewRect.set(rect);
        } else if (bmpSrcHeight > height && bmpSrcWidth <= width) {
            viewRect.set(0, 0, bmpSrcWidth, height);
        } else if (bmpSrcHeight <= height && bmpSrcWidth > width) {
            viewRect.set(0, 0, width, bmpSrcWidth);
        } else if (bmpSrcHeight > height && bmpSrcWidth > width) {
            viewRect.set(0, 0, width, height);
        }
        canvas.drawBitmap(bmpSrc, rect, viewRect, null);
    }

    return bmpDest;
}

From source file:Main.java

/**
 * to judge whether items take full page
 * if not ,even if last item is visible ,we should not call listener
 *
 * @return whether items take up full page
 *//*from  w w w. j  a v  a2 s  .  c o m*/
public static boolean itemTakeFullPage(ListView list) {
    if (list.getChildCount() > 0) {
        int totalHeight = 0;
        for (int i = 0; i < list.getChildCount(); i++) {
            View child = list.getChildAt(i);
            Rect childRect = new Rect();
            child.getDrawingRect(childRect);
            totalHeight += childRect.height();
        }
        Rect visibleRect = new Rect();
        list.getGlobalVisibleRect(visibleRect);
        return (visibleRect.bottom - visibleRect.top) - totalHeight < VISIBLE_SLOP;
    }
    return false;
}

From source file:Main.java

public static int getStatusHeight(Activity activity) {
    int statusBarHeight = 0;
    try {//w  w w .  ja  va 2 s .  com
        Class<?> c = Class.forName("com.android.internal.R$dimen");
        Object o = c.newInstance();
        Field field = c.getField("status_bar_height");
        int x = (Integer) field.get(o);
        statusBarHeight = activity.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
        e.printStackTrace();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        statusBarHeight = frame.top;
    }
    return statusBarHeight;
}

From source file:Main.java

/**
 * Returns the node's bounds clipped to the size of the display
 *
 * @param node/*from  w  w  w.ja  v  a  2s.c  o  m*/
 * @param width pixel width of the display
 * @param height pixel height of the display
 * @return null if node is null, else a Rect containing visible bounds
 */
public static Rect getVisibleBoundsInScreen(AccessibilityNodeInfo node, int width, int height) {
    if (node == null) {
        return null;
    }
    // targeted node's bounds
    Rect nodeRect = new Rect();
    node.getBoundsInScreen(nodeRect);

    Rect displayRect = new Rect();
    displayRect.top = 0;
    displayRect.left = 0;
    displayRect.right = width;
    displayRect.bottom = height;

    nodeRect.intersect(displayRect);
    return nodeRect;
}

From source file:Main.java

public static void drawTextCenter(Canvas canvas, RectF rectf, String s, String s1, Paint paint, Paint paint1) {
    Rect rect = new Rect();
    paint.getTextBounds(s, 0, s.length(), rect);
    float f = rectf.left + (rectf.width() - (float) rect.width()) / 2.0F;
    float f1 = rectf.top + (rectf.height() + (float) rect.height()) / 2.0F;
    canvas.drawText(s, f, f1, paint);//from   www  .j a  va2s. c o m
    Rect rect1 = new Rect();
    paint1.getTextBounds(s, 0, s.length(), rect1);
    canvas.drawText(s1, 6F + (f + (float) rect.width()), (f1 - (float) rect.height()) + (float) rect1.height(),
            paint1);
}

From source file:Main.java

public static Rect getLocationInView(View parent, View child) {
    if (child == null || parent == null) {
        throw new IllegalArgumentException("parent and child can not be null .");
    }/*from w w w.jav a2  s  .co  m*/

    View decorView = null;
    Context context = child.getContext();
    if (context instanceof Activity) {
        decorView = ((Activity) context).getWindow().getDecorView();
    }

    Rect result = new Rect();
    Rect tmpRect = new Rect();

    View tmp = child;

    if (child == parent) {
        child.getHitRect(result);
        return result;
    }
    while (tmp != decorView && tmp != parent) {
        tmp.getHitRect(tmpRect);
        if (!tmp.getClass().equals(FRAGMENT_CON)) {
            result.left += tmpRect.left;
            result.top += tmpRect.top;
        }
        tmp = (View) tmp.getParent();
    }
    result.right = result.left + child.getMeasuredWidth();
    result.bottom = result.top + child.getMeasuredHeight();
    return result;
}

From source file:Main.java

public static int getStatusBarHeight(Activity activity) {
    Rect outRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
    return outRect.top;
}