Java Code Examples for android.graphics.Canvas#getMatrix()

The following examples show how to use android.graphics.Canvas#getMatrix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TicketFragment.java    From Android with MIT License 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    // Log.v(DEBUG_TAG, "onDraw");
    canvas.drawBitmap(droid, translate, null);
    Matrix m = canvas.getMatrix();
    //Log.d(DEBUG_TAG, "Matrix: " + translate.toShortString());
    //Log.d(DEBUG_TAG, "Canvas: " + m.toShortString());
}
 
Example 2
Source File: SelectableRoundedImageView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private void configureBounds(Canvas canvas) {
    // I have discovered a truly marvelous explanation of this,
    // which this comment space is too narrow to contain. :)
    // If you want to understand what's going on here,
    // See http://www.joooooooooonhokim.com/?p=289
    Rect clipBounds = canvas.getClipBounds();
    Matrix canvasMatrix = canvas.getMatrix();

    if (ScaleType.CENTER == mScaleType) {
        mBounds.set(clipBounds);
    } else if (ScaleType.CENTER_CROP == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_XY == mScaleType) {
        Matrix m = new Matrix();
        m.setRectToRect(mBitmapRect, new RectF(clipBounds), Matrix.ScaleToFit.FILL);
        mBitmapShader.setLocalMatrix(m);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
               || ScaleType.FIT_CENTER == mScaleType || ScaleType.CENTER_INSIDE == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    } else if (ScaleType.MATRIX == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    }
}
 
Example 3
Source File: SelectableRoundedImageView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private void adjustCanvasForBorder(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactorX = values[0];
    final float scaleFactorY = values[4];
    final float translateX = values[2];
    final float translateY = values[5];

    final float newScaleX = mBounds.width()
                            / (mBounds.width() + mBorderWidth + mBorderWidth);
    final float newScaleY = mBounds.height()
                            / (mBounds.height() + mBorderWidth + mBorderWidth);

    canvas.scale(newScaleX, newScaleY);
    if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_XY == mScaleType || ScaleType.FIT_CENTER == mScaleType
            || ScaleType.CENTER_INSIDE == mScaleType || ScaleType.MATRIX == mScaleType) {
        canvas.translate(mBorderWidth, mBorderWidth);
    } else if (ScaleType.CENTER == mScaleType || ScaleType.CENTER_CROP == mScaleType) {
        // First, make translate values to 0
        canvas.translate(
            -translateX / (newScaleX * scaleFactorX),
            -translateY / (newScaleY * scaleFactorY));
        // Then, set the final translate values.
        canvas.translate(-(mBounds.left - mBorderWidth), -(mBounds.top - mBorderWidth));
    }
}
 
Example 4
Source File: SelectableRoundedImageView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
private void adjustBorderWidthAndBorderBounds(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactor = values[0];

    float viewWidth = mBounds.width() * scaleFactor;
    mBorderWidth = (mBorderWidth * mBounds.width()) / (viewWidth - (2 * mBorderWidth));
    mBorderPaint.setStrokeWidth(mBorderWidth);

    mBorderBounds.set(mBounds);
    mBorderBounds.inset(- mBorderWidth / 2, - mBorderWidth / 2);
}
 
Example 5
Source File: SelectableRoundedImageView.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
private void configureBounds(Canvas canvas) {
    // I have discovered a truly marvelous explanation of this,
    // which this comment space is too narrow to contain. :)
    // If you want to understand what's going on here,
    // See http://www.joooooooooonhokim.com/?p=289
    Rect clipBounds = canvas.getClipBounds();
    Matrix canvasMatrix = canvas.getMatrix();

    if (ScaleType.CENTER == mScaleType) {
        mBounds.set(clipBounds);
    } else if (ScaleType.CENTER_CROP == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_XY == mScaleType) {
        Matrix m = new Matrix();
        m.setRectToRect(mBitmapRect, new RectF(clipBounds), Matrix.ScaleToFit.FILL);
        mBitmapShader.setLocalMatrix(m);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_CENTER == mScaleType || ScaleType.CENTER_INSIDE == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    } else if (ScaleType.MATRIX == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    }
}
 
Example 6
Source File: SelectableRoundedImageView.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
private void adjustCanvasForBorder(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactorX = values[0];
    final float scaleFactorY = values[4];
    final float translateX = values[2];
    final float translateY = values[5];

    final float newScaleX = mBounds.width()
            / (mBounds.width() + mBorderWidth + mBorderWidth);
    final float newScaleY = mBounds.height()
            / (mBounds.height() + mBorderWidth + mBorderWidth);

    canvas.scale(newScaleX, newScaleY);
    if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_XY == mScaleType || ScaleType.FIT_CENTER == mScaleType
            || ScaleType.CENTER_INSIDE == mScaleType || ScaleType.MATRIX == mScaleType) {
        canvas.translate(mBorderWidth, mBorderWidth);
    } else if (ScaleType.CENTER == mScaleType || ScaleType.CENTER_CROP == mScaleType) {
        // First, make translate values to 0
        canvas.translate(
                -translateX / (newScaleX * scaleFactorX),
                -translateY / (newScaleY * scaleFactorY));
        // Then, set the final translate values.
        canvas.translate(-(mBounds.left - mBorderWidth), -(mBounds.top - mBorderWidth));
    }
}
 
