Skip to content

Commit

Permalink
fix start and end of PointerSpeedometer.
Browse files Browse the repository at this point in the history
Signed-off-by: Anas Altair <anastr244@gmail.com>
  • Loading branch information
anastr committed May 10, 2020
1 parent c7ba3f7 commit 2f4615c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.AttributeSet
import com.github.anastr.speedviewlib.components.Section
import com.github.anastr.speedviewlib.components.indicators.Indicator
import com.github.anastr.speedviewlib.components.indicators.NormalSmallIndicator
import com.github.anastr.speedviewlib.util.getRoundAngle

/**
* this Library build By Anas Altair
Expand Down Expand Up @@ -174,11 +175,7 @@ open class DeluxeSpeedView @JvmOverloads constructor(context: Context, attrs: At
val startAngle = (getEndDegree() - getStartDegree()) * it.startOffset + getStartDegree()
val sweepAngle = (getEndDegree() - getStartDegree()) * it.endOffset - (startAngle - getStartDegree())
if (it.style == Section.Style.ROUND) {
// here we calculate the extra length when strokeCap = ROUND.
// A: Arc Length, the extra length that taken ny ROUND stroke in one side.
// D: Diameter of circle.
// round angle padding = A * 360 / ( D * PI )
val roundAngle = (it.width * .5f * 360 / (speedometerRect.width() * Math.PI)).toFloat()
val roundAngle = getRoundAngle(it.width, speedometerRect.width())
speedometerPaint.strokeCap = Paint.Cap.ROUND
c.drawArc(speedometerRect, startAngle + roundAngle, sweepAngle - roundAngle * 2f, false, speedometerPaint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import com.github.anastr.speedviewlib.components.indicators.SpindleIndicator
import com.github.anastr.speedviewlib.util.getRoundAngle

/**
* this Library build By Anas Altair
Expand Down Expand Up @@ -135,7 +136,9 @@ open class PointerSpeedometer @JvmOverloads constructor(context: Context, attrs:
super.onDraw(canvas)
initDraw()

canvas.drawArc(speedometerRect, getStartDegree().toFloat(), (getEndDegree() - getStartDegree()).toFloat(), false, speedometerPaint)
val roundAngle = getRoundAngle(speedometerWidth, speedometerRect.width())
canvas.drawArc(speedometerRect, getStartDegree() + roundAngle
, (getEndDegree() - getStartDegree()) - roundAngle * 2f, false, speedometerPaint)

if (withPointer) {
canvas.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.RectF
import android.util.AttributeSet
import com.github.anastr.speedviewlib.components.Section
import com.github.anastr.speedviewlib.components.indicators.NormalIndicator
import com.github.anastr.speedviewlib.util.getRoundAngle

/**
* this Library build By Anas Altair
Expand Down Expand Up @@ -113,11 +114,7 @@ open class SpeedView @JvmOverloads constructor(context: Context, attrs: Attribut
val startAngle = (getEndDegree() - getStartDegree()) * it.startOffset + getStartDegree()
val sweepAngle = (getEndDegree() - getStartDegree()) * it.endOffset - (startAngle - getStartDegree())
if (it.style == Section.Style.ROUND) {
// here we calculate the extra length when strokeCap = ROUND.
// A: Arc Length, the extra length that taken ny ROUND stroke in one side.
// D: Diameter of circle.
// round angle padding = A * 360 / ( D * PI )
val roundAngle = (it.width * .5f * 360 / (speedometerRect.width() * Math.PI)).toFloat()
val roundAngle = getRoundAngle(it.width, speedometerRect.width())
speedometerPaint.strokeCap = Paint.Cap.ROUND
c.drawArc(speedometerRect, startAngle + roundAngle, sweepAngle - roundAngle * 2f, false, speedometerPaint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ fun Gauge.doOnSections(action: (section: Section) -> Unit) {
this.clearSections()
sections.forEach { action.invoke(it) }
this.addSections(sections)
}

/**
* here we calculate the extra length when strokeCap = ROUND.
* round angle padding = A * 360 / ( D * PI )
* @param [a] Arc Length, the extra length that taken ny ROUND stroke in one side.
* @param [d] Diameter of circle.
*/
fun getRoundAngle(a: Float, d: Float): Float {
return (a * .5f * 360 / (d * Math.PI)).toFloat()
}

0 comments on commit 2f4615c

Please sign in to comment.