Example usage for android.graphics Canvas concat

List of usage examples for android.graphics Canvas concat

Introduction

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

Prototype

public void concat(@Nullable Matrix matrix) 

Source Link

Document

Preconcat the current matrix with the specified matrix.

Usage

From source file:Main.java

private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
    float intrinsicWidth = (float) pictureDrawable.getIntrinsicWidth();
    float intrinsicHeight = (float) pictureDrawable.getIntrinsicHeight();
    float f = 1.0f;
    if (intrinsicWidth < 600.0f || intrinsicHeight < 600.0f) {
        f = Math.min(600.0f / intrinsicWidth, 600.0f / intrinsicHeight);
        intrinsicWidth = (intrinsicWidth * f) + 0.5f;
        intrinsicHeight = (intrinsicHeight * f) + 0.5f;
    }//from ww  w  .  j  a  v a  2 s  . c o  m
    Bitmap createBitmap = Bitmap.createBitmap((int) intrinsicWidth, (int) intrinsicHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(createBitmap);
    Matrix matrix = new Matrix();
    matrix.preScale(f, f);
    canvas.concat(matrix);
    canvas.drawPicture(pictureDrawable.getPicture());
    return createBitmap;
}

From source file:Main.java

/**
 * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix.
 *///from   w  w w  .  j  ava 2s.co  m
private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) {
    // Re-implement Bitmap createBitmap() to always return a mutable bitmap.
    Canvas canvas = new Canvas();

    Bitmap bitmap;
    Paint paint;
    if ((m == null) || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(width, height, source.getConfig());
        paint = null;
    } else {
        RectF rect = new RectF(0, 0, width, height);
        m.mapRect(rect);
        bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig());

        canvas.translate(-rect.left, -rect.top);
        canvas.concat(m);

        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        if (!m.rectStaysRect()) {
            paint.setAntiAlias(true);
        }
    }
    bitmap.setDensity(source.getDensity());
    canvas.setBitmap(bitmap);

    Rect srcBounds = new Rect(x, y, x + width, y + height);
    RectF dstBounds = new RectF(0, 0, width, height);
    canvas.drawBitmap(source, srcBounds, dstBounds, paint);
    return bitmap;
}

From source file:Main.java

private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
        boolean clipShadow, Paint paint) {

    int scaledWidth = width;
    int scaledHeight = height;

    final Canvas canvas = new Canvas();
    canvas.translate(offset / 2.0f, offset / 2.0f);

    Bitmap bitmap;//from  w ww  . j  a  v a 2 s . c  o m

    final Rect from = new Rect(x, y, x + width, y + height);
    final RectF to = new RectF(0, 0, width, height);

    if (m == null || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        paint = null;
    } else {
        RectF mapped = new RectF();
        m.mapRect(mapped, to);

        scaledWidth = Math.round(mapped.width());
        scaledHeight = Math.round(mapped.height());

        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        canvas.translate(-mapped.left, -mapped.top);
        canvas.concat(m);
    }

    canvas.setBitmap(bitmap);
    canvas.drawRect(0.0f, 0.0f, width, height, paint);
    canvas.drawBitmap(source, from, to, SCALE_PAINT);

    return bitmap;
}

From source file:com.scvngr.levelup.core.ui.view.LevelUpCodeView.java

/**
 * Draws the QR code to the canvas, scaling it up to fit the measured size of the view. If
 * enabled, this also draws the colored target areas per
 * {@link #drawColoredTargetAreas(Canvas, LevelUpQrCodeImage)}.
 *
 * @param canvas the drawing canvas./*from ww  w  .j a v  a2s . c  o  m*/
 * @param levelUpQrCodeImage the image of the QR code with target marker information.
 */
private void drawQrCode(@NonNull final Canvas canvas, @NonNull final LevelUpQrCodeImage levelUpQrCodeImage) {
    final Bitmap codeBitmap = levelUpQrCodeImage.getBitmap();
    /*
     * The code is cached in the smallest size and must be scaled before being displayed. It is
     * necessary to draw it directly onto a canvas and scale it in the same operation for
     * efficiency (so we do not have any perceivable lag when switching tip values).
     */
    mCodeScalingMatrix.setScale((float) getMeasuredWidth() / codeBitmap.getWidth(),
            (float) getMeasuredHeight() / codeBitmap.getHeight());

    // Save the canvas without the scaling matrix.
    canvas.save();

    canvas.concat(mCodeScalingMatrix);
    canvas.drawBitmap(codeBitmap, 0, 0, mQrCodePaint);

    if (mIsColorizeSet) {
        drawColoredTargetAreas(canvas, levelUpQrCodeImage);
    }

    canvas.restore();
}

