Example usage for android.graphics Canvas restore

List of usage examples for android.graphics Canvas restore

Introduction

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

Prototype

public void restore() 

Source Link

Document

This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call.

Usage

From source file:com.ecom.consumer.customviews.HorizontalListView.java

@Override
protected void dispatchDraw(Canvas canvas) {
    canvas.save();/*from  w w w  . ja  va 2  s .  co m*/
    super.dispatchDraw(canvas);

    if (outRect != null && !mIsBeingDragged) {
        ColorDrawable drawable = new ColorDrawable(0x884193C9);// 77318fd5new
        // ColorDrawable(0x440000ff
        // &
        // Color.BLUE);
        drawable.setBounds(outRect);
        drawable.draw(canvas);
    }
    outRect = null;
    drawEdgeGlow(canvas);
    canvas.restore();
}

From source file:org.akop.crosswords.view.CrosswordView.java

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

    if (mPuzzleBitmap != null) {
        canvas.save();/*from ww w .  ja  va2  s  .  c o  m*/
        canvas.clipRect(mContentRect);
        canvas.translate(mBitmapOffset.x, mBitmapOffset.y);
        canvas.scale(mBitmapScale, mBitmapScale);
        canvas.drawBitmap(mPuzzleBitmap, 0, 0, mBitmapPaint);

        canvas.restore();
    }
}

From source file:com.irccloud.android.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();//from   w  ww  . j a v  a2 s  .c  o m
    final boolean isRtl = isLayoutRtl();
    // Interpolated widths of arrow bars
    final float arrowSize = lerp(mBarSize, mTopBottomArrowSize, mProgress);
    final float middleBarSize = lerp(mBarSize, mMiddleArrowSize, mProgress);
    // Interpolated size of middle bar
    final float middleBarCut = lerp(0, mBarThickness / 2, mProgress);
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(isRtl ? 0 : -180, isRtl ? 180 : 0, mProgress);
    final float topBottomBarOffset = lerp(mBarGap + mBarThickness, 0, mProgress);
    mPath.rewind();

    final float arrowEdge = -middleBarSize / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + middleBarCut, 0);
    mPath.rLineTo(middleBarSize - middleBarCut, 0);

    final float arrowWidth = Math.round(arrowSize * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowSize * Math.sin(rotation));

    // top bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // bottom bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);
    mPath.moveTo(0, 0);
    mPath.close();

    canvas.save();
    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ isRtl) ? -1 : 1), bounds.centerX(), bounds.centerY());
    } else if (isRtl) {
        canvas.rotate(180, bounds.centerX(), bounds.centerY());
    }
    canvas.translate(bounds.centerX(), bounds.centerY());
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}

From source file:com.morninz.ninepinview.widget.NinePINView.java

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

    // draw circles around center points and connection line
    int drawnCount = mDrawnPoints.size();
    for (int j = 0; j < drawnCount; j++) {
        Point p1 = mDrawnPoints.get(j);
        canvas.drawCircle(p1.x, p1.y, mCircleRadius, mCirclePaint);
        if (j + 1 < drawnCount) {
            Point p2 = mDrawnPoints.get(j + 1);
            canvas.drawCircle(p2.x, p2.y, mCircleRadius, mCirclePaint);
            canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mLinePaint);
            if (mWillDrawWrongTriangle) {
                // compute the wrong triangle's direction of this point.
                float angle = 0.f;
                if (p2.y == p1.y) {// x-axis
                    angle = p2.x > p1.x ? 0.f : 180.f;
                } else if (p2.x == p1.x) { // y-axis
                    angle = p2.y > p1.y ? 90.f : -90.f;
                } else {// in quadrants
                    double tanA = ((double) p2.y - (double) p1.y) / ((double) p2.x - (double) p1.x);
                    // in 1 or 4 quadrant
                    angle = (float) (Math.atan(tanA) * 180 / Math.PI);
                    // in 2 or 3 quadrant
                    if (p2.x < p1.x) {
                        angle += 180.f;/*from  ww  w . j  ava  2 s . c  om*/
                    }
                }
                Log.d(TAG, "angle " + angle);
                canvas.save();
                canvas.rotate(angle, p1.x, p1.y);
                canvas.drawPath(mWrongPaths[p1.index], mWrongPaint);
                canvas.restore();
            }
        }
    }

    // draw extra connection line
    if (mLastDrawnPoint != null) {
        canvas.drawLine(mLastDrawnPoint.x, mLastDrawnPoint.y, mCurrX, mCurrY, mLinePaint);
    }

    // draw 9 center points
    for (int i = 0; i < POINT_COUNT; i++) {
        Point p = mCenterPoints[i];
        canvas.drawCircle(p.x, p.y, mPointSize, mPointPaint);
    }
}

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

