Example usage for android.graphics Rect width

List of usage examples for android.graphics Rect width

Introduction

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

Prototype

public final int width() 

Source Link

Usage

From source file:com.hippo.widget.BothScrollView.java

/**
 * Compute the amount to scroll in the X direction in order to get
 * a rectangle completely on the screen (or, if taller than the screen,
 * at least the first screen size chunk of it).
 *
 * @param rect The rect.//from w w w.  ja va2s  . c o  m
 * @return The scroll delta.
 */
protected int computeScrollXDeltaToGetChildRectOnScreen(Rect rect) {
    if (getChildCount() == 0)
        return 0;

    int width = getWidth();
    int screenLeft = getScrollX();
    int screenRight = screenLeft + width;

    int fadingEdge = getHorizontalFadingEdgeLength();

    // leave room for left fading edge as long as rect isn't at very left
    if (rect.left > 0) {
        screenLeft += fadingEdge;
    }

    // leave room for right fading edge as long as rect isn't at very right
    if (rect.right < getChildAt(0).getWidth()) {
        screenRight -= fadingEdge;
    }

    int scrollXDelta = 0;

    if (rect.right > screenRight && rect.left > screenLeft) {
        // need to move right to get it in view: move right just enough so
        // that the entire rectangle is in view (or at least the first
        // screen size chunk).

        if (rect.width() > width) {
            // just enough to get screen size chunk on
            scrollXDelta += (rect.left - screenLeft);
        } else {
            // get entire rect at right of screen
            scrollXDelta += (rect.right - screenRight);
        }

        // make sure we aren't scrolling beyond the end of our content
        int right = getChildAt(0).getRight();
        int distanceToRight = right - screenRight;
        scrollXDelta = Math.min(scrollXDelta, distanceToRight);

    } else if (rect.left < screenLeft && rect.right < screenRight) {
        // need to move right to get it in view: move right just enough so that
        // entire rectangle is in view (or at least the first screen
        // size chunk of it).

        if (rect.width() > width) {
            // screen size chunk
            scrollXDelta -= (screenRight - rect.right);
        } else {
            // entire rect at left
            scrollXDelta -= (screenLeft - rect.left);
        }

        // make sure we aren't scrolling any further than the left our content
        scrollXDelta = Math.max(scrollXDelta, -getScrollX());
    }
    return scrollXDelta;
}

From source file:com.android.launcher3.CellLayout.java

private void getDirectionVectorForDrop(int dragViewCenterX, int dragViewCenterY, int spanX, int spanY,
        View dragView, int[] resultDirection) {
    int[] targetDestination = new int[2];

    findNearestArea(dragViewCenterX, dragViewCenterY, spanX, spanY, targetDestination);
    Rect dragRect = new Rect();
    regionToRect(targetDestination[0], targetDestination[1], spanX, spanY, dragRect);
    dragRect.offset(dragViewCenterX - dragRect.centerX(), dragViewCenterY - dragRect.centerY());

    Rect dropRegionRect = new Rect();
    getViewsIntersectingRegion(targetDestination[0], targetDestination[1], spanX, spanY, dragView,
            dropRegionRect, mIntersectingViews);

    int dropRegionSpanX = dropRegionRect.width();
    int dropRegionSpanY = dropRegionRect.height();

    regionToRect(dropRegionRect.left, dropRegionRect.top, dropRegionRect.width(), dropRegionRect.height(),
            dropRegionRect);/* w  w w  .ja  v a2s .  com*/

    int deltaX = (dropRegionRect.centerX() - dragViewCenterX) / spanX;
    int deltaY = (dropRegionRect.centerY() - dragViewCenterY) / spanY;

    if (dropRegionSpanX == mCountX || spanX == mCountX) {
        deltaX = 0;
    }
    if (dropRegionSpanY == mCountY || spanY == mCountY) {
        deltaY = 0;
    }

    if (deltaX == 0 && deltaY == 0) {
        // No idea what to do, give a random direction.
        resultDirection[0] = 1;
        resultDirection[1] = 0;
    } else {
        computeDirectionVector(deltaX, deltaY, resultDirection);
    }
}

From source file:com.breakout.main.GameState.java

