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

Source Link

Document

Saves the current matrix and clip onto a private stack.

Usage

From source file:Main.java

public static boolean drawDrawables(Canvas canvas, @Nonnull TextView textView) {
    final int compoundPaddingLeft = textView.getCompoundPaddingLeft();
    final int compoundPaddingTop = textView.getCompoundPaddingTop();
    final int compoundPaddingRight = textView.getCompoundPaddingRight();
    final int compoundPaddingBottom = textView.getCompoundPaddingBottom();

    final int scrollX = textView.getScrollX();
    final int scrollY = textView.getScrollY();

    final int right = textView.getRight();
    final int left = textView.getLeft();
    final int bottom = textView.getBottom();
    final int top = textView.getTop();

    final Drawable[] drawables = textView.getCompoundDrawables();
    if (drawables != null) {

        int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
        int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;

        Drawable topDr = drawables[1];// w w w  .  j a v a 2  s .c o m
        // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
        // Make sure to update invalidateDrawable() when changing this code.
        if (topDr != null) {
            canvas.save();
            canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2,
                    scrollY + textView.getPaddingTop() + vspace / 2);
            topDr.draw(canvas);
            canvas.restore();
            return true;
        }
    }

    return false;
}

From source file:com.pax.view.keyboard.CustomKeyboardView.java

@Override
public void onDraw(Canvas canvas) {
    List<Keyboard.Key> keys = getKeyboard().getKeys();
    for (Keyboard.Key key : keys) {
        canvas.save();

        int offsetY = 0;
        if (key.y == 0) {
            offsetY = 1;/*w w  w.  j  a  v  a2 s. c  o m*/
        }
        int initDrawY = key.y + offsetY;
        Rect rect = new Rect(key.x, initDrawY, key.x + key.width, key.y + key.height);
        canvas.clipRect(rect);

        Drawable drawable = null;
        if (null != key.codes && key.codes.length != 0) {
            int primaryCode = key.codes[0];

            if (primaryCode < 0) {
                drawable = mOpKeyBgDrawable;
            } else {
                drawable = mKeyBgDrawable;
            }
        }

        if (null != drawable && null == key.icon) {
            int[] state = key.getCurrentDrawableState();
            drawable.setState(state);
            drawable.setBounds(rect);
            drawable.draw(canvas);
        }

        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(50);
        paint.setColor(Color.BLACK);

        if (key.icon != null) {
            int[] state = key.getCurrentDrawableState();
            key.icon.setState(state);
            key.icon.setBounds(rect);
            key.icon.draw(canvas);
        }

        if (key.label != null) {
            canvas.drawText(key.label.toString(), key.x + (key.width / 2),
                    initDrawY + (key.height + paint.getTextSize() - paint.descent()) / 2, paint);
        }
        canvas.restore();
    }
}

From source file:tw.firemaples.onscreenocr.views.AreaSelectionView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!isInEditMode()) {
        canvas.save();

        if (drawingStartPoint != null && drawingEndPoint != null) {
            //                canvas.drawLine(drawingStartPoint.x, drawingStartPoint.y, drawingEndPoint.x, drawingEndPoint.y, drawingLinePaint);
            canvas.drawRect(getNewBox(drawingStartPoint, drawingEndPoint), drawingLinePaint);
        }/*www.  j  ava 2s.co m*/

        for (Rect box : boxList) {
            canvas.drawRect(box, boxPaint);
        }

        canvas.restore();
    }
}

From source file:com.tr4android.support.extension.drawable.IndeterminateProgressDrawable.java

@Override
public void draw(Canvas canvas) {
    int saveCount = canvas.save();
    canvas.rotate(mRotation, mArcRect.centerX(), mArcRect.centerY());

    float startAngle = -90 + 360 * (mOffset + mStart);
    float sweepAngle = 360 * (mEnd - mStart);
    canvas.drawArc(mArcRect, startAngle, sweepAngle, false, mArcPaint);

    canvas.restoreToCount(saveCount);/*w  ww  .j  a  va  2 s . c  o m*/
}

From source file:im.ene.ribbon.FixedActionTabView.java

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

    canvas.save();
    canvas.translate(0, iconTranslation);
    icon.draw(canvas);/*from w  w  w . j  a v a  2s  .com*/
    drawBadge(canvas);
    canvas.restore();

    canvas.save();
    canvas.scale(canvasTextScale, canvasTextScale, textCenterX, textCenterY);
    canvas.drawText(getAction().getTitle(), textX, textY, textPaint);
    canvas.restore();
}

From source file:at.linuxtage.companion.widgets.ScrimInsetsFrameLayout.java