Example 7
Source File: SelectableRoundedImageView.java    From Expert-Android-Programming with MIT License 5 votes vote down vote up
private void adjustBorderWidthAndBorderBounds(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactor = values[0];

    float viewWidth = mBounds.width() * scaleFactor;
    mBorderWidth = (mBorderWidth * mBounds.width()) / (viewWidth - (2 * mBorderWidth));
    mBorderPaint.setStrokeWidth(mBorderWidth);

    mBorderBounds.set(mBounds);
    mBorderBounds.inset(-mBorderWidth / 2, -mBorderWidth / 2);
}
 
Example 8
Source File: SelectableRoundedImageView.java    From sealtalk-android with MIT License 5 votes vote down vote up
private void configureBounds(Canvas canvas) {
    // I have discovered a truly marvelous explanation of this,
    // which this comment space is too narrow to contain. :)
    // If you want to understand what's going on here,
    // See http://www.joooooooooonhokim.com/?p=289
    Rect clipBounds = canvas.getClipBounds();
    Matrix canvasMatrix = canvas.getMatrix();

    if (ScaleType.CENTER == mScaleType) {
        mBounds.set(clipBounds);
    } else if (ScaleType.CENTER_CROP == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_XY == mScaleType) {
        Matrix m = new Matrix();
        m.setRectToRect(mBitmapRect, new RectF(clipBounds), Matrix.ScaleToFit.FILL);
        mBitmapShader.setLocalMatrix(m);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_CENTER == mScaleType || ScaleType.CENTER_INSIDE == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    } else if (ScaleType.MATRIX == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    }
}
 
Example 9
Source File: SelectableRoundedImageView.java    From sealtalk-android with MIT License 5 votes vote down vote up
private void adjustCanvasForBorder(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactorX = values[0];
    final float scaleFactorY = values[4];
    final float translateX = values[2];
    final float translateY = values[5];

    final float newScaleX = mBounds.width()
            / (mBounds.width() + mBorderWidth + mBorderWidth);
    final float newScaleY = mBounds.height()
            / (mBounds.height() + mBorderWidth + mBorderWidth);

    canvas.scale(newScaleX, newScaleY);
    if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_XY == mScaleType || ScaleType.FIT_CENTER == mScaleType
            || ScaleType.CENTER_INSIDE == mScaleType || ScaleType.MATRIX == mScaleType) {
        canvas.translate(mBorderWidth, mBorderWidth);
    } else if (ScaleType.CENTER == mScaleType || ScaleType.CENTER_CROP == mScaleType) {
        // First, make translate values to 0
        canvas.translate(
                -translateX / (newScaleX * scaleFactorX), 
                -translateY / (newScaleY * scaleFactorY));
        // Then, set the final translate values.
        canvas.translate(-(mBounds.left - mBorderWidth), -(mBounds.top - mBorderWidth));
    } 
}
 
Example 10
Source File: SelectableRoundedImageView.java    From sealtalk-android with MIT License 5 votes vote down vote up
private void adjustBorderWidthAndBorderBounds(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactor = values[0];

    float viewWidth = mBounds.width() * scaleFactor;
    mBorderWidth = (mBorderWidth * mBounds.width()) / (viewWidth - (2 * mBorderWidth));
    mBorderPaint.setStrokeWidth(mBorderWidth);

    mBorderBounds.set(mBounds);
    mBorderBounds.inset(- mBorderWidth / 2, - mBorderWidth / 2);
}
 
Example 11
Source File: RoundedImageView.java    From AndroidMaterialDesign with Apache License 2.0 5 votes vote down vote up
private void configureBounds(Canvas canvas) {
    // I have discovered a truly marvelous explanation of this,
    // which this comment space is too narrow to contain. :)
    // If you want to understand what's going on here,
    // See http://www.joooooooooonhokim.com/?p=289
    Rect clipBounds = canvas.getClipBounds();
    Matrix canvasMatrix = canvas.getMatrix();

    if (ScaleType.CENTER == mScaleType) {
        mBounds.set(clipBounds);
    } else if (ScaleType.CENTER_CROP == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_XY == mScaleType) {
        Matrix m = new Matrix();
        m.setRectToRect(mBitmapRect, new RectF(clipBounds), Matrix.ScaleToFit.FILL);
        mBitmapShader.setLocalMatrix(m);
        mBounds.set(clipBounds);
    } else if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_CENTER == mScaleType || ScaleType.CENTER_INSIDE == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    } else if (ScaleType.MATRIX == mScaleType) {
        applyScaleToRadii(canvasMatrix);
        mBounds.set(mBitmapRect);
    }
}
 