/**
 * If appropriate, draw a message in the middle of the screen.
 *//* w  ww .  ja  va2 s .  c  o m*/
void drawMessages() {
    if (mGameStatusMessageNum != TextResources.NO_MESSAGE) {
        TexturedAlignedRect msgBox = mGameStatusMessages;

        Rect boundsRect = mTextRes.getTextureRect(mGameStatusMessageNum);
        msgBox.setTextureCoords(boundsRect);

        /*
         * We need to scale the text to be easily readable.  We have a basic choice to
         * make: do we want the message text to always be the same size (e.g. always at
         * 50 points), or should it be as large as it can be on the screen?
         *
         * For the mid-screen message, which is one or two words, we want it to be as large
         * as it can get.  The expected strings will be much wider than they are tall, so
         * we scale the width of the bounding box to be a fixed percentage of the arena
         * width.  This means the glyphs in "hello" will be much larger than they would be
         * in "hello, world", but that's exactly what we want.
         *
         * If we wanted consistent-size text, we'd need to change the way the TextResource
         * code works.  It doesn't attempt to preserve the font metrics, and the bounding
         * boxes are based on the heights of the glyphs used in a given string (i.e. not
         * all possible glyphs in the font) so we just don't have enough information in
         * here to do that.
         */

        float scale = (ARENA_WIDTH * STATUS_MESSAGE_WIDTH_PERC) / boundsRect.width();
        msgBox.setScale(boundsRect.width() * scale, boundsRect.height() * scale);

        //Log.d(TAG, "drawing " + mGameStatusMessageNum);
        msgBox.draw();
    }
}

From source file:com.albedinsky.android.ui.widget.SeekBarWidget.java

/**
 * Draws discrete interval, its thick marks depending on the current {@link #mDiscreteIntervalRatio}
 * value.//from w w w .  ja v a 2  s  . co m
 *
 * @param canvas Canvas on which to draw discrete interval.
 */
private void drawDiscreteInterval(Canvas canvas) {
    if (mDiscreteIntervalRatio == 0 || mProgressDrawable == null) {
        return;
    }

    final Rect trackBounds = mProgressDrawable.getBounds();
    final int trackLeft = getPaddingLeft();
    final int cy = trackBounds.centerY();
    float trackWidth = trackBounds.width();
    final float discreteInterval = mDiscreteIntervalRatio * trackWidth;
    trackWidth += DISCRETE_INTERVAL_TICK_MARK_INFO.radius;
    final Rect thumbBounds = mThumb != null ? mThumb.getBounds() : null;
    final int thumbOffset = getThumbOffset();

    float cx = 0;
    while (cx <= trackWidth) {
        // Ensure to not draw over thumb if it is not expected behaviour.
        final boolean isAtThumbPosition = thumbBounds != null
                && trackLeft + cx >= thumbBounds.left + thumbOffset
                && trackLeft + cx <= thumbBounds.right + thumbOffset;
        if (CAN_DRAW_DISCRETE_INTERVAL_OVER_THUMB || !isAtThumbPosition) {
            canvas.drawCircle(trackLeft + cx, cy, DISCRETE_INTERVAL_TICK_MARK_INFO.radius,
                    DISCRETE_INTERVAL_TICK_MARK_INFO.paint);
        }
        cx += discreteInterval;
    }
}

From source file:net.robotmedia.acv.ui.widget.OcrLayout.java

/** Update the location of the dictionary view based on the location of the capture box. */
@SuppressLint("NewApi")
public void updateDicViewLocation() {
    if (this.captureBox != null) {
        DicViewLocation loc = this.determineDicViewLocation();
        Rect rect = this.dicViewLocationToRect(loc);

        int width = rect.width();
        int height = rect.height();

        LayoutParams layoutParams = new LayoutParams(width, height);

        if (android.os.Build.VERSION.SDK_INT >= 11) {
            this.dicView.setLayoutParams(layoutParams);
            this.dicView.setX(rect.left);
            this.dicView.setY(rect.top);
        } else {//from   w ww  .j a  v  a 2 s . co  m
            layoutParams.leftMargin = rect.left;
            layoutParams.topMargin = rect.top;
            this.dicView.setLayoutParams(layoutParams);
        }
    }
}

