Example usage for android.graphics Canvas ALL_SAVE_FLAG

List of usage examples for android.graphics Canvas ALL_SAVE_FLAG

Introduction

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

Prototype

int ALL_SAVE_FLAG

To view the source code for android.graphics Canvas ALL_SAVE_FLAG.

Click Source Link

Document

Restore everything when restore() is called (standard save flags).

Usage

From source file:Main.java

public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);//from   w w w.  java  2 s .c om
    canvas.drawRect(outerRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    //      framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(innerRect, innerPaint);

    // 3. Set the Power Duff mode link:
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    outerPaint.setColor(Color.argb(255, 255, 255, 255));
    //      framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(outerRect, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    //      Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder;

    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    float cornerRadius = size / 18f;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);//w  w w.  ja va 2s.  c  o  m
    canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint);

    // 3. Set the Power Duff mode link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU
    outerPaint.setColor(Color.argb(100, 0, 0, 0));
    framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness, int color) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    //      Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder;

    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    float cornerRadius = size / 18f;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);/* w  ww  .j ava  2  s .  c  o m*/
    canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint);

    // 3. Set the Power Duff mode link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU
    outerPaint.setColor(color);
    framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:com.keylesspalace.tusky.view.ProgressImageView.java

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

    float angle = (progress / 100f) * 360 - 90;
    float halfWidth = canvas.getWidth() / 2;
    float halfHeight = canvas.getHeight() / 2;
    progressRect.set(halfWidth * 0.75f, halfHeight * 0.75f, halfWidth * 1.25f, halfHeight * 1.25f);
    biggerRect.set(progressRect);/*from   www  .j av  a 2s  . com*/
    int margin = 8;
    biggerRect.set(progressRect.left - margin, progressRect.top - margin, progressRect.right + margin,
            progressRect.bottom + margin);
    canvas.saveLayer(biggerRect, null, Canvas.ALL_SAVE_FLAG);
    if (progress != -1) {
        canvas.drawOval(progressRect, circlePaint);
        canvas.drawArc(biggerRect, angle, 360 - angle - 90, true, clearPaint);
    }
    canvas.restore();

    int circleRadius = Utils.dpToPx(getContext(), 14);
    int circleMargin = Utils.dpToPx(getContext(), 14);

    int circleY = canvas.getHeight() - circleMargin - circleRadius / 2;
    int circleX = canvas.getWidth() - circleMargin - circleRadius / 2;

    canvas.drawCircle(circleX, circleY, circleRadius, markBgPaint);

    captionDrawable.setBounds(canvas.getWidth() - circleMargin - circleRadius,
            canvas.getHeight() - circleMargin - circleRadius, canvas.getWidth() - circleMargin,
            canvas.getHeight() - circleMargin);
    DrawableCompat.setTint(captionDrawable, Color.WHITE);
    captionDrawable.draw(canvas);
}

From source file:com.popdeem.sdk.uikit.widget.PDUIBezelImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;//from  ww w .  j  ava2s.  com
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) {
        // Need to redraw the cache
        if (width == mCachedWidth && height == mCachedHeight) {
            // Have a correct-sized bitmap cache already allocated. Just erase it.
            mCacheBitmap.eraseColor(0);
        } else {
            // Allocate a new bitmap with the correct dimensions.
            mCacheBitmap.recycle();
            //noinspection AndroidLintDrawAllocation
            mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCachedWidth = width;
            mCachedHeight = height;
        }

        Canvas cacheCanvas = new Canvas(mCacheBitmap);
        if (mMaskDrawable != null) {
            int sc = cacheCanvas.save();
            mMaskDrawable.draw(cacheCanvas);
            mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.ALL_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else if (mDesaturateOnPress && isPressed()) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            mMaskedPaint.setColorFilter(mDesaturateColorFilter);
            cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.ALL_SAVE_FLAG);
            super.onDraw(cacheCanvas);
            cacheCanvas.restoreToCount(sc);
        } else {
            super.onDraw(cacheCanvas);
        }

        if (mBorderDrawable != null) {
            mBorderDrawable.draw(cacheCanvas);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);
}

From source file:com.rks.musicx.misc.widgets.DiagonalLayout.java

