Example usage for android.graphics Canvas scale

List of usage examples for android.graphics Canvas scale

Introduction

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

Prototype

public void scale(float sx, float sy) 

Source Link

Document

Preconcat the current matrix with the specified scale.

Usage

From source file:com.tbse.mywearapplication.WatchFaceDrawer.java

void onDraw(Context context, IWatchFaceConfig config, Canvas canvas, Rect bounds) {
    final Calendar calendar = config.getCalendar();
    final boolean isAmbient = config.isAmbient();
    final boolean isRound = config.isRound();
    final boolean useLightTheme = !isAmbient && config.isLightTheme();

    mBackgroundPaint.setColor(ContextCompat.getColor(context,
            useLightTheme ? R.color.watchface_background_light : R.color.watchface_background));

    /////////////////////////////////////////////////////////////////////
    // Draw your watch face here, using the provided canvas and bounds //
    /////////////////////////////////////////////////////////////////////

    final int width = bounds.width();
    final int height = bounds.height();

    // Find the center. Ignore the window insets so that, on round
    // watches with a "chin", the watch face is centered on the entire
    // screen, not just the usable portion.
    final float centerX = width / 2f;
    final float centerY = height / 2f;

    // Draw the background.
    if (mIsMobilePreview) {
        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mPreviewBorderPaint);
        } else {//from w ww.ja v  a2  s  .  co m
            final float radius = mPreviewSquareRadius;
            final RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, mPreviewBorderPaint);
        }

        final float translateXY = width * 0.05f;
        canvas.translate(translateXY, translateXY);
        canvas.scale(0.9f, 0.9f);

        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mBackgroundPaint);
        } else {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
        }
    } else {
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
    }

    // Draw weather icon
    if (image != null) {
        Matrix matrix = new Matrix();
        matrix.setTranslate(50, 90);
        matrix.preScale(0.2f, 0.2f);
        canvas.drawBitmap(image, matrix, null);
    }

    final float secRot = calendar.get(Calendar.SECOND) / 30f * (float) Math.PI;
    final int minutes = calendar.get(Calendar.MINUTE);
    final float minRot = minutes / 30f * (float) Math.PI;
    final float hrRot = ((calendar.get(Calendar.HOUR) + (minutes / 60f)) / 6f) * (float) Math.PI;

    final float secLength = centerX - mSecondOuterOffset;
    final float minLength = centerX - mMinuteOuterOffset;
    final float hrLength = centerX - mHourOuterOffset;

    if (!isAmbient) {
        final float secX = (float) Math.sin(secRot) * secLength;
        final float secY = (float) -Math.cos(secRot) * secLength;
        canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondHandPaint);
    }

    final float minX = (float) Math.sin(minRot) * minLength;
    final float minY = (float) -Math.cos(minRot) * minLength;
    canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinuteHandPaint);

    final float hrX = (float) Math.sin(hrRot) * hrLength;
    final float hrY = (float) -Math.cos(hrRot) * hrLength;
    canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourHandPaint);

    // Draw weather text
    canvas.drawText(day, 50, 50, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(low, 50, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(high, 80, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(weatherDescription, 50, 80, isAmbient ? ambientTextPaint : textPaint);

}

From source file:com.am.pagergradienttab.view.PagerGradientTabStrip.java

/**
 * //from ww w  .j a  v  a  2  s .c om
 * 
 * @param canvas
 */
private void drawText(Canvas canvas) {
    float x = getPaddingLeft() + itemWidth / 2;
    int y = getHeight() - getPaddingBottom() - indicatorHeight - underLineHeight;
    int position = 0;
    for (String text : tabs) {
        canvas.save();
        mTextPaint.setColor(textColorNormal);
        mTextPaint.setTextAlign(Align.LEFT);
        mTextPaint.setTextSize(textSize);
        if (showTextGradient) {
            if (position == nextPager) {
                mTextPaint.setColor(getColor(textColorNormal, textColorSelected, textSizeOffset));
            } else if (position == currectPager) {
                mTextPaint.setColor(getColor(textColorNormal, textColorSelected, 1 - textSizeOffset));
            } else {
                mTextPaint.setColor(textColorNormal);
            }
        }
        if (showTextScale) {
            if (position == nextPager) {
                canvas.translate(x - mTextPaint.measureText(text) * (1 + textSizeOffset * magnification) / 2,
                        y - textTop);
                canvas.scale(1 + textSizeOffset * magnification, 1 + textSizeOffset * magnification);
            } else if (position == currectPager) {
                canvas.translate(
                        x - mTextPaint.measureText(text) * (1 + (1 - textSizeOffset) * magnification) / 2,
                        y - textTop);
                canvas.scale(1 + (1 - textSizeOffset) * magnification,
                        1 + (1 - textSizeOffset) * magnification);
            } else {
                canvas.translate(x - mTextPaint.measureText(text) / 2, y - textTop);
                canvas.scale(1, 1);
            }
        } else {
            canvas.translate(x - mTextPaint.measureText(text) / 2, y - textTop);
        }
        canvas.drawText(text, 0, 0, mTextPaint);
        x += itemWidth + intervalWidth;
        position++;
        canvas.restore();
    }
}

From source file:com.android.launcher2.AsyncTaskCallback.java

private void renderDrawableToBitmap(Drawable d, Bitmap bitmap, int x, int y, int w, int h, float scale) {
    if (bitmap != null) {
        Canvas c = new Canvas(bitmap);
        c.scale(scale, scale);
        Rect oldBounds = d.copyBounds();
        d.setBounds(x, y, x + w, y + h);
        d.draw(c);/*w w w.j  av a 2 s.c o  m*/
        d.setBounds(oldBounds); // Restore the bounds
        c.setBitmap(null);
    }
}

From source file:com.grottworkshop.gwsvectorsandboxlib.VectorDrawable.java

@Override
public void draw(Canvas canvas) {
    final Rect bounds = getBounds();
    if (bounds.width() == 0 || bounds.height() == 0) {
        // too small to draw
        return;/*w ww .java  2s .  c om*/
    }

    final int saveCount = canvas.save();
    final boolean needMirroring = needMirroring();

    canvas.translate(bounds.left, bounds.top);
    if (needMirroring) {
        canvas.translate(bounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }

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

    if (!mAllowCaching) {
        // AnimatedVectorDrawable
        if (!mVectorState.hasTranslucentRoot()) {
            mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height(), colorFilter);
        } else {
            mVectorState.createCachedBitmapIfNeeded(bounds);
            mVectorState.updateCachedBitmap(bounds);
            mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
        }
    } else {
        // Static Vector Drawable case.
        mVectorState.createCachedBitmapIfNeeded(bounds);
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(bounds);
            mVectorState.updateCacheStates();
        }
        mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
    }

    canvas.restoreToCount(saveCount);
}

From source file:org.akop.crosswords.view.CrosswordView.java

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

    if (mPuzzleBitmap != null) {
        canvas.save();/* www .j av a 2  s .  c o m*/
        canvas.clipRect(mContentRect);
        canvas.translate(mBitmapOffset.x, mBitmapOffset.y);
        canvas.scale(mBitmapScale, mBitmapScale);
        canvas.drawBitmap(mPuzzleBitmap, 0, 0, mBitmapPaint);

        canvas.restore();
    }
}

