Example usage for android.graphics Canvas save

List of usage examples for android.graphics Canvas save

Introduction

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

Prototype

public int save(@Saveflags int saveFlags) 

Source Link

Document

Based on saveFlags, can save the current matrix and clip onto a private stack.

Usage

From source file:vc908.stickerfactory.ui.advancedrecyclerview.decoration.ItemShadowDecorator.java

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int childCount = parent.getChildCount();

    if (childCount == 0) {
        return;//ww w .  j a  v a  2  s .  c o  m
    }

    int savedCount = c.save(Canvas.CLIP_SAVE_FLAG);

    c.clipRect(parent.getLeft() + Math.max(0, parent.getPaddingLeft() - mShadowPadding.left),
            parent.getTop()/* + Math.max(0, parent.getPaddingTop() - mShadowPadding.top)*/,
            parent.getRight() - Math.max(0, parent.getPaddingRight() - mShadowPadding.right),
            parent.getBottom()/* - Math.max(0, parent.getPaddingBottom() - mShadowPadding.bottom)*/);

    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);

        if (!shouldDrawDropShadow(child)) {
            continue;
        }

        final int tx = (int) (ViewCompat.getTranslationX(child) + 0.5f);
        final int ty = (int) (ViewCompat.getTranslationY(child) + 0.5f);

        final int left = child.getLeft() - mShadowPadding.left;
        final int right = child.getRight() + mShadowPadding.right;
        final int top = child.getTop() - mShadowPadding.top;
        final int bottom = child.getBottom() + mShadowPadding.bottom;

        mShadowDrawable.setBounds(left + tx, top + ty, right + tx, bottom + ty);
        mShadowDrawable.draw(c);
    }

    c.restoreToCount(savedCount);

    ViewCompat.postInvalidateOnAnimation(parent);
}

From source file:com.example.nwilde.myfirstapp.SimpleListDividerDecorator.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int childCount = parent.getChildCount();
    final float yPositionThreshold = (mOverlap) ? 1.0f : (mDividerHeight + 1.0f); // [px]
    final float zPositionThreshold = 1.0f; // [px]

    if (childCount == 0) {
        return;//ww w  .jav a 2s  . c  om
    }

    int savedCount = c.save(Canvas.CLIP_SAVE_FLAG);

    c.clipRect(parent.getLeft() + parent.getPaddingLeft(), parent.getTop() + parent.getPaddingTop(),
            parent.getRight() - parent.getPaddingRight(), parent.getBottom() + parent.getPaddingBottom());

    for (int i = 0; i < childCount - 1; i++) {
        final View child = parent.getChildAt(i);
        final View nextChild = parent.getChildAt(i + 1);

        if ((child.getVisibility() != View.VISIBLE) || (nextChild.getVisibility() != View.VISIBLE)) {
            continue;
        }

        // check if the next item is placed at the bottom
        final float childBottom = child.getBottom() + ViewCompat.getTranslationY(child);
        final float nextChildTop = nextChild.getTop() + ViewCompat.getTranslationY(nextChild);

        if (!(Math.abs(nextChildTop - childBottom) < yPositionThreshold)) {
            continue;
        }

        // check if the next item is placed on the same plane
        final float childZ = ViewCompat.getTranslationZ(child) + ViewCompat.getElevation(child);
        final float nextChildZ = ViewCompat.getTranslationZ(nextChild) + ViewCompat.getElevation(nextChild);

        if (!(Math.abs(nextChildZ - childZ) < zPositionThreshold)) {
            continue;
        }

        final float childAlpha = ViewCompat.getAlpha(child);
        final float nextChildAlpha = ViewCompat.getAlpha(nextChild);

        final int tx = (int) (ViewCompat.getTranslationX(child) + 0.5f);
        final int ty = (int) (ViewCompat.getTranslationY(child) + 0.5f);
        final int left = child.getLeft();
        final int right = child.getRight();
        final int top = child.getBottom();
        final int bottom = top + mDividerHeight;

        mDividerDrawable.setAlpha((int) ((0.5f * 255) * (childAlpha + nextChildAlpha) + 0.5f));
        mDividerDrawable.setBounds(left + tx, top + ty, right + tx, bottom + ty);
        mDividerDrawable.draw(c);
    }

    c.restoreToCount(savedCount);
}

From source file:net.networksaremadeofstring.rhybudd.RhybuddDock.java

private void drawGaugeNeedle(Canvas canvas, int count, int Scale) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    float divisor = 360.0f / Scale;

    canvas.rotate((float) (divisor * count), 100, 100);

    //Inside//from  ww w. j  a  v a  2  s  .c  o  m
    Paint needleInsidePaint = new Paint();
    needleInsidePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    needleInsidePaint.setColor(Color.WHITE);
    needleInsidePaint.setStrokeWidth(4);
    needleInsidePaint.setAntiAlias(true);

    Paint needleEdgePaint = new Paint();
    needleEdgePaint.setStyle(Paint.Style.STROKE);
    needleEdgePaint.setColor(Color.DKGRAY);
    needleEdgePaint.setStrokeWidth(0.5f);
    needleEdgePaint.setAntiAlias(true);

    canvas.drawOval(new RectF(95, 95, 105, 105), needleInsidePaint);
    canvas.drawOval(new RectF(95, 96, 105, 105), needleEdgePaint);

    Path needleInside = new Path();
    needleInside.moveTo(98, 98);
    needleInside.lineTo(100, 20);
    needleInside.lineTo(102, 102);
    canvas.drawPath(needleInside, needleInsidePaint);

    Path needleEdge = new Path();
    needleInside.moveTo(99, 99);
    needleInside.lineTo(99, 19);
    needleInside.lineTo(103, 103);

    canvas.drawPath(needleEdge, needleEdgePaint);
    canvas.restore();
}

