List of usage examples for android.graphics Canvas drawArc
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)
Draw the specified arc, which will be scaled to fit inside the specified oval.
From source file:com.lovejjfg.demo.TouchCircleView.java
private void drawArc(Canvas canvas) { float startAngle = mCurrentGlobalAngle - mCurrentGlobalAngleOffset + START_ANGLE; float sweepAngle = mCurrentSweepAngle; if (mModeAppearing) { // paint.setColor(gradient(mColors[mCurrentColorIndex], mColors[mNextColorIndex], // mCurrentSweepAngle / (360 - MIN_SWEEP_ANGLE * 2))); sweepAngle += MIN_SWEEP_ANGLE;//www . j a v a 2 s .co m } else { startAngle = startAngle + sweepAngle; sweepAngle = 360 - sweepAngle - MIN_SWEEP_ANGLE; } canvas.drawArc(innerRectf, startAngle, sweepAngle, false, innerPaint); if (isDrawTriangle) { drawTriangle(canvas, startAngle, sweepAngle); } }
From source file:silent.kuasapmaterial.libs.ProgressWheel.java
protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (isCircleBackground) { if (isShadow) { canvas.drawCircle(circleBounds2.centerX() + mShadowSize / 2, circleBounds2.centerY() + mShadowSize, circleBounds2.width() / 2 + mShadowSize / 2, shadowPaint); }//from w w w.ja v a 2s . com canvas.drawCircle(circleBounds2.centerX(), circleBounds2.centerY(), circleBounds2.width() / 2, circlePaint); } canvas.drawArc(circleBounds, 360, 360, false, rimPaint); boolean mustInvalidate = false; if (isSpinning) { //Draw the spinning bar mustInvalidate = true; long deltaTime = (SystemClock.uptimeMillis() - lastTimeAnimated); float deltaNormalized = deltaTime * spinSpeed / 1000.0f; updateBarLength(deltaTime); mProgress += deltaNormalized; if (mProgress > 360) { mProgress -= 360f; // A full turn has been completed // we run the callback with -1 in case we want to // do something, like changing the color runCallback(-1.0f); } lastTimeAnimated = SystemClock.uptimeMillis(); float from = mProgress - 90; float length = barLength + barExtraLength; if (isInEditMode()) { from = 0; length = 135; } canvas.drawArc(circleBounds, from, length, false, barPaint); } else { float oldProgress = mProgress; if (mProgress != mTargetProgress) { //We smoothly increase the progress bar mustInvalidate = true; float deltaTime = (float) (SystemClock.uptimeMillis() - lastTimeAnimated) / 1000; float deltaNormalized = deltaTime * spinSpeed; mProgress = Math.min(mProgress + deltaNormalized, mTargetProgress); lastTimeAnimated = SystemClock.uptimeMillis(); } if (oldProgress != mProgress) { runCallback(); } float offset = 0.0f; float progress = mProgress; if (!linearProgress) { float factor = 2.0f; offset = (float) (1.0f - Math.pow(1.0f - mProgress / 360.0f, 2.0f * factor)) * 360.0f; progress = (float) (1.0f - Math.pow(1.0f - mProgress / 360.0f, factor)) * 360.0f; } if (isInEditMode()) { progress = 360; } canvas.drawArc(circleBounds, offset - 90, progress, false, barPaint); } if (mustInvalidate) { invalidate(); } }
From source file:com.winneredge.stockly.wcommons.floatingactionwidget.FloatingActionButton.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mProgressBarEnabled) { if (mShowProgressBackground) { canvas.drawArc(mProgressCircleBounds, 360, 360, false, mBackgroundPaint); }/*from ww w.j a va 2 s. c o m*/ boolean shouldInvalidate = false; if (mProgressIndeterminate) { shouldInvalidate = true; long deltaTime = SystemClock.uptimeMillis() - mLastTimeAnimated; float deltaNormalized = deltaTime * mSpinSpeed / 1000.0f; updateProgressLength(deltaTime); mCurrentProgress += deltaNormalized; if (mCurrentProgress > 360f) { mCurrentProgress -= 360f; } mLastTimeAnimated = SystemClock.uptimeMillis(); float from = mCurrentProgress - 90; float to = mBarLength + mBarExtraLength; if (isInEditMode()) { from = 0; to = 135; } canvas.drawArc(mProgressCircleBounds, from, to, false, mProgressPaint); } else { if (mCurrentProgress != mTargetProgress) { shouldInvalidate = true; float deltaTime = (float) (SystemClock.uptimeMillis() - mLastTimeAnimated) / 1000; float deltaNormalized = deltaTime * mSpinSpeed; if (mCurrentProgress > mTargetProgress) { mCurrentProgress = Math.max(mCurrentProgress - deltaNormalized, mTargetProgress); } else { mCurrentProgress = Math.min(mCurrentProgress + deltaNormalized, mTargetProgress); } mLastTimeAnimated = SystemClock.uptimeMillis(); } canvas.drawArc(mProgressCircleBounds, -90, mCurrentProgress, false, mProgressPaint); } if (shouldInvalidate) { invalidate(); } } }
From source file:com.skumar.flexibleciruclarseekbar.CircularSeekBar.java
@Override protected void onDraw(Canvas canvas) { if (!mClockwise) { canvas.scale(-1, 1, mArcRect.centerX(), mArcRect.centerY()); }/*from ww w . ja v a 2 s . c o m*/ // Set for the gradient color if (hasGradientColor) setShader(); // Draw the arcs final int arcStart = mStartAngle + mAngleOffset + mRotation; final int arcSweep = mSweepAngle; canvas.drawArc(mArcRect, arcStart, arcSweep, false, mArcPaint); // Draw the needle scale if (drawMarkings) { drawNeedleMarkings(canvas); // Draw dot markers if (hasDotMarkers) drawDotMarker(canvas); } if (mEnabled) { // Draw the thumb nail canvas.translate(mTranslateX - mThumbXPos, mTranslateY - mThumbYPos); mThumb.draw(canvas); if (hasPopupIn) { // Draw the popup canvas.translate(-mThumb.getIntrinsicWidth() / 2, -mThumb.getIntrinsicHeight() - mHeightForPopup); mPopup.draw(canvas); } } }
From source file:com.example.accessibility.ListenerView.java
@Override public void onDraw(Canvas canvas) { paintWhite.setStrokeWidth(4);//from w w w . j a v a 2 s. co m paintWhite.setARGB(128, 255, 255, 255); paintWhite.setStyle(Paint.Style.FILL_AND_STROKE); paintWhite.setAntiAlias(true); paintBlack.setStrokeWidth(4); paintBlack.setARGB(128, 0, 0, 0); paintBlack.setStyle(Paint.Style.FILL_AND_STROKE); paintBlack.setAntiAlias(true); int left = getPaddingLeft(); //int top = getPaddingTop(); int right = getWidth() - getPaddingRight(); int bottom = getHeight() - getPaddingBottom(); //-----------------------------------------------------------------------------// //Drawing general buttons canvas.drawRect(right - 80, bottom - 90, right, bottom - 10, paintWhite); canvas.drawRect(right - 70, bottom - 55, right - 10, bottom - 20, paintBlack); canvas.drawRect(left, bottom - 90, left + 80, bottom - 10, paintWhite); a.set(right - 70, bottom - 55); b.set(right - 10, bottom - 55); c.set(right - 40, bottom - 80); path.moveTo(a.x, a.y); path.lineTo(b.x, b.y); path.lineTo(c.x, c.y); path.lineTo(a.x, a.y); path.close(); canvas.drawPath(path, paintBlack); paintBlack.setStyle(Paint.Style.STROKE); canvas.drawRect(left + 30, bottom - 40, left + 60, bottom - 40, paintBlack); oval.set(left + 50, bottom - 60, left + 70, bottom - 40); canvas.drawArc(oval, 270, 180, true, paintBlack); canvas.drawRect(left + 10, bottom - 60, left + 60, bottom - 60, paintBlack); //canvas.drawRect(left+10, bottom-70, left+20, bottom-60, paintBlack); //canvas.drawRect(left+10, bottom-60, left+20, bottom-50, paintBlack); a.set(left + 20, bottom - 50); b.set(left + 10, bottom - 60); c.set(left + 20, bottom - 70); path.moveTo(a.x, a.y); path.lineTo(b.x, b.y); path.moveTo(b.x, b.y); path.lineTo(c.x, c.y); canvas.drawPath(path, paintBlack); paintBlack.setStyle(Paint.Style.FILL_AND_STROKE); }
From source file:org.stockchart.stickers.FibonacciSticker.java
@Override protected void draw(SeriesPaintInfo info, PointF p1, PointF p2, Canvas c) { PointF rightMost = Utils.getRightmost(p1, p2); PointF leftMost = Utils.getLeftmost(p1, p2); boolean isRising = Utils.isRising(p1, p2); float maxY = Math.max(p1.y, p2.y); float minY = Math.min(p1.y, p2.y); float height = maxY - minY; float originY = isRising ? minY : maxY; float m = fAppearance.getOutlineWidth(); // guideline// w w w .j a v a 2s.c o m // fAppearance.applyOutline(fPaint); // PaintUtils.drawLine(c, p1.x, p1.y, p2.x, p2.y, fPaint); switch (fType) { case FANS: { float r382 = originY + height * 0.382f * (isRising ? 1f : -1f); float r500 = originY + height * 0.5f * (isRising ? 1f : -1f); float r618 = originY + height * 0.618f * (isRising ? 1f : -1f); fAppearance.applyOutline(fPaint); float rightMostX = Math.max(rightMost.x, c.getWidth() + 1f); PaintUtils.drawLine(c, leftMost.x, leftMost.y, rightMostX, Utils.getY(leftMost.x, leftMost.y, rightMost.x, r382, rightMostX), fPaint); PaintUtils.drawLine(c, leftMost.x, leftMost.y, rightMostX, Utils.getY(leftMost.x, leftMost.y, rightMost.x, r500, rightMostX), fPaint); PaintUtils.drawLine(c, leftMost.x, leftMost.y, rightMostX, Utils.getY(leftMost.x, leftMost.y, rightMost.x, r618, rightMostX), fPaint); } break; case RETRACEMENT: { Set<Entry<Float, String>> entrySet = RETRACEMENTS.entrySet(); Iterator<Entry<Float, String>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Entry<Float, String> i = iterator.next(); float y = originY + height * i.getKey() * (isRising ? 1f : -1f); fAppearance.applyOutline(fPaint); PaintUtils.drawLine(c, 0, y, c.getWidth(), y, fPaint); fAppearance.applyText(fPaint); c.drawText(i.getValue(), 1f, y - m, fPaint); } } break; case ARCS: { float length = (float) Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); // euclidean distance float r382 = length * 0.382f; float r500 = length * 0.5f; float r618 = length * 0.618f; fAppearance.applyOutline(fPaint); int a2 = isRising ? 180 : -180; fTempRectF.set(rightMost.x - r500, rightMost.y - r500, rightMost.x + r500, rightMost.y + r500); c.drawArc(fTempRectF, 0, a2, false, fPaint); fTempRectF.set(rightMost.x - r382, rightMost.y - r382, rightMost.x + r382, rightMost.y + r382); c.drawArc(fTempRectF, 0, a2, false, fPaint); fTempRectF.set(rightMost.x - r618, rightMost.y - r618, rightMost.x + r618, rightMost.y + r618); c.drawArc(fTempRectF, 0, a2, false, fPaint); } break; } }
From source file:devlight.io.library.ArcProgressStackView.java
@Override protected void onDraw(final Canvas canvas) { super.onDraw(canvas); // Save and rotate to start angle canvas.save();/* ww w . j av a 2s . co m*/ final float radius = mSize * 0.5F; canvas.rotate(mStartAngle, radius, radius); // Draw all of progress for (int i = 0; i < mModels.size(); i++) { final Model model = mModels.get(i); // Get progress for current model float progressFraction = mIsAnimated && !isInEditMode() ? (model.mLastProgress + (mAnimatedFraction * (model.getProgress() - model.mLastProgress))) / MAX_PROGRESS : model.getProgress() / MAX_PROGRESS; if (i != mActionMoveModelIndex && mActionMoveModelIndex != ANIMATE_ALL_INDEX) progressFraction = model.getProgress() / MAX_PROGRESS; final float progress = progressFraction * mSweepAngle; // Check if model have gradient final boolean isGradient = model.getColors() != null; // Set width of progress mProgressPaint.setStrokeWidth(mProgressModelSize); // Set model arc progress model.mPath.reset(); model.mPath.addArc(model.mBounds, 0.0F, progress); // Draw gradient progress or solid resetShadowLayer(); mProgressPaint.setShader(null); mProgressPaint.setStyle(Paint.Style.STROKE); if (mIsModelBgEnabled) { //noinspection ResourceAsColor mProgressPaint.setColor(isInEditMode() ? mPreviewModelBgColor : model.getBgColor()); canvas.drawArc(model.mBounds, 0.0F, mSweepAngle, false, mProgressPaint); if (!isInEditMode()) mProgressPaint.clearShadowLayer(); } // Check if gradient for draw shadow at first and then gradient progress if (isGradient) { if (!mIsModelBgEnabled) { canvas.drawPath(model.mPath, mProgressPaint); if (!isInEditMode()) mProgressPaint.clearShadowLayer(); } mProgressPaint.setShader(model.mSweepGradient); } else mProgressPaint.setColor(model.getColor()); // Here we draw main progress mProgressPaint.setAlpha(255); canvas.drawPath(model.mPath, mProgressPaint); // Preview mode if (isInEditMode()) continue; // Get model title bounds mTextPaint.setTextSize(mProgressModelSize * 0.5F); mTextPaint.getTextBounds(model.getTitle(), 0, model.getTitle().length(), model.mTextBounds); // Draw title at start with offset final float titleHorizontalOffset = model.mTextBounds.height() * 0.5F; final float progressLength = (float) (Math.PI / 180.0F) * progress * model.mBounds.width() * 0.5F; final String title = (String) TextUtils.ellipsize(model.getTitle(), mTextPaint, progressLength - titleHorizontalOffset * 2, TextUtils.TruncateAt.END); canvas.drawTextOnPath(title, model.mPath, mIsRounded ? 0.0F : titleHorizontalOffset, titleHorizontalOffset, mTextPaint); // Get pos and tan at final path point model.mPathMeasure.setPath(model.mPath, false); model.mPathMeasure.getPosTan(model.mPathMeasure.getLength(), model.mPos, model.mTan); // Get title width final float titleWidth = model.mTextBounds.width(); // Create model progress like : 23% final String percentProgress = String.format("%d%%", (int) model.getProgress()); // Get progress text bounds mTextPaint.setTextSize(mProgressModelSize * 0.35f); mTextPaint.getTextBounds(percentProgress, 0, percentProgress.length(), model.mTextBounds); // Get pos tan with end point offset and check whether the rounded corners for offset final float progressHorizontalOffset = mIndicatorOrientation == IndicatorOrientation.VERTICAL ? model.mTextBounds.height() * 0.5F : model.mTextBounds.width() * 0.5F; final float indicatorProgressOffset = (mIsRounded ? progressFraction : 1.0F) * (-progressHorizontalOffset - titleHorizontalOffset - (mIsRounded ? model.mTextBounds.height() * 2.0F : 0.0F)); model.mPathMeasure.getPosTan(model.mPathMeasure.getLength() + indicatorProgressOffset, model.mPos, mIndicatorOrientation == IndicatorOrientation.VERTICAL && !mIsRounded ? new float[2] : model.mTan); // Check if there available place for indicator if ((titleWidth + model.mTextBounds.height() + titleHorizontalOffset * 2.0F) - indicatorProgressOffset < progressLength) { // Get rotate indicator progress angle for progress value float indicatorProgressAngle = (float) (Math.atan2(model.mTan[1], model.mTan[0]) * (180.0F / Math.PI)); // Get arc angle of progress indicator final float indicatorLengthProgressAngle = ((progressLength + indicatorProgressOffset) / (model.mBounds.width() * 0.5F)) * (float) (180.0F / Math.PI); // Detect progress indicator position : left or right and then rotate if (mIndicatorOrientation == IndicatorOrientation.VERTICAL) { // Get X point of arc angle progress indicator final float x = (float) (model.mBounds.width() * 0.5F * (Math.cos((indicatorLengthProgressAngle + mStartAngle) * Math.PI / 180.0F))) + model.mBounds.centerX(); indicatorProgressAngle += (x > radius) ? -90.0F : 90.0F; } else { // Get Y point of arc angle progress indicator final float y = (float) (model.mBounds.height() * 0.5F * (Math.sin((indicatorLengthProgressAngle + mStartAngle) * Math.PI / 180.0F))) + model.mBounds.centerY(); indicatorProgressAngle += (y > radius) ? 180.0F : 0.0F; } // Draw progress value canvas.save(); canvas.rotate(indicatorProgressAngle, model.mPos[0], model.mPos[1]); canvas.drawText(percentProgress, model.mPos[0] - model.mTextBounds.exactCenterX(), model.mPos[1] - model.mTextBounds.exactCenterY(), mTextPaint); canvas.restore(); } // Check if gradient and have rounded corners, because we must to create elevation effect // for start progress corner if ((isGradient || mIsLeveled) && mIsRounded && progress != 0) { model.mPathMeasure.getPosTan(0.0F, model.mPos, model.mTan); // Set paint for overlay rounded gradient with shadow setLevelShadowLayer(); //noinspection ResourceAsColor mLevelPaint.setColor(isGradient ? model.getColors()[0] : model.getColor()); // Get bounds of start pump final float halfSize = mProgressModelSize * 0.5F; final RectF arcRect = new RectF(model.mPos[0] - halfSize, model.mPos[1] - halfSize, model.mPos[0] + halfSize, model.mPos[1] + halfSize + 2.0F); canvas.drawArc(arcRect, 0.0F, -180.0F, true, mLevelPaint); } } // Restore after drawing canvas.restore(); }
From source file:com.appeaser.sublimepickerlibrary.datepicker.SimpleMonthView.java
/** * Draws the month days./* w ww .j a va2 s. c o m*/ */ @SuppressWarnings("ConstantConditions") private void drawDays(Canvas canvas) { final TextPaint p = mDayPaint; final int headerHeight = mMonthHeight + mDayOfWeekHeight; //final int rowHeight = mDayHeight; final float rowHeight = mDayHeight; //final int colWidth = mCellWidth; final float colWidth = mCellWidth; // Text is vertically centered within the row height. final float halfLineHeight = (p.ascent() + p.descent()) / 2f; //int rowCenter = headerHeight + rowHeight / 2; float rowCenter = headerHeight + rowHeight / 2f; for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) { //final int colCenter = colWidth * col + colWidth / 2; final float colCenter = colWidth * col + colWidth / 2f; //final int colCenterRtl; final float colCenterRtl; if (SUtils.isLayoutRtlCompat(this)) { colCenterRtl = mPaddedWidth - colCenter; } else { colCenterRtl = colCenter; } int stateMask = 0; final boolean isDayEnabled = isDayEnabled(day); if (isDayEnabled) { stateMask |= SUtils.STATE_ENABLED; } final boolean isDayInActivatedRange = mActivatedDays.isValid() && mActivatedDays.isActivated(day); final boolean isSelected = mActivatedDays.isSelected(day); if (isSelected) { stateMask |= SUtils.STATE_ACTIVATED; canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint); } else if (isDayInActivatedRange) { stateMask |= SUtils.STATE_ACTIVATED; int bgShape = DRAW_RECT; if (mActivatedDays.isSingleDay()) { if (mActivatedDays.isStartOfMonth()) { bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT; } else { bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT; } } else if (mActivatedDays.isStartingDayOfRange(day)) { bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT; } else if (mActivatedDays.isEndingDayOfRange(day)) { bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT; } // Use height to constrain the protrusion of the arc boolean constrainProtrusion = colWidth > (rowHeight - (2 * mPaddingRangeIndicator)); float horDistFromCenter = constrainProtrusion ? rowHeight / 2f - mPaddingRangeIndicator : colWidth / 2f; switch (bgShape) { case DRAW_RECT_WITH_CURVE_ON_LEFT: int leftRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl - horDistFromCenter) + 1 : (int) (colCenterRtl - horDistFromCenter); int leftRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl + horDistFromCenter) + 1 : (int) (colCenterRtl + horDistFromCenter); RectF leftArcRect = new RectF(leftRectArcLeft, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, leftRectArcRight, rowCenter + rowHeight / 2f - mPaddingRangeIndicator); canvas.drawArc(leftArcRect, 90, 180, true, mDayRangeSelectorPaint); canvas.drawRect(new RectF(leftArcRect.centerX(), rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f, rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint); break; case DRAW_RECT_WITH_CURVE_ON_RIGHT: int rightRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl - horDistFromCenter) + 1 : (int) (colCenterRtl - horDistFromCenter); int rightRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl + horDistFromCenter) + 1 : (int) (colCenterRtl + horDistFromCenter); RectF rightArcRect = new RectF(rightRectArcLeft, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightRectArcRight, rowCenter + rowHeight / 2f - mPaddingRangeIndicator); canvas.drawArc(rightArcRect, 270, 180, true, mDayRangeSelectorPaint); canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightArcRect.centerX(), rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint); break; default: canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f, rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint); break; } } if (mTouchedItem == day) { stateMask |= SUtils.STATE_PRESSED; if (isDayEnabled) { canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDayHighlightPaint); } } final boolean isDayToday = mToday == day; final int dayTextColor; if (isDayToday && !isDayInActivatedRange) { dayTextColor = mDaySelectorPaint.getColor(); } else { final int[] stateSet = SUtils.resolveStateSet(stateMask); dayTextColor = mDayTextColor.getColorForState(stateSet, 0); } p.setColor(dayTextColor); canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p); col++; if (col == DAYS_IN_WEEK) { col = 0; rowCenter += rowHeight; } } }
From source file:com.miuhouse.yourcompany.student.view.widget.date.datepicker.SimpleMonthView.java
/** * Draws the month days.//from www . ja va 2s . c o m */ @SuppressWarnings("ConstantConditions") private void drawDays(Canvas canvas) { final TextPaint p = mDayPaint; final int headerHeight = mMonthHeight + mDayOfWeekHeight; final float rowHeight = mDayHeight; final float colWidth = mCellWidth; // Text is vertically centered within the row height. final float halfLineHeight = (p.ascent() + p.descent()) / 2f; // int rowCenter = headerHeight + rowHeight / 2; float rowCenter = headerHeight + rowHeight / 2f; //mDaysInMonth303129 for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) { final float colCenter = colWidth * col + colWidth / 2f; //final int colCenterRtl; final float colCenterRtl; if (SUtils.isLayoutRtlCompat(this)) { colCenterRtl = mPaddedWidth - colCenter; } else { colCenterRtl = colCenter; } int stateMask = 0; final boolean isDayEnabled = isDayEnabled(day); if (isDayEnabled) { stateMask |= SUtils.STATE_ENABLED; } final boolean isDayInActivatedRange = mActivatedDays.isValid() && mActivatedDays.isActivated(day); final boolean isSelected = mActivatedDays.isSelected(day); final boolean isSelectedAndTogether = mActivatedDays.isSelectedAndTogether(day); if (isSelectedAndTogether) { if ((!disable && !isDisable) || isFill) { stateMask |= SUtils.STATE_ACTIVATED; canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint); } } if (!isSelected && isDayInActivatedRange) { stateMask |= SUtils.STATE_ACTIVATED; int bgShape = DRAW_RECT; if (mActivatedDays.isSingleDay()) { if (mActivatedDays.isStartOfMonth()) { bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT; } else { bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT; } } else if (mActivatedDays.isStartingDayOfRange(day)) { bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT; } else if (mActivatedDays.isEndingDayOfRange(day)) { bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT; } // Use height to constrain the protrusion of the arc boolean constrainProtrusion = colWidth > (rowHeight - (2 * mPaddingRangeIndicator)); float horDistFromCenter = constrainProtrusion ? rowHeight / 2f - mPaddingRangeIndicator : colWidth / 2f; switch (bgShape) { case DRAW_RECT_WITH_CURVE_ON_LEFT: int leftRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl - horDistFromCenter) + 1 : (int) (colCenterRtl - horDistFromCenter); int leftRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl + horDistFromCenter) + 1 : (int) (colCenterRtl + horDistFromCenter); RectF leftArcRect = new RectF(leftRectArcLeft, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, leftRectArcRight, rowCenter + rowHeight / 2f - mPaddingRangeIndicator); canvas.drawArc(leftArcRect, 90, 180, true, mDayRangeSelectorPaint); canvas.drawRect(new RectF(leftArcRect.centerX(), rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f, rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint); break; case DRAW_RECT_WITH_CURVE_ON_RIGHT: int rightRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl - horDistFromCenter) + 1 : (int) (colCenterRtl - horDistFromCenter); int rightRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1 ? (int) (colCenterRtl + horDistFromCenter) + 1 : (int) (colCenterRtl + horDistFromCenter); RectF rightArcRect = new RectF(rightRectArcLeft, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightRectArcRight, rowCenter + rowHeight / 2f - mPaddingRangeIndicator); canvas.drawArc(rightArcRect, 270, 180, true, mDayRangeSelectorPaint); canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightArcRect.centerX(), rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint); break; default: canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f, rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f, rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint); break; } } final boolean isDayToday = mToday == day; final int dayTextColor; if (isDayToday) { if (mActivatedDays.isClick && isSelected) { final int[] stateSet = SUtils.resolveStateSet(stateMask); dayTextColor = mDayTextColor.getColorForState(stateSet, 0); } else { dayTextColor = mDaySelectorPaint.getColor(); } } else { final int[] stateSet = SUtils.resolveStateSet(stateMask); if (!mActivatedDays.isSingleDay()) { if (stateMask == 1) { dayTextColor = mContext.getResources().getColor(R.color.textDarkfour); } else { dayTextColor = mDayTextColor.getColorForState(stateSet, 0); } } else { dayTextColor = mDayTextColor.getColorForState(stateSet, 0); } } p.setColor(dayTextColor); canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p); col++; if (col == DAYS_IN_WEEK) { col = 0; rowCenter += rowHeight; } if (mDaysInMonth == day) mOnRangeClickListener.onRangeSelected(calendars, position); } }
From source file:de.hs_bremen.aurora_hunter.ui.views.PredictionGraphView.java
public void onDraw(Canvas canvas) { if (mPoints.size() == 0) { return;//from w w w. j a va2 s . c o m } int yOffsetForLabels = 0; if (mMax * mScaleFactor > 0.8) { yOffsetForLabels = 15; } Path path = new Path(); int height = canvas.getHeight(); int width = canvas.getWidth(); for (Point point : mPoints) { point.x = point.percentageX * width + 60; // Log.i("mScaleFactor", " : " + mScaleFactor); //Log.i("percent", " : " + percent); point.y = (float) ((1 - point.percentageY * mScaleFactor) * height) + yOffsetForLabels * 3; } if (mPoints.size() > 1) { //calcuate x/y based on size of canvas for (int i = 0; i < mPoints.size(); i++) { if (i >= 0) { Point point = mPoints.get(i); // Log.i("dx",point.x + " - " + point.y ); if (i == 0) { Point next = mPoints.get(i + 1); point.dx = ((next.x - point.x) / 5); point.dy = ((next.y - point.y) / 5); } else if (i == mPoints.size() - 1) { Point prev = mPoints.get(i - 1); point.dx = ((point.x - prev.x) / 5); point.dy = ((point.y - prev.y) / 5); } else { Point next = mPoints.get(i + 1); Point prev = mPoints.get(i - 1); point.dx = ((next.x - prev.x) / 5); point.dy = ((next.y - prev.y) / 5); } } } } if (mPoints.size() > 0) { path.moveTo(0, (float) (canvas.getHeight() * 0.8)); path.lineTo(0, mPoints.get(0).y); } boolean first = true; for (int i = 0; i < mPoints.size(); i++) { Point point = mPoints.get(i); if (first) { first = false; path.cubicTo(point.x - point.x * 2, point.y - point.y / 5, point.x - point.dx, point.y - point.dy, point.x, point.y); } else { Point prev = mPoints.get(i - 1); // Log.i("Draw", point.dx + " " + point.dy); path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y); } } if (mPoints.size() > 0) { path.lineTo(width, mPoints.get(mPoints.size() - 1).y); } path.lineTo(width, height); path.lineTo(0, height); canvas.drawPath(path, mGraphPaint); int detlaY = 30; for (Point p : mPoints) { int val = (int) Math.round(p.percentageY * 100); //if last element if (mPoints.indexOf(p) == mPoints.size() - 1) { //Log.i("last", p.toString()); if (val == 0 || p.y > getHeight()) { canvas.drawText(val + "%", p.x - 150, getHeight() - detlaY, mTextPaint); } else { canvas.drawText(val + "%", p.x - 150, p.y - detlaY + yOffsetForLabels, mTextPaint); } } else { if (val == 0 || p.y > getHeight()) { canvas.drawText(val + "%", p.x - 20, getHeight() - detlaY, mTextPaint); } else { canvas.drawText(val + "%", p.x - 20, p.y - detlaY + yOffsetForLabels, mTextPaint); } } //Log.i("point", p.toString()); } // Log.i("Lenght", mPoints.size() + " "); float levelStart = (float) (canvas.getHeight() - (mNotificationLevel * canvas.getHeight())); float lineHeight = DpToPixelUtil.convertDpToPixel(2, getContext()); if (mViewMode == PredictionFragment.NOTIFICATION_MODE.SET_NOTIFICATION_LEVEL) { mNotificationLevelPaint.setAlpha(150); float leftOffset = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { float arcHeight = DpToPixelUtil.convertDpToPixel(30, getContext()); leftOffset = arcHeight; RectF rectF = new RectF(getWidth() - arcHeight, levelStart - arcHeight / 2, getWidth(), levelStart + arcHeight / 2); canvas.drawArc(rectF, 0, 360, true, mNotificationLevelPaint); } canvas.drawRect(0, levelStart, canvas.getWidth() - leftOffset, levelStart + lineHeight, mNotificationLevelPaint); mNotificationLevelPaint.setAlpha(20); canvas.drawRect(0, levelStart + lineHeight, canvas.getWidth(), canvas.getHeight(), mNotificationLevelPaint); String text = Math.round(mNotificationLevel * 100) + "%"; canvas.drawText(text, DpToPixelUtil.convertDpToPixel(10, getContext()), levelStart + DpToPixelUtil.convertDpToPixel(25, getContext()), mNotificationLevelTextPaint); } else { mNotificationLevelPaint.setAlpha(30); canvas.drawRect(0, levelStart, canvas.getWidth(), levelStart + lineHeight, mNotificationLevelPaint); } }