Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing the position of ray start speed #248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ open class RaySpeedometer @JvmOverloads constructor(
private var withEffects = true

private var degreeBetweenMark = 5
var rayStartSpeed: Float? = null
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value the ray will be drawn from. This allows the ray to be drawn backwards. null preserves current functionality and draws from startSpeed.


var isWithEffects: Boolean
get() = withEffects
Expand Down Expand Up @@ -104,12 +105,21 @@ open class RaySpeedometer @JvmOverloads constructor(
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.RaySpeedometer, 0, 0)

rayPaint.color = a.getColor(R.styleable.RaySpeedometer_sv_rayColor, rayPaint.color)
val degreeBetweenMark = a.getInt(R.styleable.RaySpeedometer_sv_degreeBetweenMark, this.degreeBetweenMark)
val rayMarkWidth = a.getDimension(R.styleable.RaySpeedometer_sv_rayMarkWidth, rayMarkPaint.strokeWidth)
val degreeBetweenMark =
a.getInt(R.styleable.RaySpeedometer_sv_degreeBetweenMark, this.degreeBetweenMark)
val rayMarkWidth =
a.getDimension(R.styleable.RaySpeedometer_sv_rayMarkWidth, rayMarkPaint.strokeWidth)
rayMarkPaint.strokeWidth = rayMarkWidth
activeMarkPaint.strokeWidth = rayMarkWidth
speedBackgroundPaint.color = a.getColor(R.styleable.RaySpeedometer_sv_speedBackgroundColor, speedBackgroundPaint.color)
speedBackgroundPaint.color = a.getColor(
R.styleable.RaySpeedometer_sv_speedBackgroundColor,
speedBackgroundPaint.color
)
withEffects = a.getBoolean(R.styleable.RaySpeedometer_sv_withEffects, withEffects)
rayStartSpeed =
a.getFloat(R.styleable.RaySpeedometer_sv_rayStartSpeed, Float.NEGATIVE_INFINITY).let {
if (it == Float.NEGATIVE_INFINITY) null else it
Copy link
Author

@agronick agronick Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of hacky but I couldn't find a good way in the attributes API to allow null values.

}
a.recycle()
isWithEffects = withEffects
if (degreeBetweenMark in 1..20)
Expand Down Expand Up @@ -139,27 +149,35 @@ open class RaySpeedometer @JvmOverloads constructor(
updateBackgroundBitmap()
}

open fun getRayStartDegree(): Float {
return getDegreeAtSpeed(rayStartSpeed ?: minSpeed)
}

override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

canvas.save()
canvas.rotate(getStartDegree() + 90f, size * .5f, size * .5f)
var i = getStartDegree()
while (i < getEndDegree()) {
if (degree <= i) {
rayMarkPaint.color = markColor
canvas.drawPath(markPath, rayMarkPaint)
canvas.rotate(degreeBetweenMark.toFloat(), size * .5f, size * .5f)
i += degreeBetweenMark
continue

val zeroDegree = getRayStartDegree()
val rangeCheck = if (degree <= zeroDegree) {
degree.toInt()..zeroDegree.toInt()
} else {
zeroDegree.toInt()..degree.toInt()
}

for (i in getStartDegree()..getEndDegree() step degreeBetweenMark) {
val drawWith = if (!rangeCheck.contains(i)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part I tried to clean up. These lines seemed to be present in both parts of the conditional

canvas.drawPath(markPath, rayMarkPaint)
canvas.rotate(degreeBetweenMark.toFloat(), size * .5f, size * .5f)
i += degreeBetweenMark

The only thing that really looked to be changing was the paint.

rayMarkPaint.apply {
color = markColor
}
} else {
activeMarkPaint.apply {
color = currentSection?.color ?: 0
}
}
if (currentSection != null)
activeMarkPaint.color = currentSection!!.color
else
activeMarkPaint.color = 0 // transparent color
canvas.drawPath(markPath, activeMarkPaint)
canvas.rotate(degreeBetweenMark.toFloat(), size * .5f, size / 2f)
i += degreeBetweenMark
canvas.drawPath(markPath, drawWith)
canvas.rotate(degreeBetweenMark.toFloat(), size * .5f, size * .5f)
}
canvas.restore()

Expand Down
1 change: 1 addition & 0 deletions speedviewlib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<attr name="sv_rayColor" format="color"/>
<attr name="sv_degreeBetweenMark" format="integer"/>
<attr name="sv_rayMarkWidth" format="dimension"/>
<attr name="sv_rayStartSpeed" format="float" />
</declare-styleable>

<declare-styleable name="PointerSpeedometer" parent="Speedometer">
Expand Down