Skip to content

0. Get Started

Anas Altair edited this page Feb 24, 2020 · 16 revisions

to start work with SpeedView, add the Library to dependencies to download it, see how you can do that in SpeedView - Download.

first add one of Gauges to your xml Layout "choose one of gauges Here" like so:

<com.github.anastr.speedviewlib.SpeedView
        android:id="@+id/speedometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

All Speedometers extends Gauge class ⚡.
define your Gauge object from your Activity, Fragment or anywhere by simple line:

val speedometer = findViewById<SpeedView>(R.id.speedometer)

or maybe you want to create programmatically and add it to layout:

// AwesomeSpeedometer  example:
val speedometer = AwesomeSpeedometer (context)

// define ViewGroup, and add AwesomeSpeedometer to it.
val layout = findViewById<LinearLayout>(R.id.linear_layout)
layout.add(speedometer)

Change Speed

smoothly change speed value is very Easy ! by simple line:

// move to 50 Km/s
speedometer.speedTo(50)
// or move to 50 Km/s with Duration = 4 sec
speedometer.speedTo(50, 4000)

Or maybe you want to change speed to percent value, for example lets move it to 30% by this method :

// move indicator to percent value (30%)
speedometer.speedPercentTo(30)

also speedPercentTo() have Duration parameter speedPercentTo(100, 5000).

Unit

unit text, the text after speed text, default value = "Km/h".
to change unit you can use sv_unit attribute in XML or in code:

speedometer.unit = "YOUR_UNIT"

min and max speed

main and max value, the number in speedTo(int) method should be between [sv_minSpeed, sv_maxSpeed] if you put number bigger than sv_maxSpeed speed will move to sv_maxSpeed..., by default sv_minSpeed=0 and sv_maxSpeed=100, you can change these values simply in XML:

app:sv_minSpeed="50"
app:sv_maxSpeed="125"

or in code:

speedometer.minSpeed = -30
speedometer.maxSpeed = 30

as you can see, Values can be Negative.

get Speed

you can get the speed any time by many ways, call speedometer.currentSpeed or speedometer.currentIntSpeed to know what current speed is now, or use the speedometer.speed to get last speed which you set by speedTo(), speedPercentTo() methods or if you stop speedometer By stop() method.

// use 'currentSpeed' (recommend)
Log.d("speed know", gauge.currentSpeed.toString())
// to handle speed value, you can use 'currentIntSpeed' safely method.

Speed Listener

we have OnSpeedChangeListener: A callback that notifies clients when the speed has been changed (just when speed change in integer), it is safe callback pass on all speed values when use currentIntSpeed.
example:

speedometer.onSpeedChangeListener = { gauge: Gauge, isSpeedUp: Boolean, isByTremble: Boolean ->
    val speed = gauge.currentIntSpeed
    Log.d("Speedometer Listener", speed.toString())
}

it have three parameter:

  • Gauge: the gauge who change.
  • isSpeedUp: if speed increase.
  • isByTremble: true if speed has changed by Tremble.

and OnSectionChangeListener: A callback that notifies clients when the the indicator move to new section, what is sections ?.
example:

speedometer.onSectionChangeListener = sv@ { previousSection: Section?, newSection: Section? ->
    if (newSection == null)
        // there is no section here!
        return@sv
    if (newSection.endOffset== 1f)
        Log.d("Speedometer Listener", "you are on highest speed!!")
}

also speedTextListener to print custom speed value:

speedometer.speedTextListener = { speed: Float -> ".3f".format(speed) }

Tremble

Tremble is an option to increases and decreases speed value around current speed, which means that indicator (in Speedometer family) will move to add some reality to speedometer.
to stop it, just use app:sv_withTremble="false" Attribute, or speedometer.withTremble = false in code.
you can also change trembleDegree and trembleDuration.

trembleDegree:

  • sv_trembleDegree="6"
  • a degree to increases and decreases the indicator around correct speed.
  • it shouldn't be between negative, or it will throw IllegalArgumentException.
  • the unit is speed unit, which means speed value.
  • default : 4.

trembleDuration:

  • sv_trembleDuration="3000"
  • tremble Animation duration in millisecond.
  • it shouldn't be between negative, or it will throw IllegalArgumentException.
  • default : 1000 millisecond.

you can change tremble data by methods in code, or one method speedometer.setTrembleData(trembleDegree,trembleDuration).



Go to Style page to make speedometer more beautiful.
now you can jump to Advanced Usage.