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.bitflake.counter.HorizontalPicker.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int saveCount = canvas.getSaveCount();
    canvas.save();/* w  w  w  .  j  a  v  a 2  s .c  om*/

    int selectedItem = this.selectedItem;

    float itemWithPadding = itemWidth + dividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * sideItems, 0);

    if (values != null) {
        for (int i = 0; i < values.length; i++) {

            // set text color for item
            textPaint.setColor(getTextColor(i));

            // get text layout
            BoringLayout layout = layouts[i];

            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;

            float lineWidth = layout.getLineWidth(0);
            if (lineWidth > itemWidth) {
                if (isRtl(values[i])) {
                    x += (lineWidth - itemWidth) / 2;
                } else {
                    x -= (lineWidth - itemWidth) / 2;
                }
            }

            if (marquee != null && i == selectedItem) {
                x += marquee.getScroll();
            }

            // translate vertically to center
            canvas.translate(-x, (canvas.getHeight() - layout.getHeight()) / 2);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = itemClipBounds;
            } else {
                clipBounds = itemClipBoundsOffset;
                clipBounds.set(itemClipBounds);
                clipBounds.offset(x, 0);
            }

            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (marquee != null && i == selectedItem && marquee.shouldDrawGhost()) {
                canvas.translate(marquee.getGhostOffset(), 0);
                layout.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, leftEdgeEffect, 270);
    drawEdgeEffect(canvas, rightEdgeEffect, 90);
}

From source file:kr.selfcontrol.selflocklauncher.picker.HorizontalPicker.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int saveCount = canvas.getSaveCount();
    canvas.save();//from  ww w  . j  a v  a  2  s .co m

    int selectedItem = mSelectedItem;

    float itemWithPadding = mItemWidth + mDividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * mSideItems, 0);

    if (mValues != null) {
        for (int i = 0; i < mValues.length; i++) {

            // set text color for item
            mTextPaint.setColor(getTextColor(i));

            // get text layout
            BoringLayout layout = mLayouts[i];

            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;

            float lineWidth = layout.getLineWidth(0);
            if (lineWidth > mItemWidth) {
                if (isRtl(mValues[i])) {
                    x += (lineWidth - mItemWidth) / 2;
                } else {
                    x -= (lineWidth - mItemWidth) / 2;
                }
            }

            if (mMarquee != null && i == selectedItem) {
                x += mMarquee.getScroll();
            }

            // translate vertically to center
            canvas.translate(-x, (canvas.getHeight() - layout.getHeight()) / 2);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = mItemClipBounds;
            } else {
                clipBounds = mItemClipBoundsOffser;
                clipBounds.set(mItemClipBounds);
                clipBounds.offset(x, 0);
            }

            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (mMarquee != null && i == selectedItem && mMarquee.shouldDrawGhost()) {
                canvas.translate(mMarquee.getGhostOffset(), 0);
                layout.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, mLeftEdgeEffect, 270);
    drawEdgeEffect(canvas, mRightEdgeEffect, 90);
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

/**
 * Draws a divider for the given child in the given bounds.
 * //from ww w . j ava  2s.  com
 * @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.ionesmile.variousdemo.view.HorizontalPicker.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int saveCount = canvas.getSaveCount();
    canvas.save();//w ww.  j  av  a  2s  . c  o  m

    int selectedItem = this.selectedItem;

    float itemWithPadding = itemWidth + dividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * sideItems, 0);

    if (values != null) {
        for (int i = 0; i < values.length; i++) {

            // set text color for item
            textPaint.setColor(getTextColor(i));
            textPaint.setTextSize(getTextSize(i));
            // get text layout
            BoringLayout layout = layouts[i];

            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;

            float lineWidth = layout.getLineWidth(0);
            if (lineWidth > itemWidth) {
                if (isRtl(values[i])) {
                    x += (lineWidth - itemWidth) / 2;
                } else {
                    x -= (lineWidth - itemWidth) / 2;
                }
            }

            if (marquee != null && i == selectedItem) {
                x += marquee.getScroll();
            }

            // translate vertically to center
            canvas.translate(-x, (canvas.getHeight() - layout.getHeight()) / 2);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = itemClipBounds;
            } else {
                clipBounds = itemClipBoundsOffset;
                clipBounds.set(itemClipBounds);
                clipBounds.offset(x, 0);
            }

            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (marquee != null && i == selectedItem && marquee.shouldDrawGhost()) {
                canvas.translate(marquee.getGhostOffset(), 0);
                layout.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, leftEdgeEffect, 270);
    drawEdgeEffect(canvas, rightEdgeEffect, 90);
}

From source file:de.tud.nhd.petimo.libs.HorizontalPicker.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int saveCount = canvas.getSaveCount();
    canvas.save();/* w  w  w. ja v a  2 s . co m*/

    int selectedItem = this.selectedItem;

    float itemWithPadding = itemWidth + dividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * sideItems, 0);

    if (values != null) {
        for (int i = 0; i < values.length; i++) {

            if (i == selectedItem) {
                textPaint.setColor(getResources().getColor(R.color.colorAccent));
                //textPaint.setTextSize(40);
                textPaint.setFakeBoldText(true);
            } else {
                textPaint.setColor(getTextColor(i));
                //textPaint.setTextSize(textSize);
                textPaint.setFakeBoldText(false);
            }

            // get text layout
            BoringLayout layout = layouts[i];

            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;

            float lineWidth = layout.getLineWidth(0);
            if (lineWidth > itemWidth) {
                if (isRtl(values[i])) {
                    x += (lineWidth - itemWidth) / 2;
                } else {
                    x -= (lineWidth - itemWidth) / 2;
                }
            }

            if (marquee != null && i == selectedItem) {
                x += marquee.getScroll();
            }

            // translate vertically to center
            canvas.translate(-x, (canvas.getHeight() - layout.getHeight()) / 2);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = itemClipBounds;
            } else {
                clipBounds = itemClipBoundsOffset;
                clipBounds.set(itemClipBounds);
                clipBounds.offset(x, 0);
            }

            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (marquee != null && i == selectedItem && marquee.shouldDrawGhost()) {
                canvas.translate(marquee.getGhostOffset(), 0);
                layout.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, leftEdgeEffect, 270);
    drawEdgeEffect(canvas, rightEdgeEffect, 90);
}

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

