Example usage for android.graphics RectF RectF

List of usage examples for android.graphics RectF RectF

Introduction

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

Prototype

public RectF() 

Source Link

Document

Create a new empty RectF.

Usage

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

@Override
protected void handleClick(Point p) {
    final int c = getChildCount();
    View v;/*from   ww w  .  ja  v  a 2  s .co  m*/
    final RectF r = new RectF();
    final int[] childOrder = new int[c];

    for (int i = 0; i < c; i++) {
        childOrder[i] = getChildDrawingOrder(c, i);
    }

    for (int i = c - 1; i >= 0; i--) {
        v = getChildAt(childOrder[i]); //we need reverse drawing order. Check children drawn last
        // first
        getScrolledTransformedChildRectangle(v, r);
        if (r.contains(p.x, p.y)) {
            final View old = getSelectedView();
            if (old != null)
                old.setSelected(false);

            int position = mFirstItemPosition + childOrder[i];
            if (position >= mAdapter.getCount())
                position = position - mAdapter.getCount();

            mSelectedPosition = position;
            v.setSelected(true);

            if (mOnItemClickListener != null)
                mOnItemClickListener.onItemClick(this, v, position, getItemIdAtPosition(position));
            if (mOnItemSelectedListener != null)
                mOnItemSelectedListener.onItemSelected(this, v, position, getItemIdAtPosition(position));

            break;
        }
    }
}

From source file:com.pdftron.pdf.tools.Tool.java

/**
 * Computes the page bounding box in the client space.
 *///from   w w  w  . j  a  va 2s  .  c  o m
protected RectF buildPageBoundBoxOnClient(int page_num) {
    RectF rect = null;
    if (page_num >= 1) {
        try {
            mPDFView.docLockRead();
            Page page = mPDFView.getDoc().getPage(page_num);
            if (page != null) {
                rect = new RectF();
                com.pdftron.pdf.Rect r = page.getBox(mPDFView.getPageBox());

                double x1 = r.getX1();
                double y1 = r.getY1();
                double x2 = r.getX2();
                double y2 = r.getY2();
                double[] pts1, pts2, pts3, pts4;

                // Need to compute the transformed coordinates for the four
                // corners of the page bounding box, since a page can be rotated.
                pts1 = mPDFView.convPagePtToScreenPt(x1, y1, page_num);
                pts2 = mPDFView.convPagePtToScreenPt(x2, y1, page_num);
                pts3 = mPDFView.convPagePtToScreenPt(x2, y2, page_num);
                pts4 = mPDFView.convPagePtToScreenPt(x1, y2, page_num);

                double min_x = Math.min(Math.min(Math.min(pts1[0], pts2[0]), pts3[0]), pts4[0]);
                double max_x = Math.max(Math.max(Math.max(pts1[0], pts2[0]), pts3[0]), pts4[0]);
                double min_y = Math.min(Math.min(Math.min(pts1[1], pts2[1]), pts3[1]), pts4[1]);
                double max_y = Math.max(Math.max(Math.max(pts1[1], pts2[1]), pts3[1]), pts4[1]);

                float sx = mPDFView.getScrollX();
                float sy = mPDFView.getScrollY();
                rect = new RectF();
                rect.set((float) min_x + sx, (float) min_y + sy, (float) max_x + sx, (float) max_y + sy);
            }

        } catch (Exception e) {

        } finally {
            mPDFView.docUnlockRead();
        }
    }
    return rect;
}

From source file:org.akop.crosswords.view.CrosswordView.java

