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.android.launcher3.folder.FolderIcon.java

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

    if (mReferenceDrawable != null) {
        computePreviewDrawingParams(mReferenceDrawable);
    }//from www .  j a  v  a2  s.c om

    if (!mBackground.drawingDelegated()) {
        mBackground.drawBackground(canvas, mBgPaint);
    }

    if (mFolder == null)
        return;
    if (mFolder.getItemCount() == 0 && !mAnimating)
        return;

    canvas.save();

    if (mPreviewLayoutRule.clipToBackground()) {
        mBackground.clipCanvas(canvas);
    }

    // The items are drawn in coordinates relative to the preview offset
    canvas.translate(mBackground.basePreviewOffsetX, mBackground.basePreviewOffsetY);

    // The first item should be drawn last (ie. on top of later items)
    for (int i = mDrawingParams.size() - 1; i >= 0; i--) {
        PreviewItemDrawingParams p = mDrawingParams.get(i);
        if (!p.hidden) {
            drawPreviewItem(canvas, p);
        }
    }
    canvas.restore();

    if (mPreviewLayoutRule.clipToBackground() && !mBackground.drawingDelegated()) {
        mBackground.drawBackgroundStroke(canvas, mBgPaint);
    }
}

From source file:com.jjoe64.graphview.GridLabelRenderer.java

/**
 * draws the vertical axis title if/*from w ww .  ja va  2 s.co  m*/
 * it is set
 * @param canvas canvas
 */
protected void drawVerticalAxisTitle(Canvas canvas) {
    if (mVerticalAxisTitle != null && mVerticalAxisTitle.length() > 0) {
        mPaintAxisTitle.setColor(getVerticalAxisTitleColor());
        mPaintAxisTitle.setTextSize(getVerticalAxisTitleTextSize());
        float x = getVerticalAxisTitleWidth();
        float y = canvas.getHeight() / 2;
        canvas.save();
        canvas.rotate(-90, x, y);
        canvas.drawText(mVerticalAxisTitle, x, y, mPaintAxisTitle);
        canvas.restore();
    }
}

From source file:org.akvo.caddisfly.sensor.colorimetry.strip.widget.PercentageMeterView.java