@Override
protected void dispatchDraw(Canvas canvas) {
    int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

    super.dispatchDraw(canvas);

    paint.setXfermode(pdMode);/*from   ww  w .  j a va2 s. c o m*/
    canvas.drawPath(clipPath, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
}

From source file:android.widget.PinnedHeaderListView.java

private void drawHeader(final Canvas canvas, final PinnedHeader header, final long currentTime) {
    if (header.animating) {
        final int timeLeft = (int) (header.targetTime - currentTime);
        if (timeLeft <= 0) {
            header.y = header.targetY;/* w  w  w  . jav a  2  s  .  co  m*/
            header.visible = header.targetVisible;
            header.animating = false;
        } else
            header.y = header.targetY + (header.sourceY - header.targetY) * timeLeft / mAnimationDuration;
    }
    if (header.visible) {
        final View view = header.view;
        final int saveCount = canvas.save();
        final int translateX = ViewUtil.isViewLayoutRtl(this)
                ? getWidth() - mHeaderPaddingStart - view.getWidth()
                : mHeaderPaddingStart;
        canvas.translate(translateX, header.y);
        if (header.state == FADING) {
            mBounds.set(0, 0, view.getWidth(), view.getHeight());
            canvas.saveLayerAlpha(mBounds, header.alpha, Canvas.ALL_SAVE_FLAG);
        }
        view.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ?? ??/* w  ww  . j av a2s  .co m*/
 * 
 * @param src
 *            the bitmap object you want proecss
 * @param watermark
 *            the water mark above the src
 * @return return a bitmap object ,if paramter's length is 0,return null
 */
public static Bitmap createWatermarkBitmap(Bitmap src, Bitmap watermark) {
    if (src == null) {
        return null;
    }

    int w = src.getWidth();
    int h = src.getHeight();
    int ww = watermark.getWidth();
    int wh = watermark.getHeight();
    // create the new blank bitmap
    Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// SRC?
    Canvas cv = new Canvas(newb);
    // draw src into
    cv.drawBitmap(src, 0, 0, null);//  00??src
    // draw watermark into
    cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// src??
    // save all clip
    cv.save(Canvas.ALL_SAVE_FLAG);// ?
    // store
    cv.restore();// 
    return newb;
}

From source file:io.doist.datetimepicker.time.RadialTimePickerView.java

@Override
public void onDraw(Canvas canvas) {
    if (!mInputEnabled) {
        canvas.saveLayerAlpha(0, 0, getWidth(), getHeight(), mDisabledAlpha, Canvas.ALL_SAVE_FLAG);
    } else {//w  ww. j a v a2 s  .com
        canvas.save();
    }

    calculateGridSizesHours();
    calculateGridSizesMinutes();

    drawCircleBackground(canvas);
    drawSelector(canvas);

    drawTextElements(canvas, mTextSize[HOURS], mTypeface, mOuterTextHours, mTextGridWidths[HOURS],
            mTextGridHeights[HOURS], mPaint[HOURS], mColor[HOURS], mAlpha[HOURS].getValue());

    if (mIs24HourMode && mInnerTextHours != null) {
        drawTextElements(canvas, mInnerTextSize, mTypeface, mInnerTextHours, mInnerTextGridWidths,
                mInnerTextGridHeights, mPaint[HOURS], mColor[HOURS], mAlpha[HOURS].getValue());
    }

    drawTextElements(canvas, mTextSize[MINUTES], mTypeface, mOuterTextMinutes, mTextGridWidths[MINUTES],
            mTextGridHeights[MINUTES], mPaint[MINUTES], mColor[MINUTES], mAlpha[MINUTES].getValue());

    drawCenter(canvas);

    if (DEBUG) {
        drawDebug(canvas);
    }

    canvas.restore();
}

From source file:com.aigo.kt03airdemo.ui.view.ResideLayout.java

@Override
protected boolean drawChild(@NonNull Canvas canvas, @NonNull View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save(Canvas.ALL_SAVE_FLAG);

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.scale(1.2f - 0.2f * mSlideOffset, 1.2f - 0.2f * mSlideOffset, child.getRight(), getHeight() / 2);
    } else {/*  w  w w  .  j av a 2s  .  c o m*/
        assert mSlideableView != null;
        canvas.scale(1 - mSlideOffset / 3, 1 - mSlideOffset / 3, mSlideableView.getLeft(), getHeight() / 2);
        ViewCompat.setRotationY(child, -10 * mSlideOffset);
    }

    if (!lp.slideable && mSlideOffset == 0) {
        result = true;
    } else {
        if (Build.VERSION.SDK_INT >= 11) { // HC
            result = super.drawChild(canvas, child, drawingTime);
        } else {
            if (lp.dimWhenOffset && mSlideOffset > 0) {
                if (!child.isDrawingCacheEnabled()) {
                    child.setDrawingCacheEnabled(true);
                }
                final Bitmap cache = child.getDrawingCache();
                if (cache != null) {
                    canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                    result = false;
                } else {
                    Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                    result = super.drawChild(canvas, child, drawingTime);
                }
            } else {
                if (child.isDrawingCacheEnabled()) {
                    child.setDrawingCacheEnabled(false);
                }
                result = super.drawChild(canvas, child, drawingTime);
            }
        }
    }

    canvas.restoreToCount(save);

    return result;
}