From source file:org.akop.crosswords.view.CrosswordView.java

private void renderSelection(Canvas canvas, boolean clearSelection) {
    if (mSelectedWord == null) {
        return;/* ww  w.  j  a  v  a  2 s  .  c om*/
    }

    int startRow = mSelectedWord.getStartRow();
    int endRow = startRow;
    int startColumn = mSelectedWord.getStartColumn();
    int endColumn = startColumn;
    RectF cellRect = new RectF();

    canvas.save();
    canvas.scale(mRenderScale, mRenderScale);

    if (mSelectedWord.getDirection() == Crossword.Word.DIR_ACROSS) {
        endColumn += mSelectedWord.getLength() - 1;
    } else {
        endRow += mSelectedWord.getLength() - 1;
    }

    float top = mSelectedWord.getStartRow() * mCellSize;
    for (int row = startRow, index = 0; row <= endRow; row++, top += mCellSize) {
        float left = mSelectedWord.getStartColumn() * mCellSize;
        for (int column = startColumn; column <= endColumn; column++, left += mCellSize) {
            Cell cell = mPuzzleCells[row][column];
            if (cell != null) {
                // Draw the unselected cell
                Paint paint;
                if (clearSelection) {
                    paint = mCellFillPaint;
                } else {
                    if (index == mSelectedCell) {
                        paint = mSelectedCellFillPaint;
                    } else {
                        paint = mSelectedWordFillPaint;
                    }
                }

                cellRect.set(left, top, left + mCellSize, top + mCellSize);
                renderCell(canvas, cell, cellRect, paint);
            }

            index++;
        }
    }

    canvas.restore();
}