@Override
public void onDraw(@NonNull Canvas canvas) {

    if (Float.isNaN(percentage)) {
        return;//from   ww  w .j a  v a 2 s . c om
    }

    double number = (100 - percentage) * NUMBER_OF_BARS * PERCENT;

    canvas.save();

    // Set each dot's diameter to half the canvas height
    canvas.translate(
            canvas.getWidth() / 2f - ((canvas.getHeight() / 2f + GUTTER_SPACE) * ((NUMBER_OF_BARS - 1) / 2f)),
            0);

    for (double i = 0; i < NUMBER_OF_BARS; i++) {

        // Reset color
        paint.setColor(Color.LTGRAY);

        if (number >= 0) {
            if (i < 2) {
                // Red if number is lower than i + 1
                // 2 red dots if number < 1 or 1 red dot if number > 1
                if (number <= i + 1) {
                    paint.setColor(red);
                }
            } else if (i < 4) {
                // Orange if number between 1 and 4
                // if number == 1.5 then 1 red followed by orange dot
                // if number == 2.5 then 2 orange dots
                // if number == 3.5 then 1 orange dot
                if (number >= i - 1 && number <= i + 2) {
                    paint.setColor(orange);
                }
            } else {
                if (number > i) {
                    // Green if number larger than 3 but yellow if number exceeds optimum
                    if (number > NUMBER_OF_BARS) {
                        paint.setColor(Color.YELLOW);
                    } else {
                        paint.setColor(green);
                    }
                }
            }
        }

        canvas.drawCircle(0, canvas.getHeight() / 2f, canvas.getHeight() / 4f, paint);

        // Position next circle
        canvas.translate(canvas.getHeight() / 2f + GUTTER_SPACE, 0);
    }

    canvas.restore();

    super.onDraw(canvas);
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.RadialTimePickerView.java

private void drawMinutes(Canvas canvas, float alphaMod) {
    final int minutesAlpha = (int) (mAlpha[MINUTES].getValue() * alphaMod + 0.5f);
    if (minutesAlpha > 0) {
        // Draw the minute selector under the elements.
        drawSelector(canvas, MINUTES, mSelectorPath, alphaMod);

        // Exclude the selector region, then draw minutes with no
        // activated states.
        canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipPath(mSelectorPath, Region.Op.DIFFERENCE);
        drawTextElements(canvas, mTextSize[MINUTES], mTypeface, mTextColor[MINUTES], mMinutesText,
                mOuterTextX[MINUTES], mOuterTextY[MINUTES], mPaint[MINUTES], minutesAlpha, false, 0, false);
        canvas.restore();

        // Intersect the selector region, then draw minutes with only
        // activated states.
        canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipPath(mSelectorPath, Region.Op.INTERSECT);
        drawTextElements(canvas, mTextSize[MINUTES], mTypeface, mTextColor[MINUTES], mMinutesText,
                mOuterTextX[MINUTES], mOuterTextY[MINUTES], mPaint[MINUTES], minutesAlpha, true,
                mSelectionDegrees[MINUTES], true);
        canvas.restore();//from  ww  w  .ja  va 2s.  c  o  m
    }
}

From source file:com.byagowi.persiancalendar.view.QiblaCompassView.java

public void drawMoon(Canvas canvas) {
    moonPaint.reset();//from   w  w  w  . ja  v  a2s  .  c  o m
    moonPaint.setColor(Color.WHITE);
    moonPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    moonPaintB.reset();// moon Paint Black
    moonPaintB.setColor(Color.BLACK);
    moonPaintB.setStyle(Paint.Style.FILL_AND_STROKE);
    moonPaintO.reset();// moon Paint for Oval
    moonPaintO.setColor(Color.WHITE);
    moonPaintO.setStyle(Paint.Style.FILL_AND_STROKE);
    moonPaintD.reset();// moon Paint for Diameter
    // draw
    moonPaintD.setColor(Color.GRAY);
    moonPaintD.setStyle(Paint.Style.STROKE);
    double moonPhase = sunMoonPosition.getMoonPhase();
    if (moonPosition.getElevation() > -5) {
        canvas.rotate((float) moonPosition.getAzimuth() - 360, px, py);
        int eOffset = (int) ((moonPosition.getElevation() / 90) * Radius);
        // elevation Offset 0 for 0 degree; r for 90 degree
        moonRect.set(px - r, py + eOffset - Radius - r, px + r, py + eOffset - Radius + r);
        canvas.drawArc(moonRect, 90, 180, false, moonPaint);
        canvas.drawArc(moonRect, 270, 180, false, moonPaintB);
        int arcWidth = (int) ((moonPhase - 0.5) * (4 * r));
        moonPaintO.setColor(arcWidth < 0 ? Color.BLACK : Color.WHITE);
        moonOval.set(px - Math.abs(arcWidth) / 2, py + eOffset - Radius - r, px + Math.abs(arcWidth) / 2,
                py + eOffset - Radius + r);
        canvas.drawArc(moonOval, 0, 360, false, moonPaintO);
        canvas.drawArc(moonRect, 0, 360, false, moonPaintD);
        moonPaintD.setPathEffect(dashPath);
        canvas.drawLine(px, py - Radius, px, py + Radius, moonPaintD);
        moonPaintD.setPathEffect(null);
        canvas.restore();

    }

}

From source file:com.mjhram.geodata.GpsMainActivity.java

public Bitmap drawMultilineTextToBitmap(Context gContext, Bitmap bitmap, String gText) {
    // prepare canvas
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    //Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/* ww w  .j a va 2s.c o m*/
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);

    // new antialiased Paint
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (20 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    //canvas.drawText("This is", 100, 100, paint);
    //canvas.drawText("multi-line", 100, 150, paint);
    //canvas.drawText("text", 100, 200, paint);

    // set text width to canvas width minus 16dp padding
    int textWidth = canvas.getWidth() - (int) (16 * scale);

    // init StaticLayout for text
    StaticLayout textLayout = new StaticLayout(gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f,
            0.0f, false);

    // get height of multiline text
    int textHeight = textLayout.getHeight();

    // get position of text's top left corner
    float x = (bitmap.getWidth() - textWidth) / 2;
    float y = bitmap.getHeight() - textHeight;

    // draw text to the Canvas center
    canvas.save();
    canvas.translate(x, y);
    textLayout.draw(canvas);
    canvas.restore();

    return bitmap;
}

From source file:org.telegram.ui.Cells.DialogCell.java

@Override
protected void onDraw(Canvas canvas) {
    if (currentDialogId == 0) {
        return;//from   ww  w .  j  av a 2s  .c om
    }

    if (isSelected) {
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), backPaint);
    }

    if (drawNameLock) {
        setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop);
        lockDrawable.draw(canvas);
    } else if (drawNameGroup) {
        setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop);
        groupDrawable.draw(canvas);
    } else if (drawNameBroadcast) {
        setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop);
        broadcastDrawable.draw(canvas);
    } else if (drawNameBot) {
        setDrawableBounds(botDrawable, nameLockLeft, nameLockTop);
        botDrawable.draw(canvas);
    }

    if (nameLayout != null) {
        canvas.save();
        canvas.translate(nameLeft, AndroidUtilities.dp(13));
        nameLayout.draw(canvas);
        canvas.restore();
    }

    canvas.save();
    canvas.translate(timeLeft, timeTop);
    timeLayout.draw(canvas);
    canvas.restore();

    if (messageLayout != null) {
        canvas.save();
        canvas.translate(messageLeft, messageTop);
        try {
            messageLayout.draw(canvas);
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
        canvas.restore();
    }

    if (drawClock) {
        setDrawableBounds(clockDrawable, checkDrawLeft, checkDrawTop);
        clockDrawable.draw(canvas);
    } else if (drawCheck2) {
        if (drawCheck1) {
            setDrawableBounds(halfCheckDrawable, halfCheckDrawLeft, checkDrawTop);
            halfCheckDrawable.draw(canvas);
            setDrawableBounds(checkDrawable, checkDrawLeft, checkDrawTop);
            checkDrawable.draw(canvas);
        } else {
            setDrawableBounds(checkDrawable, checkDrawLeft, checkDrawTop);
            checkDrawable.draw(canvas);
        }
    }

    if (dialogMuted && !drawVerified) {
        setDrawableBounds(muteDrawable, nameMuteLeft, AndroidUtilities.dp(16.5f));
        muteDrawable.draw(canvas);
    } else if (drawVerified) {
        setDrawableBounds(verifiedDrawable, nameMuteLeft, AndroidUtilities.dp(16.5f));
        verifiedDrawable.draw(canvas);
    }

    if (drawError) {
        setDrawableBounds(errorDrawable, errorLeft, errorTop);
        errorDrawable.draw(canvas);
    } else if (drawCount) {
        if (dialogMuted) {
            setDrawableBounds(countDrawableGrey, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawableGrey.draw(canvas);
        } else {
            setDrawableBounds(countDrawable, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawable.draw(canvas);
        }
        canvas.save();
        canvas.translate(countLeft, countTop + AndroidUtilities.dp(4));
        if (countLayout != null) {
            countLayout.draw(canvas);
        }
        canvas.restore();
    }

    if (useSeparator) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, linePaint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, linePaint);
        }
    }

    avatarImage.draw(canvas);
}

