Skip to content

0. Get Started

Anas Altair edited this page Jul 19, 2017 · 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="250dp"
        android:layout_height="wrap_content" />

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

Gauge gauge = (Gauge) 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);

Change Speed

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

// move to 50 Km/s
gauge.speedTo(50);
// or move to 50 Km/s with Duration = 4 sec
gauge.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%)
gauge.speedPercentTo(30);

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

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:

gauge.setMinSpeed(-30);
gauge.setMaxSpeed(30);

as you can see, Values can be Negative.

get Speed

you can get the speed any time by many ways, use getCurrentSpeed() or getCurrentIntSpeed() methods to know what current speed is 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 getCurrentSpeed() method (recommend)
Log.e("speed know", gauge.getCurrentSpeed()+"");
// to handle speed value, you can use getCurrentIntSpeed() 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 getCurrentIntSpeed().
example:

gauge.setOnSpeedChangeListener(new OnSpeedChangeListener() {
    @Override
    public void onSpeedChange(Gauge gauge, boolean isSpeedUp, boolean isByTremble) {
        int speed = gauge.getCurrentIntSpeed();
        Log.e("Speedometer Listener", speed + "");
    }
});

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:

gauge.setOnSectionChangeListener(new OnSectionChangeListener() {
    @Override
    public void onSectionChangeListener(byte oldSection, byte newSection) {
        if (newSection == Speedometer.HIGH_SECTION)
            Toast.makeText(getApplicationContext(), "Slow Down !!", Toast.LENGTH_LONG).show();
    }
});

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.setWithTremble(false); method.
you can also change trembleDegree and trembleDuration by their methods or setTrembleData () method.



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