From source file:cnedu.ustcjd.widget.MultiSlider.java

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // --> draw track
    if (mTrack != null) {
        // Translate canvas so a indeterminate circular progress bar with padding
        // rotates properly in its animation
        canvas.save();//from   w  w  w  . j av a  2  s.com
        if (isLayoutRtl() && mMirrorForRtl) {
            canvas.translate(getWidth() - getPaddingRight(), getPaddingTop());
            canvas.scale(-1.0f, 1.0f);
        } else {
            canvas.translate(getPaddingLeft(), getPaddingTop());
        }
        mTrack.draw(canvas);
        canvas.restore();
    }

    // --> draw ranges

    for (Thumb thumb : mThumbs) {
        if (thumb.getRange() != null) {
            canvas.save();
            if (isLayoutRtl() && mMirrorForRtl) {
                canvas.translate(getWidth() - getPaddingRight(), getPaddingTop());
                canvas.scale(-1.0f, 1.0f);
            } else {
                canvas.translate(getPaddingLeft(), getPaddingTop());
            }
            thumb.getRange().draw(canvas);

            canvas.restore();
        }
    }

    // --> then draw thumbs
    for (Thumb thumb : mThumbs) {
        if (thumb.getThumb() != null && !thumb.isInvisibleThumb()) {
            canvas.save();
            // Translate the padding. For the x, we need to allow the thumb to
            // draw in its extra space
            canvas.translate(getPaddingLeft() - thumb.getThumbOffset(), getPaddingTop());
            // float scale = mScaleMax > 0 ? (float) thumb.getValue() / (float) mScaleMax : 0;
            thumb.getThumb().draw(canvas);

            canvas.restore();
        }
    }
}

From source file:org.akop.ararat.view.CrosswordView.java

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

    constrainTranslation();/* ww w . j  av  a  2  s  .  c  o m*/

    canvas.save();
    canvas.clipRect(mContentRect);
    canvas.translate(mBitmapOffset.x, mBitmapOffset.y);
    canvas.scale(mBitmapScale, mBitmapScale);

    if (mPuzzleBitmap != null) {
        canvas.drawBitmap(mPuzzleBitmap, 0, 0, mBitmapPaint);
    } else {
        // Perform a fast, barebones render so that the screen doesn't
        // look completely empty

        mInPlaceRenderer.renderPuzzle(canvas, mRenderScale, true);
    }

    canvas.restore();
}

From source file:com.bettervectordrawable.lib.graphics.drawable.VectorDrawable.java

@Override
public void draw(Canvas canvas) {
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);//from   www.  j av  a  2s  .c  o m
    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]);
    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.hippo.vector.VectorDrawable.java

@Override
@SuppressWarnings("deprecation")
public void draw(Canvas canvas) {
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);//from   ww w  .j  av a 2s .c  om
    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]);
    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);
}