private void renderSelection(Canvas canvas, boolean clearSelection) {
    if (mSelectedWord == null) {
        return;// w ww . j  a v  a2 s  .  c  o  m
    }

    int startRow = mSelectedWord.getStartRow();
    int endRow = startRow;
    int startColumn = mSelectedWord.getStartColumn();
    int endColumn = startColumn;
    RectF cellRect = new RectF();

    canvas.save();
    canvas.scale(mRenderScale, mRenderScale);

    if (mSelectedWord.getDirection() == Crossword.Word.DIR_ACROSS) {
        endColumn += mSelectedWord.getLength() - 1;
    } else {
        endRow += mSelectedWord.getLength() - 1;
    }

    float top = mSelectedWord.getStartRow() * mCellSize;
    for (int row = startRow, index = 0; row <= endRow; row++, top += mCellSize) {
        float left = mSelectedWord.getStartColumn() * mCellSize;
        for (int column = startColumn; column <= endColumn; column++, left += mCellSize) {
            Cell cell = mPuzzleCells[row][column];
            if (cell != null) {
                // Draw the unselected cell
                Paint paint;
                if (clearSelection) {
                    paint = mCellFillPaint;
                } else {
                    if (index == mSelectedCell) {
                        paint = mSelectedCellFillPaint;
                    } else {
                        paint = mSelectedWordFillPaint;
                    }
                }

                cellRect.set(left, top, left + mCellSize, top + mCellSize);
                renderCell(canvas, cell, cellRect, paint);
            }

            index++;
        }
    }

    canvas.restore();
}

From source file:org.akop.ararat.view.CrosswordView.java

private void bringIntoView(Selectable sel) {
    if (sel == null) {
        return;//from  ww w  .j a  v  a2  s.c  o  m
    }

    RectF wordRect = new RectF();

    wordRect.left = sel.getStartColumn() * mScaledCellSize - mContentRect.left;
    wordRect.top = sel.getStartRow() * mScaledCellSize - mContentRect.top;

    if (sel.getDirection() == Crossword.Word.DIR_ACROSS) {
        wordRect.right = wordRect.left + sel.mWord.getLength() * mScaledCellSize;
        wordRect.bottom = wordRect.top + mScaledCellSize;
    } else if (sel.getDirection() == Crossword.Word.DIR_DOWN) {
        wordRect.right = wordRect.left + mScaledCellSize;
        wordRect.bottom = wordRect.top + sel.mWord.getLength() * mScaledCellSize;
    }

    RectF objectRect = new RectF(wordRect);
    RectF visibleArea = new RectF(-mBitmapOffset.x, -mBitmapOffset.y, -mBitmapOffset.x + mContentRect.width(),
            -mBitmapOffset.y + mContentRect.height());

    if (visibleArea.contains(objectRect)) {
        return; // Already visible
    }

    if (objectRect.width() > visibleArea.width() || objectRect.height() > visibleArea.height()) {
        // Available area isn't large enough to fit the entire word
        // Is the selected cell visible? If not, bring it into view

        RectF cellRect = new RectF();
        cellRect.left = sel.getColumn() * mScaledCellSize;
        cellRect.top = sel.getRow() * mScaledCellSize;
        cellRect.right = cellRect.left + mScaledCellSize;
        cellRect.bottom = cellRect.top + mScaledCellSize;

        if (visibleArea.contains(cellRect)) {
            return; // Already visible
        }

        objectRect.set(cellRect);
    }

    // Compute view that includes the object in the center
    PointF end = new PointF((visibleArea.width() - objectRect.width()) / 2.0f - objectRect.left,
            (visibleArea.height() - objectRect.height()) / 2.0f - objectRect.top);

    // Clamp the values
    clampPointF(end, mTranslationBounds);

    // Compute the distance to travel from current location
    float distanceX = end.x - mBitmapOffset.x;
    float distanceY = end.y - mBitmapOffset.y;

    // Scroll the point into view
    mScroller.startScroll((int) mBitmapOffset.x, (int) mBitmapOffset.y, (int) distanceX, (int) distanceY,
            NAVIGATION_SCROLL_DURATION_MS);
}

From source file:me.ccrama.redditslide.Views.SubsamplingScaleImageView.java