From source file:com.mylikes.likes.etchasketch.Slate.java

@Override
protected void onDraw(Canvas canvas) {
    if (mTiledCanvas != null) {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);

        if (mPanX != 0 || mPanY != 0 || !mZoomMatrix.isIdentity()) {
            canvas.translate(mPanX, mPanY);
            canvas.concat(mZoomMatrix);

            canvas.drawRect(-20000, -20000, 20000, 0, mWorkspacePaint);
            canvas.drawRect(-20000, 0, 0, mTiledCanvas.getHeight(), mWorkspacePaint);
            canvas.drawRect(mTiledCanvas.getWidth(), 0, 20000, mTiledCanvas.getHeight(), mWorkspacePaint);
            canvas.drawRect(-20000, mTiledCanvas.getHeight(), 20000, 20000, mWorkspacePaint);
        }//  www. j ava2 s . co  m

        if (!mDirtyRegion.isEmpty()) {
            canvas.clipRegion(mDirtyRegion);
            mDirtyRegion.setEmpty();
        }
        // TODO: tune this threshold based on the device density
        mBlitPaint.setFilterBitmap(getScale(mZoomMatrix) < 3f);
        mTiledCanvas.drawTo(canvas, 0, 0, mBlitPaint, false); // @@ set to true for dirty tile updates
        if (0 != (mDebugFlags & FLAG_DEBUG_STROKES)) {
            drawStrokeDebugInfo(canvas);
        }
        for (MoveableDrawing drawing : overlays) {
            drawing.renderInto(canvas, moveMode && drawing == selectedDrawing);
        }

        canvas.restore();

        if (0 != (mDebugFlags & FLAG_DEBUG_PRESSURE)) {
            mPressureCooker.drawDebug(canvas);
        }
    }
}

From source file:cl.monsoon.s1next.widget.PhotoView.java

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

    // draw the photo
    if (mDrawable != null) {
        int saveCount = canvas.getSaveCount();
        canvas.save();/* w ww  .  j av  a  2s. c  om*/

        if (mDrawMatrix != null) {
            canvas.concat(mDrawMatrix);
        }
        mDrawable.draw(canvas);

        canvas.restoreToCount(saveCount);

        if (mVideoBlob != null) {
            final Bitmap videoImage = (mVideoReady ? sVideoImage : sVideoNotReadyImage);
            final int drawLeft = (getWidth() - videoImage.getWidth()) / 2;
            final int drawTop = (getHeight() - videoImage.getHeight()) / 2;
            canvas.drawBitmap(videoImage, drawLeft, drawTop, null);
        }

        // Extract the drawable's bounds (in our own copy, to not alter the image)
        mTranslateRect.set(mDrawable.getBounds());
        if (mDrawMatrix != null) {
            mDrawMatrix.mapRect(mTranslateRect);
        }

        if (mAllowCrop) {
            int previousSaveCount = canvas.getSaveCount();
            canvas.drawRect(0, 0, getWidth(), getHeight(), sCropDimPaint);
            canvas.save();
            canvas.clipRect(mCropRect);

            if (mDrawMatrix != null) {
                canvas.concat(mDrawMatrix);
            }

            mDrawable.draw(canvas);
            canvas.restoreToCount(previousSaveCount);
            canvas.drawRect(mCropRect, sCropPaint);
        }
    }
}

From source file:martin.app.bitunion.widget.PhotoView.java

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

    // draw the photo
    if (mDrawable != null) {
        int saveCount = canvas.getSaveCount();
        canvas.save();//from  w  w  w .j a  va 2s . co m

        if (mDrawMatrix != null) {
            canvas.concat(mDrawMatrix);
        }
        mDrawable.draw(canvas);

        canvas.restoreToCount(saveCount);

        if (mVideoBlob != null) {
            final Bitmap videoImage = (mVideoReady ? sVideoImage : sVideoNotReadyImage);
            final int drawLeft = (getWidth() - videoImage.getWidth()) / 2;
            final int drawTop = (getHeight() - videoImage.getHeight()) / 2;
            canvas.drawBitmap(videoImage, drawLeft, drawTop, null);
        }

        // Extract the drawable's bounds (in our own copy, to not alter the image)
        mTranslateRect.set(mDrawable.getBounds());
        if (mDrawMatrix != null) {
            mDrawMatrix.mapRect(mTranslateRect);
        }

        if (mAllowCrop) {
            int previousSaveCount = canvas.getSaveCount();
            canvas.drawRect(0, 0, getWidth(), getHeight(), sCropDimPaint);
            canvas.save();
            canvas.clipRect(mCropRect);

            if (mDrawMatrix != null) {
                canvas.concat(mDrawMatrix);
            }

            mDrawable.draw(canvas);
            canvas.restoreToCount(previousSaveCount);
            canvas.drawRect(mCropRect, sCropPaint);
        }
    }
}

