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(int left, int top, int right, int bottom) 

Source Link

Document

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

Usage

From source file:android.support.v7.widget.DividerItemDecoration.java

private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();//from  ww w .  j a v  a  2s .  c  o  m
    final int top;
    final int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:com.by_syk.lib.nanoiconpack.widget.DividerItemDecoration.java

@SuppressLint("NewApi")
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();/* ww  w . ja v a2  s .  c  om*/
    final int top;
    final int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:com.by_syk.schttable.widget.DividerItemDecoration.java

@SuppressLint("NewApi")
private void drawVertical(Canvas canvas, RecyclerView parent) {
    canvas.save();//from ww  w.  j av a  2s .  co  m
    final int left;
    final int right;
    if (parent.getClipToPadding()) {
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
    } else {
        left = 0;
        right = parent.getWidth();
    }

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getDecoratedBoundsWithMargins(child, mBounds);
        final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
        final int top = bottom - mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:com.by_syk.lib.nanoiconpack.widget.DividerItemDecoration.java

@SuppressLint("NewApi")
private void drawVertical(Canvas canvas, RecyclerView parent) {
    canvas.save();//from w  w  w .  j a v a 2s.c  o m
    final int left;
    final int right;
    if (parent.getClipToPadding()) {
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
    } else {
        left = 0;
        right = parent.getWidth();
    }

    final int childCount = parent.getChildCount();
    //        for (int i = 0; i < childCount; i++) {
    // Remove the last divider
    // @By_syk
    for (int i = 0; i < childCount - 1; i++) {
        final View child = parent.getChildAt(i);
        parent.getDecoratedBoundsWithMargins(child, mBounds);
        final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
        final int top = bottom - mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:com.mark.app.widget.recycler_view.LLMDivider.java

private void drawVertical(Canvas canvas, RecyclerView parent) {
    canvas.save();//from  ww  w  .j  a  v a 2s .c  o  m
    final int left;
    final int right;
    if (getParentClipToPadding(parent)) {
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
    } else {
        left = 0;
        right = parent.getWidth();
    }

    // ???
    final int childCount = mEnableLastItemDivider ? parent.getChildCount() : parent.getChildCount() - 1;
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
        final int top = bottom - mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:com.mark.app.widget.recycler_view.LLMDivider.java

private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();//from ww  w.  j a v  a  2 s  .c  o m
    final int top;
    final int bottom;
    if (getParentClipToPadding(parent)) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    // ???
    final int childCount = mEnableLastItemDivider ? parent.getChildCount() : parent.getChildCount() - 1;
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:org.mozilla.focus.widget.AnimatedProgressBar.java

@Override
public void onDraw(Canvas canvas) {
    if (mClipRegion == 0) {
        super.onDraw(canvas);
    } else {//  w w w  .  j  a v a 2  s . c  o m
        canvas.getClipBounds(tempRect);
        final float clipWidth = tempRect.width() * mClipRegion;
        final int saveCount = canvas.save();

        if (mIsRtl) {
            canvas.clipRect(tempRect.left, tempRect.top, tempRect.right - clipWidth, tempRect.bottom);
        } else {
            canvas.clipRect(tempRect.left + clipWidth, tempRect.top, tempRect.right, tempRect.bottom);
        }
        super.onDraw(canvas);
        canvas.restoreToCount(saveCount);
    }
}

From source file:com.cocosw.accessory.views.layout.CollapsingTitleLayout.java

@Override
public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    final int toolbarHeight = mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(), interpolate(canvas.getHeight(), toolbarHeight, mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (mTitleToDraw != null) {
        float x = mTextLeft;
        float y = mTextTop;

        if (!mUseTexture) {
            // If we're not drawing a texture, we need to properly offset the text
            x -= mDrawnTextBounds.left;/*w  w w . j  a  v a 2  s . c o  m*/
            y -= mDrawnTextBounds.top;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mTextLeft, mTextTop, mTextRight, mTextTop + mDrawnTextBounds.height(),
                    DEBUG_DRAW_PAINT);
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (mUseTexture && mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTitleToDraw, x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.example.nwilde.myfirstapp.SimpleListDividerDecorator.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int childCount = parent.getChildCount();
    final float yPositionThreshold = (mOverlap) ? 1.0f : (mDividerHeight + 1.0f); // [px]
    final float zPositionThreshold = 1.0f; // [px]

    if (childCount == 0) {
        return;/*from  ww  w. j av a 2  s .co  m*/
    }

    int savedCount = c.save(Canvas.CLIP_SAVE_FLAG);

    c.clipRect(parent.getLeft() + parent.getPaddingLeft(), parent.getTop() + parent.getPaddingTop(),
            parent.getRight() - parent.getPaddingRight(), parent.getBottom() + parent.getPaddingBottom());

    for (int i = 0; i < childCount - 1; i++) {
        final View child = parent.getChildAt(i);
        final View nextChild = parent.getChildAt(i + 1);

        if ((child.getVisibility() != View.VISIBLE) || (nextChild.getVisibility() != View.VISIBLE)) {
            continue;
        }

        // check if the next item is placed at the bottom
        final float childBottom = child.getBottom() + ViewCompat.getTranslationY(child);
        final float nextChildTop = nextChild.getTop() + ViewCompat.getTranslationY(nextChild);

        if (!(Math.abs(nextChildTop - childBottom) < yPositionThreshold)) {
            continue;
        }

        // check if the next item is placed on the same plane
        final float childZ = ViewCompat.getTranslationZ(child) + ViewCompat.getElevation(child);
        final float nextChildZ = ViewCompat.getTranslationZ(nextChild) + ViewCompat.getElevation(nextChild);

        if (!(Math.abs(nextChildZ - childZ) < zPositionThreshold)) {
            continue;
        }

        final float childAlpha = ViewCompat.getAlpha(child);
        final float nextChildAlpha = ViewCompat.getAlpha(nextChild);

        final int tx = (int) (ViewCompat.getTranslationX(child) + 0.5f);
        final int ty = (int) (ViewCompat.getTranslationY(child) + 0.5f);
        final int left = child.getLeft();
        final int right = child.getRight();
        final int top = child.getBottom();
        final int bottom = top + mDividerHeight;

        mDividerDrawable.setAlpha((int) ((0.5f * 255) * (childAlpha + nextChildAlpha) + 0.5f));
        mDividerDrawable.setBounds(left + tx, top + ty, right + tx, bottom + ty);
        mDividerDrawable.draw(c);
    }

    c.restoreToCount(savedCount);
}

From source file:com.android.launcher3.allapps.AllAppsRecyclerView.java

/**
 * We need to override the draw to ensure that we don't draw the overscroll effect beyond the
 * background bounds.// w  w  w.  ja v a2s . c  o  m
 */
@Override
protected void dispatchDraw(Canvas canvas) {
    // Clip to ensure that we don't draw the overscroll effect beyond the background bounds
    canvas.clipRect(mBackgroundPadding.left, mBackgroundPadding.top, getWidth() - mBackgroundPadding.right,
            getHeight() - mBackgroundPadding.bottom);
    super.dispatchDraw(canvas);
}