/**
 * Draw method should not be called until the view has dimensions so the first calls are used as triggers to calculate
 * the scaling and tiling required. Once the view is setup, tiles are displayed as they are loaded.
 *//* ww  w .  j  a  v a2  s.c  om*/
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    createPaints();

    // If image or view dimensions are not known yet, abort.
    if (sWidth == 0 || sHeight == 0 || getWidth() == 0 || getHeight() == 0) {
        return;
    }

    // When using tiles, on first render with no tile map ready, initialise it and kick off async base image loading.
    if (tileMap == null && decoder != null) {
        initialiseBaseLayer(getMaxBitmapDimensions(canvas));
    }

    // If image has been loaded or supplied as a bitmap, onDraw may be the first time the view has
    // dimensions and therefore the first opportunity to set scale and translate. If this call returns
    // false there is nothing to be drawn so return immediately.
    if (!checkReady()) {
        return;
    }

    // Set scale and translate before draw.
    preDraw();

    // If animating scale, calculate current scale and center with easing equations
    if (anim != null) {
        long scaleElapsed = System.currentTimeMillis() - anim.time;
        boolean finished = scaleElapsed > anim.duration;
        scaleElapsed = Math.min(scaleElapsed, anim.duration);
        setScale(ease(anim.easing, scaleElapsed, anim.scaleStart, anim.scaleEnd - anim.scaleStart,
                anim.duration));

        // Apply required animation to the focal point
        float vFocusNowX = ease(anim.easing, scaleElapsed, anim.vFocusStart.x,
                anim.vFocusEnd.x - anim.vFocusStart.x, anim.duration);
        float vFocusNowY = ease(anim.easing, scaleElapsed, anim.vFocusStart.y,
                anim.vFocusEnd.y - anim.vFocusStart.y, anim.duration);
        // Find out where the focal point is at this scale and adjust its position to follow the animation path
        vTranslate.x -= sourceToViewX(anim.sCenterEnd.x) - vFocusNowX;
        vTranslate.y -= sourceToViewY(anim.sCenterEnd.y) - vFocusNowY;

        // For translate anims, showing the image non-centered is never allowed, for scaling anims it is during the animation.
        fitToBounds(finished || (anim.scaleStart == anim.scaleEnd));
        refreshRequiredTiles(finished);
        if (finished) {
            if (anim.listener != null) {
                try {
                    anim.listener.onComplete();
                } catch (Exception e) {
                    Log.w(TAG, "Error thrown by animation listener", e);
                }
            }
            anim = null;
        }
        invalidate();
    }

    if (tileMap != null && isBaseLayerReady()) {

        // Optimum sample size for current scale
        int sampleSize = Math.min(fullImageSampleSize, calculateInSampleSize(scale));

        // First check for missing tiles - if there are any we need the base layer underneath to avoid gaps
        boolean hasMissingTiles = false;
        for (Map.Entry<Integer, List<Tile>> tileMapEntry : tileMap.entrySet()) {
            if (tileMapEntry.getKey() == sampleSize) {
                for (Tile tile : tileMapEntry.getValue()) {
                    if (tile.visible && (tile.loading || tile.bitmap == null)) {
                        hasMissingTiles = true;
                    }
                }
            }
        }

        // Render all loaded tiles. LinkedHashMap used for bottom up rendering - lower res tiles underneath.
        for (Map.Entry<Integer, List<Tile>> tileMapEntry : tileMap.entrySet()) {
            if (tileMapEntry.getKey() == sampleSize || hasMissingTiles) {
                for (Tile tile : tileMapEntry.getValue()) {
                    sourceToViewRect(tile.sRect, tile.vRect);
                    if (!tile.loading && tile.bitmap != null) {
                        if (tileBgPaint != null) {
                            canvas.drawRect(tile.vRect, tileBgPaint);
                        }
                        if (matrix == null) {
                            matrix = new Matrix();
                        }
                        matrix.reset();
                        setMatrixArray(srcArray, 0, 0, tile.bitmap.getWidth(), 0, tile.bitmap.getWidth(),
                                tile.bitmap.getHeight(), 0, tile.bitmap.getHeight());
                        if (getRequiredRotation() == ORIENTATION_0) {
                            setMatrixArray(dstArray, tile.vRect.left, tile.vRect.top, tile.vRect.right,
                                    tile.vRect.top, tile.vRect.right, tile.vRect.bottom, tile.vRect.left,
                                    tile.vRect.bottom);
                        } else if (getRequiredRotation() == ORIENTATION_90) {
                            setMatrixArray(dstArray, tile.vRect.right, tile.vRect.top, tile.vRect.right,
                                    tile.vRect.bottom, tile.vRect.left, tile.vRect.bottom, tile.vRect.left,
                                    tile.vRect.top);
                        } else if (getRequiredRotation() == ORIENTATION_180) {
                            setMatrixArray(dstArray, tile.vRect.right, tile.vRect.bottom, tile.vRect.left,
                                    tile.vRect.bottom, tile.vRect.left, tile.vRect.top, tile.vRect.right,
                                    tile.vRect.top);
                        } else if (getRequiredRotation() == ORIENTATION_270) {
                            setMatrixArray(dstArray, tile.vRect.left, tile.vRect.bottom, tile.vRect.left,
                                    tile.vRect.top, tile.vRect.right, tile.vRect.top, tile.vRect.right,
                                    tile.vRect.bottom);
                        }
                        matrix.setPolyToPoly(srcArray, 0, dstArray, 0, 4);
                        canvas.drawBitmap(tile.bitmap, matrix, bitmapPaint);
                        if (debug) {
                            canvas.drawRect(tile.vRect, debugPaint);
                        }
                    } else if (tile.loading && debug) {
                        canvas.drawText("LOADING", tile.vRect.left + 5, tile.vRect.top + 35, debugPaint);
                    }
                    if (tile.visible && debug) {
                        canvas.drawText(
                                "ISS " + tile.sampleSize + " RECT " + tile.sRect.top + "," + tile.sRect.left
                                        + "," + tile.sRect.bottom + "," + tile.sRect.right,
                                tile.vRect.left + 5, tile.vRect.top + 15, debugPaint);
                    }
                }
            }
        }

        if (debug) {
            canvas.drawText("Scale: " + String.format("%.2f", scale), 5, 15, debugPaint);
            canvas.drawText("Translate: " + String.format("%.2f", vTranslate.x) + ":"
                    + String.format("%.2f", vTranslate.y), 5, 35, debugPaint);
            PointF center = getCenter();
            canvas.drawText(
                    "Source center: " + String.format("%.2f", center.x) + ":" + String.format("%.2f", center.y),
                    5, 55, debugPaint);

            if (anim != null) {
                PointF vCenterStart = sourceToViewCoord(anim.sCenterStart);
                PointF vCenterEndRequested = sourceToViewCoord(anim.sCenterEndRequested);
                PointF vCenterEnd = sourceToViewCoord(anim.sCenterEnd);
                canvas.drawCircle(vCenterStart.x, vCenterStart.y, 10, debugPaint);
                canvas.drawCircle(vCenterEndRequested.x, vCenterEndRequested.y, 20, debugPaint);
                canvas.drawCircle(vCenterEnd.x, vCenterEnd.y, 25, debugPaint);
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, 30, debugPaint);
            }
        }

    } else if (bitmap != null) {

        float xScale = scale, yScale = scale;
        if (bitmapIsPreview) {
            xScale = scale * ((float) sWidth / bitmap.getWidth());
            yScale = scale * ((float) sHeight / bitmap.getHeight());
        }

        if (matrix == null) {
            matrix = new Matrix();
        }
        matrix.reset();
        matrix.postScale(xScale, yScale);
        matrix.postRotate(getRequiredRotation());
        matrix.postTranslate(vTranslate.x, vTranslate.y);

        if (getRequiredRotation() == ORIENTATION_180) {
            matrix.postTranslate(scale * sWidth, scale * sHeight);
        } else if (getRequiredRotation() == ORIENTATION_90) {
            matrix.postTranslate(scale * sHeight, 0);
        } else if (getRequiredRotation() == ORIENTATION_270) {
            matrix.postTranslate(0, scale * sWidth);
        }

        if (tileBgPaint != null) {
            if (sRect == null) {
                sRect = new RectF();
            }
            sRect.set(0f, 0f, sWidth, sHeight);
            matrix.mapRect(sRect);
            canvas.drawRect(sRect, tileBgPaint);
        }
        canvas.drawBitmap(bitmap, matrix, bitmapPaint);

    }
}