Example usage for android.graphics Canvas translate

List of usage examples for android.graphics Canvas translate

Introduction

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

Prototype

public void translate(float dx, float dy) 

Source Link

Document

Preconcat the current matrix with the specified translation

Usage

From source file:com.example.view.VerticalViewPager.java

@Override
public void draw(Canvas canvas) {
    // XXX//from w  w  w  . ja  v a2  s  .  c  o m
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.rotate(270);
            canvas.translate(-width + getPaddingLeft(), 0);
            mTopEdge.setSize(width, getHeight());
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(180);
            canvas.translate(-width + getPaddingLeft(), -itemCount * (height + mPageMargin) + mPageMargin);
            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    } /* end of if */

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    } /* end of if */
}

From source file:interactive.view.pagereader.VerticalViewPager.java

@Override
public void draw(Canvas canvas) {
    // XXX ?/*from  w  w  w .  j av a  2s .co  m*/
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.rotate(270);
            canvas.translate(-width + getPaddingLeft(), 0);
            mTopEdge.setSize(width, getHeight());
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(180);
            canvas.translate(-width + getPaddingLeft(), -itemCount * (height + mPageMargin) + mPageMargin);
            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } /* end of if */
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    } /* end of if */

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    } /* end of if */
}

From source file:android.support.v7.graphics.drawable.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();//from w w w .j a v  a2  s. c  o m

    final boolean flipToPointRight;
    switch (mDirection) {
    case ARROW_DIRECTION_LEFT:
        flipToPointRight = false;
        break;
    case ARROW_DIRECTION_RIGHT:
        flipToPointRight = true;
        break;
    case ARROW_DIRECTION_END:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR;
        break;
    case ARROW_DIRECTION_START:
    default:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
        break;
    }

    // Interpolated widths of arrow bars

    float arrowHeadBarLength = (float) Math.sqrt(mArrowHeadLength * mArrowHeadLength * 2);
    arrowHeadBarLength = lerp(mBarLength, arrowHeadBarLength, mProgress);
    final float arrowShaftLength = lerp(mBarLength, mArrowShaftLength, mProgress);
    // Interpolated size of middle bar
    final float arrowShaftCut = Math.round(lerp(0, mMaxCutForBarSize, mProgress));
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(flipToPointRight ? 0 : -180, flipToPointRight ? 180 : 0, mProgress);

    final float arrowWidth = Math.round(arrowHeadBarLength * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowHeadBarLength * Math.sin(rotation));

    mPath.rewind();
    final float topBottomBarOffset = lerp(mBarGap + mPaint.getStrokeWidth(), -mMaxCutForBarSize, mProgress);

    final float arrowEdge = -arrowShaftLength / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + arrowShaftCut, 0);
    mPath.rLineTo(arrowShaftLength - arrowShaftCut * 2, 0);

    // bottom bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // top bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);

    mPath.close();

    canvas.save();

    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    final float barThickness = mPaint.getStrokeWidth();
    final int remainingSpace = (int) (bounds.height() - barThickness * 3 - mBarGap * 2);
    float yOffset = (remainingSpace / 4) * 2; // making sure it is a multiple of 2.
    yOffset += barThickness * 1.5 + mBarGap;

    canvas.translate(bounds.centerX(), yOffset);
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ flipToPointRight) ? -1 : 1));
    } else if (flipToPointRight) {
        canvas.rotate(180);
    }
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

From source file:com.commonsware.cwac.crossport.v7.graphics.drawable.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();//from   w  w w. j ava  2s  .  co  m

    final boolean flipToPointRight;
    switch (mDirection) {
    case ARROW_DIRECTION_LEFT:
        flipToPointRight = false;
        break;
    case ARROW_DIRECTION_RIGHT:
        flipToPointRight = true;
        break;
    case ARROW_DIRECTION_END:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR;
        break;
    case ARROW_DIRECTION_START:
    default:
        flipToPointRight = DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
        break;
    }

    // Interpolated widths of arrow bars

    float arrowHeadBarLength = (float) Math.sqrt(mArrowHeadLength * mArrowHeadLength * 2);
    arrowHeadBarLength = lerp(mBarLength, arrowHeadBarLength, mProgress);
    final float arrowShaftLength = lerp(mBarLength, mArrowShaftLength, mProgress);
    // Interpolated size of middle bar
    final float arrowShaftCut = Math.round(lerp(0, mMaxCutForBarSize, mProgress));
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(flipToPointRight ? 0 : -180, flipToPointRight ? 180 : 0, mProgress);

    final float arrowWidth = Math.round(arrowHeadBarLength * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowHeadBarLength * Math.sin(rotation));

    mPath.rewind();
    final float topBottomBarOffset = lerp(mBarGap + mPaint.getStrokeWidth(), -mMaxCutForBarSize, mProgress);

    final float arrowEdge = -arrowShaftLength / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + arrowShaftCut, 0);
    mPath.rLineTo(arrowShaftLength - arrowShaftCut * 2, 0);

    // bottom bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // top bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);

    mPath.close();

    canvas.save();

    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    final float barThickness = mPaint.getStrokeWidth();
    final int remainingSpace = (int) (bounds.height() - barThickness * 3 - mBarGap * 2);
    float yOffset = (remainingSpace / 4) * 2; // making sure it is a multiple of 2.
    yOffset += barThickness * 1.5f + mBarGap;

    canvas.translate(bounds.centerX(), yOffset);
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ flipToPointRight) ? -1 : 1));
    } else if (flipToPointRight) {
        canvas.rotate(180);
    }
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