From source file:com.marlonjones.voidlauncher.CellLayout.java

private boolean addViewsToTempLocation(ArrayList<View> views, Rect rectOccupiedByPotentialDrop, int[] direction,
        View dragView, ItemConfiguration currentState) {
    if (views.size() == 0)
        return true;

    boolean success = false;
    Rect boundingRect = new Rect();
    // We construct a rect which represents the entire group of views passed in
    currentState.getBoundingRectForViews(views, boundingRect);

    // Mark the occupied state as false for the group of views we want to move.
    for (View v : views) {
        CellAndSpan c = currentState.map.get(v);
        mTmpOccupied.markCells(c, false);
    }/*from  w w  w .  j av  a 2 s  .c  o  m*/

    GridOccupancy blockOccupied = new GridOccupancy(boundingRect.width(), boundingRect.height());
    int top = boundingRect.top;
    int left = boundingRect.left;
    // We mark more precisely which parts of the bounding rect are truly occupied, allowing
    // for interlocking.
    for (View v : views) {
        CellAndSpan c = currentState.map.get(v);
        blockOccupied.markCells(c.cellX - left, c.cellY - top, c.spanX, c.spanY, true);
    }

    mTmpOccupied.markCells(rectOccupiedByPotentialDrop, true);

    findNearestArea(boundingRect.left, boundingRect.top, boundingRect.width(), boundingRect.height(), direction,
            mTmpOccupied.cells, blockOccupied.cells, mTempLocation);

    // If we successfuly found a location by pushing the block of views, we commit it
    if (mTempLocation[0] >= 0 && mTempLocation[1] >= 0) {
        int deltaX = mTempLocation[0] - boundingRect.left;
        int deltaY = mTempLocation[1] - boundingRect.top;
        for (View v : views) {
            CellAndSpan c = currentState.map.get(v);
            c.cellX += deltaX;
            c.cellY += deltaY;
        }
        success = true;
    }

    // In either case, we set the occupied array as marked for the location of the views
    for (View v : views) {
        CellAndSpan c = currentState.map.get(v);
        mTmpOccupied.markCells(c, true);
    }
    return success;
}

From source file:net.robotmedia.acv.ui.widget.OcrLayout.java

/** Returns true is text capture box orientation is vertical. */
private boolean isOrientationVertical(Rect captureBox) {
    if (this.textOrientation == this.TEXT_ORIENTATION_AUTO) {
        double aspectRatio = captureBox.width() / (double) captureBox.height();

        if (aspectRatio < 2.0) {
            return true;
        } else {/*from   ww w.  j  av  a2 s  . c o  m*/
            return false;
        }
    } else {
        return (this.textOrientation == this.TEXT_ORIENTATION_VERTICAL);
    }
}

From source file:net.robotmedia.acv.ui.widget.OcrLayout.java

