Skip to content

0. Get Started

Anas Altair edited this page Dec 23, 2016 · 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 Speedometers to your xml Layout like so:

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

All Speedometers extends Speedometer class ⚡.
So, we can define any Speedometer object from your Activity, Fragment or anywhere by simple line:

Speedometer speedometer = (Speedometer) findViewById(R.id.speedometer);

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

// AwesomeSpeedometer  example:
AwesomeSpeedometer speedometer = new AwesomeSpeedometer (this);

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

Move the Indicator

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 don't know the speed to move indicator, so you can move indicator to percent value, for example lets move it to 30% by this method :

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

get Speed

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

// use getCorrectSpeed() method (recommend)
Log.e("speed know", speedometer.getCorrectSpeed()+"");

Speed Listener

we have OnSpeedChangeListener: A callback that notifies clients when the speed has been changed (just when speed change in integer).
example:

speedometer.setOnSpeedChangeListener(new OnSpeedChangeListener() {
    @Override
    public void onSpeedChange(Speedometer speedometer, boolean isSpeedUp, boolean isByTremble) {
        Log.e("Speedometer Listener", speedometer.getCorrectSpeed()+"");
    }
});

it have three parameter:

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

Tremble

Tremble is an option to increases and decreases speed value around correct speed, which means that indicator will move to add some reality to speedometer.
to stop it, just use app:withTremble="false" Attribute, or speedometer.setWithTremble(false); method.
you can also change trembleDegree and trembleDuration by their methods or setTrembleData () method.



now you can jump to Advanced Usage.