Example 12
Source File: RoundedImageView.java    From AndroidMaterialDesign with Apache License 2.0 5 votes vote down vote up
private void adjustCanvasForBorder(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactorX = values[0];
    final float scaleFactorY = values[4];
    final float translateX = values[2];
    final float translateY = values[5];

    final float newScaleX = mBounds.width()
            / (mBounds.width() + mBorderWidth + mBorderWidth);
    final float newScaleY = mBounds.height()
            / (mBounds.height() + mBorderWidth + mBorderWidth);

    canvas.scale(newScaleX, newScaleY);
    if (ScaleType.FIT_START == mScaleType || ScaleType.FIT_END == mScaleType
            || ScaleType.FIT_XY == mScaleType || ScaleType.FIT_CENTER == mScaleType
            || ScaleType.CENTER_INSIDE == mScaleType || ScaleType.MATRIX == mScaleType) {
        canvas.translate(mBorderWidth, mBorderWidth);
    } else if (ScaleType.CENTER == mScaleType || ScaleType.CENTER_CROP == mScaleType) {
        // First, make translate values to 0
        canvas.translate(
                -translateX / (newScaleX * scaleFactorX),
                -translateY / (newScaleY * scaleFactorY));
        // Then, set the final translate values.
        canvas.translate(-(mBounds.left - mBorderWidth), -(mBounds.top - mBorderWidth));
    }
}
 
Example 13
Source File: RoundedImageView.java    From AndroidMaterialDesign with Apache License 2.0 5 votes vote down vote up
private void adjustBorderWidthAndBorderBounds(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();
    final float[] values = new float[9];
    canvasMatrix.getValues(values);

    final float scaleFactor = values[0];

    float viewWidth = mBounds.width() * scaleFactor;
    mBorderWidth = (mBorderWidth * mBounds.width()) / (viewWidth - (2 * mBorderWidth));
    mBorderPaint.setStrokeWidth(mBorderWidth);

    mBorderBounds.set(mBounds);
    mBorderBounds.inset(-mBorderWidth / 2, -mBorderWidth / 2);
}
 
Example 14
Source File: SafeTranslatedCanvas.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void setCanvas(Canvas canvas) {
    mCanvas = canvas;
    canvas.getMatrix(mMatrix);
}
 
Example 15
Source File: VectorDrawableCompat.java    From VectorChildFinder with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    if (mDelegateDrawable != null) {
        mDelegateDrawable.draw(canvas);
        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);
}
 
Example 16
Source File: CreditsRollView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    mPaddingLeft = getPaddingLeft();
    mPaddingTop = getPaddingTop();
    mPaddingRight = getPaddingRight();
    mPaddingBottom = getPaddingBottom();

    final CharSequence text = getText();
    if (TextUtils.isEmpty(text)) {
        return;
    }

    int contentWidth = getWidth() - mPaddingLeft - mPaddingRight;
    int contentHeight = getHeight() - mPaddingTop - mPaddingBottom;

    final int saveCnt = canvas.save();

    // Rotate/translate the camera
    canvas.getMatrix(mMatrix);
    mCamera.save();

    int cX = contentWidth / 2 + mPaddingLeft;
    int cY = contentHeight / 2 + mPaddingTop;
    mCamera.rotateX(mAngle);
    mCamera.translate(0, 0, mDistanceFromText);
    mCamera.getMatrix(mMatrix);
    mMatrix.preTranslate(-cX, -cY);
    mMatrix.postTranslate(cX, cY);
    mCamera.restore();

    canvas.concat(mMatrix);

    // The end scroll multiplier ensures that the text scrolls completely out of view
    canvas.translate(0f, contentHeight - mScrollPosition *
                                         (mTextLayout.getHeight() + mEndScrollMult * contentHeight));

    // Draw the text
    mTextLayout.draw(canvas);

    canvas.restoreToCount(saveCnt);
}
 
Example 17
Source File: SVGDrawable.java    From SVG-Android with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
    // We will offset the bounds for draw, 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);

    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);

    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);

    // Use the renderer to draw.
    mState.mRenderer.draw(canvas, scaledWidth, scaledHeight, colorFilter, mTmpBounds);

    canvas.restoreToCount(saveCount);
}
 
Example 18
Source File: RoundedImageView.java    From ChatKit with Apache License 2.0 4 votes vote down vote up
private void configureBounds(Canvas canvas) {
    Matrix canvasMatrix = canvas.getMatrix();

    applyScaleToRadii(canvasMatrix);
    mBounds.set(mBitmapRect);
}