From source file:pl.motyczko.scrollheader.PagerSlidingTabStrip.java

private void drawBackground(Canvas canvas) {
    if (mViewBackground == null)
        return;/*from  w w  w .  ja  v a 2  s  .c o m*/

    if (mViewBackground instanceof KenBurnsDrawable && !((KenBurnsDrawable) mViewBackground).isAnimating()) {
        mViewBackground.setBounds(0, 0, getWidth(), getHeight());
        ((KenBurnsDrawable) mViewBackground).animate();
        mKenBurnsInitialized = true;
    }
    if (mDrawMatrix == null && !(mViewBackground instanceof KenBurnsDrawable)) {
        calculateBackgroundBounds();
    }
    int saveCount = canvas.getSaveCount();
    canvas.save();
    float translation = mParallaxForBackground ? getTranslationY() / 2 : 0;
    canvas.translate(getScrollX(), getScrollY() - translation);
    if (mDrawMatrix != null)
        canvas.concat(mDrawMatrix);

    float fraction = Math.abs(getTranslationY() / getAllowedVerticalScrollLength());
    int color = (Integer) mColorEvaluator.evaluate(fraction, mOverlayColorExpanded, mOverlayColorCollapsed);
    mViewBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    mViewBackground.draw(canvas);
    canvas.restoreToCount(saveCount);
}

From source file:im.ene.lab.design.widget.coverflow.FeatureCoverFlow.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    canvas.save();/*from  ww  w. ja  v  a  2s.  co  m*/

    //set matrix to child's transformation
    setChildTransformation(child, mMatrix);

    //Generate child bitmap
    Bitmap bitmap = child.getDrawingCache();

    //initialize canvas state. Child 0,0 coordinates will match canvas 0,0
    canvas.translate(child.getLeft(), child.getTop());

    //set child transformation on canvas
    canvas.concat(mMatrix);

    final Bitmap rfCache = ((CoverFrame) child).mReflectionCache;

    if (mReflectionBackgroundColor != Color.TRANSPARENT) {
        final int top = bitmap.getHeight() + mReflectionGap - 2;
        final float frame = 1.0f;
        mReflectionPaint.setColor(mReflectionBackgroundColor);
        canvas.drawRect(frame, top + frame, rfCache.getWidth() - frame, top + rfCache.getHeight() - frame,
                mReflectionPaint);
    }

    mPaint.reset();
    mPaint.setAntiAlias(true);
    mPaint.setFilterBitmap(true);

    //Draw child bitmap with applied transforms
    canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);

    //Draw reflection
    canvas.drawBitmap(rfCache, 0.0f, bitmap.getHeight() - 2 + mReflectionGap, mPaint);

    canvas.restore();
    return false;
}

From source file:cl.monsoon.s1next.widget.PhotoView.java

/**
 * Gets a bitmap of the cropped region. If cropping is not enabled, returns {@code null}.
 *///  w  ww .j a  v  a 2 s.  co m
public Bitmap getCroppedPhoto() {
    if (!mAllowCrop) {
        return null;
    }

    final Bitmap croppedBitmap = Bitmap.createBitmap((int) CROPPED_SIZE, (int) CROPPED_SIZE,
            Bitmap.Config.ARGB_8888);
    final Canvas croppedCanvas = new Canvas(croppedBitmap);

    // scale for the final dimensions
    final int cropWidth = mCropRect.right - mCropRect.left;
    final float scaleWidth = CROPPED_SIZE / cropWidth;
    final float scaleHeight = CROPPED_SIZE / cropWidth;

    // translate to the origin & scale
    final Matrix matrix = new Matrix(mDrawMatrix);
    matrix.postTranslate(-mCropRect.left, -mCropRect.top);
    matrix.postScale(scaleWidth, scaleHeight);

    // draw the photo
    if (mDrawable != null) {
        croppedCanvas.concat(matrix);
        mDrawable.draw(croppedCanvas);
    }

    return croppedBitmap;
}