@Override
protected void onDraw(Canvas canvas) {
    if (mRemeasure) {
        remeasure(getWidth(), getHeight());
        mRemeasure = false;/* ww  w.  java  2 s. c  o m*/
    }
    canvas.save();

    float yTranslate = -mViewStartY + DAY_HEADER_HEIGHT;
    // offset canvas by the current drag and header position
    canvas.translate(-mViewStartX, yTranslate);
    // clip to everything below the allDay area
    Rect dest = mDestRect;
    dest.top = (int) (mFirstCell - yTranslate);
    dest.bottom = (int) (mViewHeight - yTranslate);
    dest.left = 0;
    dest.right = mViewWidth;
    canvas.save();
    canvas.clipRect(dest);
    // Draw the movable part of the view
    doDraw(canvas);
    // restore to having no clip
    canvas.restore();

    if ((mTouchMode & TOUCH_MODE_HSCROLL) != 0) {
        float xTranslate;
        if (mViewStartX > 0) {
            xTranslate = mViewWidth;
        } else {
            xTranslate = -mViewWidth;
        }
        // Move the canvas around to prep it for the next view
        // specifically, shift it by a screen and undo the
        // yTranslation which will be redone in the nextView's onDraw().
        canvas.translate(xTranslate, -yTranslate);
        DayView nextView = (DayView) mViewSwitcher.getNextView();

        // Prevent infinite recursive calls to onDraw().
        nextView.mTouchMode = TOUCH_MODE_INITIAL_STATE;

        nextView.onDraw(canvas);
        // Move it back for this view
        canvas.translate(-xTranslate, 0);
    } else {
        // If we drew another view we already translated it back
        // If we didn't draw another view we should be at the edge of the
        // screen
        canvas.translate(mViewStartX, -yTranslate);
    }

    // Draw the fixed areas (that don't scroll) directly to the canvas.
    drawAfterScroll(canvas);
    mComputeSelectedEvents = false;

    // Draw overscroll glow
    if (!mEdgeEffectTop.isFinished()) {
        if (DAY_HEADER_HEIGHT != 0) {
            canvas.translate(0, DAY_HEADER_HEIGHT);
        }
        if (mEdgeEffectTop.draw(canvas)) {
            invalidate();
        }
        if (DAY_HEADER_HEIGHT != 0) {
            canvas.translate(0, -DAY_HEADER_HEIGHT);
        }
    }
    if (!mEdgeEffectBottom.isFinished()) {
        canvas.rotate(180, mViewWidth / 2, mViewHeight / 2);
        if (mEdgeEffectBottom.draw(canvas)) {
            invalidate();
        }
    }
    canvas.restore();
}

