Skip to content

0. Get Started

Anas Altair edited this page Jan 10, 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 Speedometers to your xml Layout "choose one of speedometer Here" 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

rotate indicator to correct value(speed), 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);

min and max speed

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

app:minSpeed="50"
app:maxSpeed="125"

or in code:

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

as you can see, Values can be Negative.

get Speed

you can get the speed any time by many ways, use getCorrectSpeed() or getCorrectIntSpeed() methods to know what correct 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 getCorrectSpeed() method (recommend)
Log.e("speed know", speedometer.getCorrectSpeed()+"");
// to handle speed value, you can use getCorrectIntSpeed() method.

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) {
        float speed = speedometer.getCorrectSpeed();
        Log.e("Speedometer Listener", speed + "");
    }
});

it have three parameter:

  • speedometer: the speedometer 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.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 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.



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