From source file:com.mukesh.OtpView.java

private void drawOtpView(Canvas canvas) {
    int nextItemToFill;
    if (rtlTextDirection) {
        nextItemToFill = otpViewItemCount - 1;
    } else {/*from ww  w.  ja  va  2s  .  co  m*/
        if (getText() != null) {
            nextItemToFill = getText().length();
        } else {
            nextItemToFill = 0;
        }
    }
    for (int i = 0; i < otpViewItemCount; i++) {
        boolean itemSelected = isFocused() && nextItemToFill == i;
        boolean itemFilled = i < nextItemToFill;
        int[] itemState = null;
        if (itemFilled) {
            itemState = FILLED_STATE;
        } else if (itemSelected) {
            itemState = SELECTED_STATE;
        }
        paint.setColor(itemState != null ? getLineColorForState(itemState) : cursorLineColor);
        updateItemRectF(i);
        updateCenterPoint();
        canvas.save();
        if (viewType == VIEW_TYPE_RECTANGLE) {
            updateOtpViewBoxPath(i);
            canvas.clipPath(path);
        }
        drawItemBackground(canvas, itemState);
        canvas.restore();
        if (itemSelected) {
            drawCursor(canvas);
        }
        if (viewType == VIEW_TYPE_RECTANGLE) {
            drawOtpBox(canvas, i);
        } else if (viewType == VIEW_TYPE_LINE) {
            drawOtpLine(canvas, i);
        }
        if (DBG) {
            drawAnchorLine(canvas);
        }
        if (rtlTextDirection) {
            int reversedPosition = otpViewItemCount - i;
            if (getText().length() >= reversedPosition) {
                drawInput(canvas, i);
            } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == otpViewItemCount) {
                drawHint(canvas, i);
            }
        } else {
            if (getText().length() > i) {
                drawInput(canvas, i);
            } else if (!TextUtils.isEmpty(getHint()) && getHint().length() == otpViewItemCount) {
                drawHint(canvas, i);
            }
        }
    }
    if (isFocused() && getText() != null && getText().length() != otpViewItemCount
            && viewType == VIEW_TYPE_RECTANGLE) {
        int index = getText().length();
        updateItemRectF(index);
        updateCenterPoint();
        updateOtpViewBoxPath(index);
        paint.setColor(getLineColorForState(SELECTED_STATE));
        drawOtpBox(canvas, index);
    }
}

From source file:com.example.mylibrary.utils.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}//ww  w  .j a  v  a2  s .com
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    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.pdftron.pdf.tools.Tool.java

@Override
public boolean onDrawEdgeEffects(Canvas canvas, int width, int verticalOffset) {
    boolean needsInvalidate = false;

    if (!mEdgeEffectLeft.isFinished()) {
        canvas.save();/*from ww w .j  a v  a2s  . com*/
        try {
            canvas.translate(0, canvas.getHeight() + verticalOffset);
            canvas.rotate(-90, 0, 0);
            mEdgeEffectLeft.setSize(canvas.getHeight(), canvas.getWidth());
            if (mEdgeEffectLeft.draw(canvas)) {
                needsInvalidate = true;
            }
        } finally {
            canvas.restore();
        }
    }

    if (!mEdgeEffectRight.isFinished()) {
        canvas.save();
        try {
            canvas.translate(width, verticalOffset);
            canvas.rotate(90, 0, 0);
            mEdgeEffectRight.setSize(canvas.getHeight(), canvas.getWidth());
            if (mEdgeEffectRight.draw(canvas)) {
                needsInvalidate = true;
            }
        } finally {
            canvas.restore();
        }
    }
    return needsInvalidate;
}