@Override
public void draw(@NonNull Canvas canvas) {
    super.draw(canvas);
    int width = getWidth();
    int height = getHeight();
    if (mInsets != null && mInsetForeground != null) {
        int sc = canvas.save();
        canvas.translate(getScrollX(), getScrollY());
        // Top//from  w  w w. ja v a  2s.c o  m
        mTempRect.set(0, 0, width, mInsets.top);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        // Bottom
        mTempRect.set(0, height - mInsets.bottom, width, height);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        // Left
        mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        // Right
        mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        canvas.restoreToCount(sc);
    }
}

From source file:com.handmark.pulltorefresh.library.internal.LoadingLayout.java

private void rotateArrow() {
    final Drawable drawable = mHeaderArrow.getDrawable();
    final Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Config.ARGB_8888);//from  www  .j  a v a 2  s  .  c o  m
    final Canvas canvas = new Canvas(bitmap);
    canvas.save();
    canvas.rotate(180.0f, canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    canvas.restore();
    mHeaderArrow.setImageBitmap(bitmap);
}

From source file:com.mobidev.myliveonavigationmaterial.ui.widget.ScrimInsetsFrameLayout.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    int width = getWidth();
    int height = getHeight();
    if (mInsets != null && mInsetForeground != null) {
        int sc = canvas.save();
        canvas.translate(getScrollX(), getScrollY());
        // Top/*from w  w w .  j av  a2 s .  co  m*/
        mTempRect.set(0, 0, width, mInsets.top);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        // Bottom
        mTempRect.set(0, height - mInsets.bottom, width, height);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        // Left
        mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        // Right
        mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom);
        mInsetForeground.setBounds(mTempRect);
        mInsetForeground.draw(canvas);
        canvas.restoreToCount(sc);
    }
}

From source file:net.yanzm.actionbarprogress.MaterialIndeterminateProgressDrawable.java

@Override
public void draw(Canvas canvas) {
    final int level = getLevel();
    final float input = level / 10000f;

    canvas.drawColor(trackColor);//from   w w w  .  j  a va  2  s.  c om

    canvas.save();
    canvas.translate(canvas.getWidth() / 2f, 0);
    canvas.scale(canvas.getWidth() / 360f, 1);

    paint.setColor(accentColor);

    {
        final int saveCount = canvas.save();

        float translateX2 = translateInterpolator2.getInterpolation(input);
        canvas.translate(-197.60001f + translateX2 * 620.20002f, 0);

        float x = scaleInterpolator2.getInterpolation(input);
        float scaleX = 1.6199005127f * -Math.abs(x - 0.5f) + 0.909950256348f;
        rect2.set(-144f * scaleX, 0, 144f * scaleX, canvas.getHeight());
        canvas.drawRect(rect2, paint);

        canvas.restoreToCount(saveCount);
    }
    {
        final int saveCount = canvas.save();

        float translateX1 = translateInterpolator1.getInterpolation(input);
        canvas.translate(-522.59998f + translateX1 * 722.19999f, 0);

        float x = scaleInterpolator1.getInterpolation(input);
        float scaleX = 1.45369842529f * -Math.abs(x - 0.5f) + 0.826849212646f;
        rect1.set(-144f * scaleX, 0, 144f * scaleX, canvas.getHeight());
        canvas.drawRect(rect1, paint);

        canvas.restoreToCount(saveCount);
    }

    canvas.restore();
}

From source file:com.appeaser.sublimenavigationviewlibrary.ScrimInsetsFrameLayout.java

public void draw(@NonNull Canvas canvas) {
    super.draw(canvas);
    int width = this.getWidth();
    int height = this.getHeight();
    if (mInsets != null && mInsetForeground != null) {
        int sc = canvas.save();
        canvas.translate(getScrollX(), getScrollY());

        this.mTempRect.set(0, 0, width, this.mInsets.top);
        this.mInsetForeground.setBounds(this.mTempRect);
        this.mInsetForeground.draw(canvas);

        this.mTempRect.set(0, height - this.mInsets.bottom, width, height);
        this.mInsetForeground.setBounds(this.mTempRect);
        this.mInsetForeground.draw(canvas);

        this.mTempRect.set(0, this.mInsets.top, this.mInsets.left, height - this.mInsets.bottom);
        this.mInsetForeground.setBounds(this.mTempRect);
        this.mInsetForeground.draw(canvas);

        this.mTempRect.set(width - this.mInsets.right, this.mInsets.top, width, height - this.mInsets.bottom);
        this.mInsetForeground.setBounds(this.mTempRect);
        this.mInsetForeground.draw(canvas);

        canvas.restoreToCount(sc);//w  w w. ja  v a 2s  . co  m
    }
}