From source file:chan.android.app.bitwise.util.StaggeredGridView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (mTopEdge != null) {
        boolean needsInvalidate = false;
        if (!mTopEdge.isFinished()) {
            mTopEdge.draw(canvas);/*from w w w.j a  va  2s  . c  o  m*/
            needsInvalidate = true;
        }
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            canvas.translate(-width, getHeight());
            canvas.rotate(180, width, 0);
            mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
            needsInvalidate = true;
        }

        if (needsInvalidate) {
            invalidate();
        }
    }

    //        drawSelector(canvas);
}

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

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

    for (int i = 0; i < mFolderBackgrounds.size(); i++) {
        FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
        if (bg.isClipping) {
            cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
            canvas.save();/*from w  w w.  jav  a  2 s . c om*/
            canvas.translate(mTempLocation[0], mTempLocation[1]);
            bg.drawBackgroundStroke(canvas, mFolderBgPaint);
            canvas.restore();
        }
    }
}

From source file:com.brian.common.view.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    if (mIsCirculate) {
        if (mProgress >= .995) {
            flipped = true;// w w w  .  jav  a  2 s .co m
        } else if (mProgress <= .005) {
            flipped = false;
        }
        if (flipped) {
            canvas.save();
            canvas.scale(1f, -1f, getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
        }
    }

    Rect bounds = getBounds();

    final boolean flipToPointRight;
    switch (mDirection) {
    case ARROW_DIRECTION_LEFT:
        flipToPointRight = false;
        break;
    case ARROW_DIRECTION_RIGHT:
        flipToPointRight = true;
        break;
    default:
        flipToPointRight = false;
        break;
    }

    // Interpolated widths of arrow bars

    float arrowHeadBarLength = (float) Math.sqrt(mArrowHeadLength * mArrowHeadLength * 2);
    arrowHeadBarLength = lerp(mBarLength, arrowHeadBarLength, mProgress);
    final float arrowShaftLength = lerp(mBarLength, mArrowShaftLength, mProgress);
    // Interpolated size of middle bar
    final float arrowShaftCut = Math.round(lerp(0, mMaxCutForBarSize, mProgress));
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(flipToPointRight ? 0 : -180, flipToPointRight ? 180 : 0, mProgress);

    final float arrowWidth = Math.round(arrowHeadBarLength * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowHeadBarLength * Math.sin(rotation));

    mPath.rewind();
    final float topBottomBarOffset = lerp(mBarGap + mPaint.getStrokeWidth(), -mMaxCutForBarSize, mProgress);

    final float arrowEdge = -arrowShaftLength / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + arrowShaftCut, 0);
    mPath.rLineTo(arrowShaftLength - arrowShaftCut * 2, 0);

    // bottom bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // top bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);

    mPath.close();

    canvas.save();

    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    final float barThickness = mPaint.getStrokeWidth();
    final int remainingSpace = (int) (bounds.height() - barThickness * 3 - mBarGap * 2);
    float yOffset = (remainingSpace / 4) * 2; // making sure it is a multiple of 2.
    yOffset += barThickness * 1.5 + mBarGap;

    canvas.translate(bounds.centerX(), yOffset);
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ flipToPointRight) ? -1 : 1));
    } else if (flipToPointRight) {
        canvas.rotate(180);
    }
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

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

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/* w  w w .  ja v  a  2s  . c  o  m*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.gome.ecmall.custom.VerticalViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mTopEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();
            canvas.rotate(270);/*from   w w  w  .j  a  v  a  2s  .  c  o  m*/
            canvas.translate(mFirstOffset * height, -width + getPaddingLeft());

            mTopEdge.setSize(width, height);
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();

            canvas.rotate(90);
            canvas.translate(-(mLastOffset + 1) * height, -getPaddingLeft());
            mBottomEdge.setSize(width, height);
            needsInvalidate |= mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mTopEdge.finish();
        mBottomEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:com.mixiaoxiao.support.widget.SmoothSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!isEnabled()) {//wangbin added this line
        if (mThumbDrawable != null) {
            mThumbDrawable.setColor(getDisableColor(isChecked() ? mThumbColorOn : mThumbColorOff));
        }//from   w  w w . j  a  v  a 2s.  co m
        if (mTrackDrawable != null) {
            mTrackDrawable.setColor(getDisableColor(isChecked() ? mTrackColorOn : mTrackColorOff));
        }
    }
    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        trackDrawable.draw(canvas);
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}