From source file:com.lab47billion.appchooser.HorizontalPicker.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int saveCount = canvas.getSaveCount();
    canvas.save();//  w  ww  . j a v a 2 s .c o m

    int selectedItem = mSelectedItem;

    float itemWithPadding = mItemWidth + mDividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * mSideItems, 0);

    if (mValues != null) {
        for (int i = 0; i < mValues.length; i++) {

            mTextPaint.setColor(getTextColor(i));

            // get text layout
            View layout = mLayouts[i][0];
            //                layout.draw(canvas);
            //NEW
            View layoutSub = mLayouts[i][1];
            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;
            float lineWidth = 40f;
            if (lineWidth > mItemWidth) {
                if (isRtl(mValues[i])) {
                    x += (lineWidth - mItemWidth) / 2;
                } else {
                    x -= (lineWidth - mItemWidth) / 2;
                }
            }

            /*set select item top padding */
            int yTranslate;
            if (!isScrollingStart && i == selectedItem) {
                mTextPaint.setTextSize(mSelectedTextSize);
            } else {
                mTextPaint.setTextSize(mNormalTextSize);
            }

            // translate vertically to center
            if (layoutSub != null) {
                yTranslate = (canvas.getHeight() - (layout.getHeight() + layoutSub.getHeight())) / 2;
            } else {
                yTranslate = (canvas.getHeight() - layout.getHeight()) / 2;
            }

            if (i == selectedItem && topPadding != -1) {
                yTranslate = topPadding;
            }

            canvas.translate(-x, yTranslate);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = mItemClipBounds;
            } else {
                clipBounds = mItemClipBoundsOffser;
                clipBounds.set(mItemClipBounds);
                clipBounds.offset(x, 0);
            }
            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (layoutSub != null) {
                Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
                int heightText = (int) (Math.abs(fontMetrics.ascent) + Math.abs(fontMetrics.descent));
                canvas.translate(-x, (layout.getHeight() + heightText - getTopPadding()) / 2);
                canvas.clipRect(clipBounds);
                layoutSub.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, mLeftEdgeEffect, 270);
    drawEdgeEffect(canvas, mRightEdgeEffect, 90);
}

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

void drawOverscrollHeader(Canvas canvas, Drawable drawable, Rect bounds) {
    final int width = drawable.getMinimumWidth();

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

    final int span = bounds.right - bounds.left;
    if (span < width) {
        bounds.left = bounds.right - width;
    }

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

    canvas.restore();
}

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

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

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

    final int span = bounds.right - bounds.left;
    if (span < width) {
        bounds.right = bounds.left + width;
    }

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

    canvas.restore();
}

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

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

    canvas.save();//www.  j a va 2s  . c o m
    canvas.clipRect(bounds);

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

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

    canvas.restore();
}