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:org.bangbang.support.v4.widget.VerticalViewPager.java

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

    final int overScrollMode = android.support.v4.view.ViewCompat.getOverScrollMode(this);
    if (overScrollMode == android.support.v4.view.ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == android.support.v4.view.ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS
                    && mAdapter != null && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);//ww  w.  jav a  2 s. c  o  m
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

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

    if (needsInvalidate) {
        // Keep animating
        android.support.v4.view.ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file:cn.trinea.android.common.view.CycleViewPager.java

@Override
public void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    if (mSupportCycle) {
        final int width = getWidth();
        float leftBound = width * mFirstOffset;
        float rightBound = width * mLastOffset;
        int count = mItems.size();
        int scrollx = getScrollX();
        if (scrollx < leftBound) {
            canvas.translate(-width * count, 0);
            canvas.clipRect(width * (count - 1), 0, width * count, getHeight());
            super.dispatchDraw(canvas);
        }//from ww w.j  av  a  2s .  c  o m

        if (scrollx > rightBound) {
            canvas.translate(width * count, 0);
            canvas.clipRect(0, 0, width, getHeight());
            super.dispatchDraw(canvas);
        }
    }
}

From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * Tag//from  ww w .  j  a va2 s  .  co  m
 *
 * @param canvas     
 * @param position   ???
 * @param itemWidth  ?
 * @param itemHeight ?
 */
@SuppressWarnings("unused")
protected void drawTag(Canvas canvas, int position, int itemWidth, int itemHeight) {
    if (mAdapter == null || !mAdapter.isTagEnable(position))
        return;
    String text = mAdapter.getTag(position) == null ? "" : mAdapter.getTag(position);
    mTextPaint.setTextSize(mTagTextSize);
    mTextPaint.setColor(mTagTextColor);
    mTextPaint.getTextBounds(text, 0, text.length(), mTextMeasureBounds);
    final int textWidth = mTextMeasureBounds.width();
    final int textHeight = mTextMeasureBounds.height();
    final int tagBackgroundWidth = mTagBackground == null ? 0 : mTagBackground.getIntrinsicWidth();
    final int tagBackgroundHeight = mTagBackground == null ? 0 : mTagBackground.getIntrinsicHeight();
    int tagWidth;
    int tagHeight;
    switch (mTagMinSizeMode) {
    default:
    case TAG_MIN_SIZE_MODE_HAS_TEXT:
        if ("".equals(text)) {
            tagWidth = Math.min(mTagMinWidth, tagBackgroundWidth);
            tagHeight = Math.min(mTagMinHeight, tagBackgroundHeight);
            break;
        }
    case TAG_MIN_SIZE_MODE_ALWAYS:
        tagWidth = Math.max(textWidth + mTagLocation.getPaddingLeft() + mTagLocation.getPaddingRight(),
                Math.max(mTagMinWidth, tagBackgroundWidth));
        tagHeight = Math.max(textHeight + mTagLocation.getPaddingTop() + mTagLocation.getPaddingBottom(),
                Math.max(mTagMinHeight, tagBackgroundHeight));
        break;
    }
    final int rightTabX = position == getItemCount() - 1 ? getWidth() - ViewCompat.getPaddingEnd(this)
            : ViewCompat.getPaddingStart(this) + (itemWidth + getIntervalWidth()) * position + itemWidth;
    final int rightTagX = rightTabX - mTagLocation.getMarginRight();
    final int tagY = getPaddingTop() + mTagLocation.getMarginTop();
    final int leftTagX = rightTagX - tagWidth;
    final float tagCenterX = leftTagX + tagWidth * 0.5f;
    final float tagCenterY = tagY + tagWidth * 0.5f;
    canvas.save();
    if (mTagBackground != null) {
        mTagBackground.setBounds(0, 0, tagWidth, tagHeight);
        canvas.translate(leftTagX, tagY);
        mTagBackground.draw(canvas);
        if (!"".equals(text)) {
            canvas.translate(tagWidth * 0.5f, tagHeight * 0.5f + mTagTextDesc);
            canvas.drawText(text, 0, 0, mTextPaint);
        }
    } else {
        canvas.translate(tagCenterX, tagCenterY + mTagTextDesc);
        canvas.drawText(text, 0, 0, mTextPaint);
    }
    canvas.restore();
}

