Example usage for android.graphics Canvas clipRect

List of usage examples for android.graphics Canvas clipRect

Introduction

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

Prototype

public boolean clipRect(@NonNull Rect rect) 

Source Link

Document

Intersect the current clip with the specified rectangle, which is expressed in local coordinates.

Usage

From source file:com.appunite.list.ListView.java

void drawOverscrollFooter(Canvas canvas, Drawable drawable, Rect bounds) {
    final int height = drawable.getMinimumHeight();

    canvas.save();//  ww  w.j  a  v a  2 s  .c o m
    canvas.clipRect(bounds);

    final int span = bounds.bottom - bounds.top;
    if (span < height) {
        bounds.bottom = bounds.top + height;
    }

    drawable.setBounds(bounds);
    drawable.draw(canvas);

    canvas.restore();
}

From source file:org.bangbang.support.v4.widget.HListView.java

/**
     * Draws a divider for the given child in the given bounds.
     */*from   w  w  w  .  j a v a2 s . c  om*/
     * @param canvas The canvas to draw to.
     * @param bounds The bounds of the divider.
     * @param childIndex The index of child (of the View) above the divider.
     *            This will be -1 if there is no child above the divider to be
     *            drawn.
     */
    void drawDivider(Canvas canvas, Rect bounds, int childIndex) {
        // This widget draws the same divider for all children
        final Drawable divider = mDivider;
        final boolean clipDivider = mClipDivider;

        if (!clipDivider) {
            divider.setBounds(bounds);
        } else {
            canvas.save();
            canvas.clipRect(bounds);
        }

        divider.draw(canvas);

        if (clipDivider) {
            canvas.restore();
        }
    }

From source file:com.raibow.yamahaspk.filtershow.imageshow.ImageShow.java

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;// w  w w .jav a2 s. co m
    }
    MasterImage master = MasterImage.getImage();
    Matrix m = master.computeImageToScreen(image, 0, false);
    if (m == null) {
        return;
    }

    canvas.save();

    RectF d = new RectF(0, 0, image.getWidth(), image.getHeight());
    m.mapRect(d);
    d.roundOut(mImageBounds);

    boolean showAnimatedImage = master.onGoingNewLookAnimation();
    if (!showAnimatedImage && mDidStartAnimation) {
        // animation ended, but do we have the correct image to show?
        if (master.getPreset().equals(master.getCurrentPreset())) {
            // we do, let's stop showing the animated image
            mDidStartAnimation = false;
            MasterImage.getImage().resetAnimBitmap();
        } else {
            showAnimatedImage = true;
        }
    } else if (showAnimatedImage) {
        mDidStartAnimation = true;
    }

    if (showAnimatedImage) {
        canvas.save();

        // Animation uses the image before the change
        Bitmap previousImage = master.getPreviousImage();
        Matrix mp = master.computeImageToScreen(previousImage, 0, false);
        RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight());
        mp.mapRect(dp);
        Rect previousBounds = new Rect();
        dp.roundOut(previousBounds);
        float centerX = dp.centerX();
        float centerY = dp.centerY();
        boolean needsToDrawImage = true;

        if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) {
            float maskScale = MasterImage.getImage().getMaskScale();
            if (maskScale >= 0.0f) {
                float maskW = sMask.getWidth() / 2.0f;
                float maskH = sMask.getHeight() / 2.0f;
                Point point = mActivity.hintTouchPoint(this);
                float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH);
                maskScale = maskScale * maxMaskScale;
                float x = point.x - maskW * maskScale;
                float y = point.y - maskH * maskScale;

                // Prepare the shader
                mShaderMatrix.reset();
                mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale);
                mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top);
                float scaleImageX = mImageBounds.width() / (float) image.getWidth();
                float scaleImageY = mImageBounds.height() / (float) image.getHeight();
                mShaderMatrix.preScale(scaleImageX, scaleImageY);
                mMaskPaint.reset();
                mMaskPaint.setShader(createShader(image));
                mMaskPaint.getShader().setLocalMatrix(mShaderMatrix);

                drawShadow(canvas, mImageBounds); // as needed
                canvas.drawBitmap(previousImage, m, mPaint);
                canvas.clipRect(mImageBounds);
                canvas.translate(x, y);
                canvas.scale(maskScale, maskScale);
                canvas.drawBitmap(sMask, 0, 0, mMaskPaint);
                needsToDrawImage = false;
            }
        } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) {
            Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(),
                    master.getPreviousImage().getWidth());
            Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(),
                    master.getPreviousImage().getHeight());
            float finalScale = d1.width() / (float) d2.height();
            finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction());
            canvas.rotate(master.getAnimRotationValue(), centerX, centerY);
            canvas.scale(finalScale, finalScale, centerX, centerY);
        } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) {
            if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) {
                FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master
                        .getCurrentFilterRepresentation();

                ImagePreset preset = master.getPreset();
                ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset
                        .getGeometryFilters();
                GeometryMathUtils.GeometryHolder holder = null;
                holder = GeometryMathUtils.unpackGeometry(geometry);

                if (holder.rotation.value() == 90 || holder.rotation.value() == 270) {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    }
                } else {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    }
                }
            }
        }

        if (needsToDrawImage) {
            drawShadow(canvas, previousBounds); // as needed
            canvas.drawBitmap(previousImage, mp, mPaint);
        }

        canvas.restore();
    } else {
        drawShadow(canvas, mImageBounds); // as needed
        canvas.drawBitmap(image, m, mPaint);
    }

    canvas.restore();
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;//from  www  .ja  va2s.  c  o  m
    }
    MasterImage master = MasterImage.getImage();
    Matrix m = master.computeImageToScreen(image, 0, false);
    if (m == null) {
        return;
    }

    canvas.save();

    RectF d = new RectF(0, 0, image.getWidth(), image.getHeight());
    m.mapRect(d);
    d.roundOut(mImageBounds);

    boolean showAnimatedImage = master.onGoingNewLookAnimation();
    if (!showAnimatedImage && mDidStartAnimation) {
        // animation ended, but do we have the correct image to show?
        if (master.getPreset().equals(master.getCurrentPreset())) {
            // we do, let's stop showing the animated image
            mDidStartAnimation = false;
            MasterImage.getImage().resetAnimBitmap();
        } else {
            showAnimatedImage = true;
        }
    } else if (showAnimatedImage) {
        mDidStartAnimation = true;
    }

    if (showAnimatedImage) {
        canvas.save();

        // Animation uses the image before the change
        Bitmap previousImage = master.getPreviousImage();
        Matrix mp = master.computeImageToScreen(previousImage, 0, false);
        RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight());
        mp.mapRect(dp);
        Rect previousBounds = new Rect();
        dp.roundOut(previousBounds);
        float centerX = dp.centerX();
        float centerY = dp.centerY();
        boolean needsToDrawImage = true;

        if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) {
            float maskScale = MasterImage.getImage().getMaskScale();
            if (maskScale >= 0.0f) {
                float maskW = sMask.getWidth() / 2.0f;
                float maskH = sMask.getHeight() / 2.0f;
                Point point = mActivity.hintTouchPoint(this);
                float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH);
                maskScale = maskScale * maxMaskScale;
                float x = point.x - maskW * maskScale;
                float y = point.y - maskH * maskScale;

                // Prepare the shader
                mShaderMatrix.reset();
                mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale);
                mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top);
                float scaleImageX = mImageBounds.width() / (float) image.getWidth();
                float scaleImageY = mImageBounds.height() / (float) image.getHeight();
                mShaderMatrix.preScale(scaleImageX, scaleImageY);
                mMaskPaint.reset();
                Shader maskShader = createShader(image);
                maskShader.setLocalMatrix(mShaderMatrix);
                mMaskPaint.setShader(maskShader);

                drawShadow(canvas, mImageBounds); // as needed
                canvas.drawBitmap(previousImage, m, mPaint);
                canvas.clipRect(mImageBounds);
                canvas.translate(x, y);
                canvas.scale(maskScale, maskScale);
                canvas.drawBitmap(sMask, 0, 0, mMaskPaint);
                needsToDrawImage = false;
            }
        } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) {
            Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(),
                    master.getPreviousImage().getWidth());
            Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(),
                    master.getPreviousImage().getHeight());
            float finalScale = d1.width() / (float) d2.height();
            finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction());
            canvas.rotate(master.getAnimRotationValue(), centerX, centerY);
            canvas.scale(finalScale, finalScale, centerX, centerY);
        } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) {
            if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) {
                FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master
                        .getCurrentFilterRepresentation();

                ImagePreset preset = master.getPreset();
                ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset
                        .getGeometryFilters();
                GeometryMathUtils.GeometryHolder holder = null;
                holder = GeometryMathUtils.unpackGeometry(geometry);

                if (holder.rotation.value() == 90 || holder.rotation.value() == 270) {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    }
                } else {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    }
                }
            }
        }

        if (needsToDrawImage) {
            drawShadow(canvas, previousBounds); // as needed
            canvas.drawBitmap(previousImage, mp, mPaint);
        }

        canvas.restore();
    } else {
        drawShadow(canvas, mImageBounds); // as needed
        canvas.drawBitmap(image, m, mPaint);
    }

    canvas.restore();
}