/** Capture screen to the lastCapture bitmap. */
private boolean captureScreen(Rect curCaptureBox) {
    View v = this.comicView;

    if ((v != null) && (curCaptureBox != null) && (curCaptureBox.width() > 0) && (curCaptureBox.height() > 0)) {
        this.clipOffset = new Point(0, 0);

        try {//from w  w w.  ja v a 2s  .  co  m
            // Only create the bitmap that contains the entire screen once
            if (this.lastScreenshot == null) {
                this.lastScreenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
                this.captureCanvas = new Canvas(this.lastScreenshot);
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            v.layout(0, 0, v.getWidth(), v.getHeight());

            // Draw the comic view to the lastScreenshot bitmap
            v.draw(this.captureCanvas);

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Get bitmap that contains only the capture box area
            Bitmap captureBmp = Bitmap.createBitmap(this.lastScreenshot, curCaptureBox.left, curCaptureBox.top,
                    curCaptureBox.width(), curCaptureBox.height());

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Create a Leptonica Pix object from the captured image
            this.lastCapture = ReadFile.readBitmap(captureBmp);

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Convert to grayscale
            this.lastCapture = Convert.convertTo8(this.lastCapture);

            if (this.lastCapture == null) {
                return false;
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            Pix tempOtsu = Binarize.otsuAdaptiveThreshold(this.lastCapture, 2000, 2000, 0, 0, 0.0f);

            if (tempOtsu == null) {
                return false;
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                tempOtsu.recycle();
                return false;
            }

            /* Get the average intensity of the border pixels */
            float aveBorderBrightness = LeptUtils.getAveBorderBrightness(tempOtsu);
            tempOtsu.recycle();

            // If background is dark
            if (aveBorderBrightness <= 0.5f) {
                // Negate image
                boolean invertStatus = this.lastCapture.invert();

                if (!invertStatus) {
                    return false;
                }
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Scale the image
            this.lastCapture = Scale.scale(this.lastCapture, this.getIdealPreProcessingScaleFactor());

            if (this.lastCapture == null) {
                return false;
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Apply unsharp mask
            this.lastCapture = Enhance.unsharpMasking(this.lastCapture, 5, 2.5f);

            if (this.lastCapture == null) {
                return false;
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Binarize
            this.lastCapture = Binarize.otsuAdaptiveThreshold(this.lastCapture, 2000, 2000, 0, 0, 0.0f);

            if (this.lastCapture == null) {
                return false;
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Remove furigana
            if (this.isOrientationVertical(this.captureBox)) {
                Furigana.eraseFuriganaVerticalText(this.lastCapture, this.getIdealPreProcessingScaleFactor());
            } else {
                Furigana.eraseFuriganaHorizontalText(this.lastCapture, this.getIdealPreProcessingScaleFactor());
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }

            // Clip foreground and add border
            if (this.isTriggerCapture || this.ocrSettingsForceBorder) {
                // Are there any foreground pixels to clip to?
                boolean canClip = Clip.testClipToForeground(this.lastCapture);

                if (canClip) {
                    // Get the top-left point of the foreground clip (will be used to offset bounding boxes)
                    this.clipOffset = LeptUtils.getForegroundClipOffset(this.lastCapture);

                    // If user adjusted capture box since this routine was called, stop here
                    if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                        return false;
                    }

                    // Remove all existing white border pixels
                    this.lastCapture = Clip.clipToForeground(this.lastCapture);

                    if (this.lastCapture == null) {
                        return false;
                    }
                }

                // If user adjusted capture box since this routine was called, stop here
                if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                    return false;
                }

                // Add an external white border
                this.lastCapture = Pix.addBlackOrWhiteBorder(this.lastCapture, 2, 2, 2, 2, false);
                this.clipOffset.x -= 2;
                this.clipOffset.y -= 2;

                this.isTriggerCapture = false;
            }

            // If user adjusted capture box since this routine was called, stop here
            if (isCaptureBoxBeingAdjusted() || !isCaptureBoxEqual(this.captureBox, curCaptureBox)) {
                return false;
            }
        } catch (Exception e) {
            // If we're here, it's probably an out-of-memory exception
            Log.e(LOG_TAG, "Exception in captureScreen()! " + e);
            return false;
        }
    } else {
        return false;
    }

    return true;
}

From source file:cn.ismartv.tvrecyclerview.widget.StaggeredGridLayoutManager.java

@Override
public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec) {
    // we don't like it to wrap content in our non-scroll direction.
    final int width, height;
    final int horizontalPadding = getPaddingLeft() + getPaddingRight();
    final int verticalPadding = getPaddingTop() + getPaddingBottom();
    if (mOrientation == VERTICAL) {
        final int usedHeight = childrenBounds.height() + verticalPadding;
        height = chooseSize(hSpec, usedHeight, getMinimumHeight());
        width = chooseSize(wSpec, mSizePerSpan * mSpanCount + horizontalPadding, getMinimumWidth());
    } else {/*  www .  j a  va 2 s .c o  m*/
        final int usedWidth = childrenBounds.width() + horizontalPadding;
        width = chooseSize(wSpec, usedWidth, getMinimumWidth());
        height = chooseSize(hSpec, mSizePerSpan * mSpanCount + verticalPadding, getMinimumHeight());
    }
    setMeasuredDimension(width, height);
}

From source file:com.klinker.android.launcher.launcher3.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//w w  w  .java  2 s  . c  o  m
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = getTextViewIcon((TextView) v);
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}