From source file:me.lizheng.deckview.views.DeckChildViewHeader.java

@SuppressLint("DrawAllocation")
@Override/* w w  w  . j  av a2s.co  m*/
protected void onDraw(Canvas canvas) {
    // Draw the highlight at the top edge (but put the bottom edge just out of view)
    float offset = (float) Math.ceil(mConfig.taskViewHighlightPx / 2f);
    float radius = mConfig.taskViewRoundedCornerRadiusPx;
    int count = canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipRect(0, 0, getMeasuredWidth(), getMeasuredHeight());
    canvas.drawRoundRect(
            new RectF(-offset, 0f, (float) getMeasuredWidth() + offset, getMeasuredHeight() + radius), radius,
            radius, sHighlightPaint);
    canvas.restoreToCount(count);
}

From source file:com.github.shareme.gwsmaterialuikit.library.advancerv.draggable.DraggingItemDecorator.java

private Bitmap createDraggingItemImage(View v, NinePatchDrawable shadow) {
    int width = v.getWidth() + mShadowPadding.left + mShadowPadding.right;
    int height = v.getHeight() + mShadowPadding.top + mShadowPadding.bottom;

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    final Canvas canvas = new Canvas(bitmap);

    if (shadow != null) {
        shadow.setBounds(0, 0, width, height);
        shadow.draw(canvas);//from w  w  w.j  a v  a  2s  .  co  m
    }

    final int savedCount = canvas.save(Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG);
    // NOTE: Explicitly set clipping rect. This is required on Gingerbread.
    canvas.clipRect(mShadowPadding.left, mShadowPadding.top, width - mShadowPadding.right,
            height - mShadowPadding.bottom);
    canvas.translate(mShadowPadding.left, mShadowPadding.top);
    v.draw(canvas);
    canvas.restoreToCount(savedCount);

    return bitmap;
}

From source file:com.appsimobile.appsii.module.home.SunriseDrawable.java

private float drawArc(Canvas canvas, Rect bounds) {
    canvas.drawPath(mArc, mArcPaint);//from w w  w .ja v a2  s. c  o m

    int minuteSinceStart = mNowMinutes - mRiseMinutes;
    int totalMinutes = mSetMinutes - mRiseMinutes;
    float pct = (minuteSinceStart / (float) totalMinutes);

    int r = bounds.right - mRightOffset;
    int l = bounds.left + mLeftOffset;

    int px = (int) (pct * (r - l));

    mClipRect.set(0, 0, l + px, bounds.bottom);
    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipRect(mClipRect);
    canvas.drawPath(mArcFill, mArcFillPaint);
    canvas.restore();

    return pct;
}

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

/**
 * ?? ??/*from   w w w. j a v  a  2s . c o  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:com.hippo.largeimageview.LargeImageView.java

private int transformCanvas(Canvas canvas) {
    final int saved;
    switch (mOrientation) {
    case ORIENTATION_0:
        saved = 0;/*from w w w. j a  va 2 s .c o  m*/
        break;
    case ORIENTATION_90:
        saved = canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
        break;
    case ORIENTATION_180:
        saved = canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.translate(getWidth(), getHeight());
        canvas.rotate(180);
        break;
    case ORIENTATION_270:
        saved = canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
        break;
    default:
        throw new IllegalStateException("Unknown orientation: " + mOrientation);
    }
    return saved;
}

From source file:com.android.widget.SlidingPaneLayout.java

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

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);//from www  .j a  v a2 s .com
        mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());
        canvas.clipRect(mTmpRect);
    }

    result = super.drawChild(canvas, child, drawingTime);

    canvas.restoreToCount(save);

    return result;
}

From source file:uk.ac.kent.jb509.shopper.utils.SlidingUpPanelLayout.java

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

    boolean drawScrim = false;

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately
        // be covered.
        canvas.getClipBounds(mTmpRect);/* w  w  w. ja v a2 s  . co m*/
        mTmpRect.bottom = Math.min(mTmpRect.bottom, mSlideableView.getTop());

        if (!mIsTransparent) {
            canvas.clipRect(mTmpRect);
        }

        if (mSlideOffset < 1) {
            drawScrim = true;
        }
    }

    result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(save);

    if (drawScrim) {
        final int baseAlpha = (mCoveredFadeColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * (1 - mSlideOffset));
        final int color = imag << 24 | mCoveredFadeColor & 0xffffff;
        mCoveredFadePaint.setColor(color);
        canvas.drawRect(mTmpRect, mCoveredFadePaint);
    }

    return result;
}