From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java

private void drawEventText(StaticLayout eventLayout, Rect rect, Canvas canvas, int top, int bottom,
        boolean center) {
    // drawEmptyRect(canvas, rect, 0xFFFF00FF); // for debugging

    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;

    // If the rectangle is too small for text, then return
    if (eventLayout == null || width < MIN_CELL_WIDTH_FOR_TEXT) {
        return;/*  w  w  w  .  ja  v  a  2  s  . com*/
    }

    int totalLineHeight = 0;
    int lineCount = eventLayout.getLineCount();
    for (int i = 0; i < lineCount; i++) {
        int lineBottom = eventLayout.getLineBottom(i);
        if (lineBottom <= height) {
            totalLineHeight = lineBottom;
        } else {
            break;
        }
    }

    // + 2 is small workaround when the font is slightly bigger then the rect. This will
    // still allow the text to be shown without overflowing into the other all day rects.
    if (totalLineHeight == 0 || rect.top > bottom || rect.top + totalLineHeight + 2 < top) {
        return;
    }

    // Use a StaticLayout to format the string.
    canvas.save();
    //  canvas.translate(rect.left, rect.top + (rect.bottom - rect.top / 2));
    int padding = center ? (rect.bottom - rect.top - totalLineHeight) / 2 : 0;
    canvas.translate(rect.left, rect.top + padding);
    rect.left = 0;
    rect.right = width;
    rect.top = 0;
    rect.bottom = totalLineHeight;

    // There's a bug somewhere. If this rect is outside of a previous
    // cliprect, this becomes a no-op. What happens is that the text draw
    // past the event rect. The current fix is to not draw the staticLayout
    // at all if it is completely out of bound.
    canvas.clipRect(rect);
    eventLayout.draw(canvas);
    canvas.restore();
}