From source file:com.android.internal.widget.ViewPager.java

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

    final int overScrollMode = getOverScrollMode();
    if (overScrollMode == View.OVER_SCROLL_ALWAYS || (overScrollMode == View.OVER_SCROLL_IF_CONTENT_SCROLLS
            && mAdapter != null && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();

            canvas.rotate(270);//from www.  j  av a  2s  .c  o  m
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

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

    if (needsInvalidate) {
        // Keep animating
        postInvalidateOnAnimation();
    }
}

From source file:ticwear.design.widget.CoordinatorLayout.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (mEdgeGlowTop != null) {
        if (!mEdgeGlowTop.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();

            int edgeY = Math.min(0, getScrollY());
            canvas.translate(0, edgeY);
            mEdgeGlowTop.setSize(width, getHeight());
            if (mEdgeGlowTop.draw(canvas)) {
                invalidate(0, 0, getWidth(), mEdgeGlowTop.getMaxHeight() + getPaddingTop());
            }// w  w  w.  ja v  a  2 s  .c  om
            canvas.restoreToCount(restoreCount);
        }
        if (!mEdgeGlowBottom.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight();

            int edgeX = -width;
            int edgeY = Math.max(height, getScrollY() + height);
            canvas.translate(edgeX, edgeY);
            canvas.rotate(180, width, 0);
            mEdgeGlowBottom.setSize(width, height);
            if (mEdgeGlowBottom.draw(canvas)) {
                invalidate(0, getHeight() - getPaddingBottom() - mEdgeGlowBottom.getMaxHeight(), getWidth(),
                        getHeight());
            }
            canvas.restoreToCount(restoreCount);
        }
    }
}

From source file:org.ohmage.widget.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.translate(0, (float) mFirstOffset * height);
            mTopEdge.setSize(width, height);
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }//from w w w.  j  a  v a 2  s  .c  o  m
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();

            canvas.rotate(180);
            canvas.translate(-width, (float) -(mLastOffset + 1) * height);
            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:android.support.graphics.drawable.VectorDrawableCompat.java

@Override
public void draw(Canvas canvas) {
    if (mDelegateDrawable != null) {
        mDelegateDrawable.draw(canvas);/*from   www  .  ja v  a  2  s  .  co  m*/
        return;
    }
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);
    if (mTmpBounds.width() <= 0 || mTmpBounds.height() <= 0) {
        // Nothing to draw
        return;
    }

    // Color filters always override tint filters.
    final ColorFilter colorFilter = (mColorFilter == null ? mTintFilter : mColorFilter);

    // The imageView can scale the canvas in different ways, in order to
    // avoid blurry scaling, we have to draw into a bitmap with exact pixel
    // size first. This bitmap size is determined by the bounds and the
    // canvas scale.
    canvas.getMatrix(mTmpMatrix);
    mTmpMatrix.getValues(mTmpFloats);
    float canvasScaleX = Math.abs(mTmpFloats[Matrix.MSCALE_X]);
    float canvasScaleY = Math.abs(mTmpFloats[Matrix.MSCALE_Y]);

    float canvasSkewX = Math.abs(mTmpFloats[Matrix.MSKEW_X]);
    float canvasSkewY = Math.abs(mTmpFloats[Matrix.MSKEW_Y]);

    // When there is any rotation / skew, then the scale value is not valid.
    if (canvasSkewX != 0 || canvasSkewY != 0) {
        canvasScaleX = 1.0f;
        canvasScaleY = 1.0f;
    }

    int scaledWidth = (int) (mTmpBounds.width() * canvasScaleX);
    int scaledHeight = (int) (mTmpBounds.height() * canvasScaleY);
    scaledWidth = Math.min(MAX_CACHED_BITMAP_SIZE, scaledWidth);
    scaledHeight = Math.min(MAX_CACHED_BITMAP_SIZE, scaledHeight);

    if (scaledWidth <= 0 || scaledHeight <= 0) {
        return;
    }

    final int saveCount = canvas.save();
    canvas.translate(mTmpBounds.left, mTmpBounds.top);

    // Handle RTL mirroring.
    final boolean needMirroring = needMirroring();
    if (needMirroring) {
        canvas.translate(mTmpBounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }

    // At this point, canvas has been translated to the right position.
    // And we use this bound for the destination rect for the drawBitmap, so
    // we offset to (0, 0);
    mTmpBounds.offsetTo(0, 0);

    mVectorState.createCachedBitmapIfNeeded(scaledWidth, scaledHeight);
    if (!mAllowCaching) {
        mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
    } else {
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
            mVectorState.updateCacheStates();
        }
    }
    mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter, mTmpBounds);
    canvas.restoreToCount(saveCount);
}

From source file:com.isapp.android.circularviewpager.CircularViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;
    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if ((mAdapter != null && mAdapter.getCount() == 1) || overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();
            canvas.rotate(270);/*w w w  . j  a  va2  s.  c  o m*/
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }
    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

From source file: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 height = getHeight();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();

            canvas.translate(-getPaddingLeft(), mFirstOffset * height);
            mTopEdge.setSize(width, height);
            needsInvalidate |= mTopEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }//w  w w .  j a v  a 2s  . c  o  m
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth() - getPaddingLeft() - getPaddingRight();
            final int height = getHeight();

            canvas.rotate(180);
            canvas.translate(-width + getPaddingLeft(), -(mLastOffset + 1) * height);
            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.dishes.views.stageredggridview.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 ww  .  j  ava  2 s  .  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();
        }
    }
    if (mFastScroller != null) {
        final int scrollY = this.getScrollY();
        if (scrollY != 0) {
            // Pin to the top/bottom during overscroll
            int restoreCount = canvas.save();
            canvas.translate(0, (float) scrollY);
            mFastScroller.draw(canvas);
            canvas.restoreToCount(restoreCount);
        } else {
            mFastScroller.draw(canvas);
        }
    }
    // drawSelector(canvas);
}