private void drawScale(Canvas canvas, Boolean Colors, int Count, int Max) {
    RectF faceRect = new RectF();
    faceRect.set(10, 10, 190, 190);/*from  w w  w .  j a  v a2 s  .c o  m*/

    Paint scalePaint = new Paint();
    scalePaint.setStyle(Paint.Style.STROKE);
    scalePaint.setColor(getResources().getColor(R.color.WarningGreen));
    scalePaint.setStrokeWidth(1);
    scalePaint.setAntiAlias(true);

    scalePaint.setTextSize(12);
    scalePaint.setTypeface(Typeface.createFromAsset(this.getAssets(), "fonts/chivo.ttf"));
    scalePaint.setTextAlign(Paint.Align.CENTER);

    float scalePosition = 10;
    RectF scaleRect = new RectF();
    scaleRect.set(faceRect.left + scalePosition, faceRect.top + scalePosition, faceRect.right - scalePosition,
            faceRect.bottom - scalePosition);

    if (!Colors)
        scalePaint.setColor(Color.WHITE);

    scalePaint.setStrokeWidth(2);
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    for (int i = 0; i < Max; ++i) {
        if (Colors) {
            if (i > 20)
                scalePaint.setColor(getResources().getColor(R.color.WarningYellow));

            if (i > 40)
                scalePaint.setColor(getResources().getColor(R.color.WarningOrange));

            if (i > 60)
                scalePaint.setColor(getResources().getColor(R.color.WarningRed));
        }

        canvas.drawLine(100, 20, 100, 18, scalePaint);
        int divisor = 5;

        if (Max > 100)
            divisor = 25;

        if (i % divisor == 0) {
            canvas.drawText(Integer.toString(i), 100, 16, scalePaint);
        }

        canvas.rotate((360.0f / Max), 100, 100);
    }

    canvas.restore();
}

From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * ??//from   ww  w  .j a v  a2  s  .co  m
 *
 * @param canvas     
 * @param position   ???
 * @param itemWidth  ?
 * @param itemHeight ?
 */
protected void drawItemGradient(Canvas canvas, int position, int itemWidth, int itemHeight) {
    if (mGradient == null || !mGradient.isStateful())
        return;
    final int normalColor = mGradient.getDefaultColor();
    final int selectedColor = mGradient.getColorForState(SELECTED_STATE_SET, normalColor);
    if (position == mNextPager) {
        mTextPaint.setColor(getColor(normalColor, selectedColor, mOffset));
    } else if (position == mCurrentPager) {
        mTextPaint.setColor(getColor(normalColor, selectedColor, 1 - mOffset));
    } else {
        mTextPaint.setColor(normalColor);
    }
    final float moveX = ViewCompat.getPaddingStart(this) + (itemWidth + getIntervalWidth()) * position;
    final float moveY = getPaddingTop();
    final int restWidth = position == getItemCount() - 1 ? getLastItemWidth() : itemWidth;
    canvas.save();
    canvas.translate(moveX, moveY);
    canvas.drawRect(0, 0, restWidth, itemHeight, mTextPaint);
    canvas.restore();
}

From source file:org.androfarsh.widget.DragGridLayout.java

private boolean drawChildDrawable(BitmapDrawable childDrawable, Canvas canvas, View child, long drawingTime) {
    canvas.save();/*from   ww w .  ja  v  a2s .  c o  m*/
    requestCurrentRect(mTmpRect, child);
    if (child.getAnimation() == null) {
        canvas.clipRect(mTmpRect);
    }

    final boolean result;
    if (mEditMode && (childDrawable != null) && !childDrawable.getBitmap().isRecycled()) {
        childDrawable.setBounds(mTmpRect);
        childDrawable.draw(canvas);
        result = false;
    } else {
        result = super.drawChild(canvas, child, drawingTime);
    }
    canvas.restore();
    return result;
}

From source file:com.android.leanlauncher.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw//from w ww .  j a va  2 s .  c  o m
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);
    }
    destCanvas.restore();
}

From source file:com.jude.easyrecyclerview.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//from   w w  w.j a v  a2  s .  c  om
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        int adapterPos = parent.getChildAdapterPosition(child);

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof RecyclerArrayAdapter) {
                int headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((RecyclerArrayAdapter) parent.getAdapter()).getCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top);

                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(canvas);
                canvas.restore();
            }
        }
    }
}

From source file:com.youth.xframe.adapter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//w w  w .j  ava  2 s . c o m
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        int adapterPos = parent.getChildAdapterPosition(child);

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof XRecyclerViewAdapter) {
                int headerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((XRecyclerViewAdapter) parent.getAdapter()).getDataCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top);

                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(canvas);
                canvas.restore();
            }
        }
    }
}