0

speedometer

Hi all,

I have written an application where I have a speedometer with a needle vertically set at 90 degree and I am trying to rotate the needle around its center with the speed that changes for every one second(I am displaying the speed in a text view that changes randomly from 0 to 120)

I am getting the speed from a remote service and displaying in a textview.

so as the speed changes the speedometer needle gauge should change accordingly around its center. I mean If speed is 30 the needle should be at 30 and so on in speedometer.

My code is not working exactly, How to get around this problem?

Help is always appreciated,Thanks.

Here is my code:

pointer1 = (ImageView) findViewById(R.id.pointer1);

double degrees= speed;
double angle = degrees * 2 * Math.PI / 360.0;

for( speed=0;speed<120;speed++){
    RotateAnimation rAnimAntiClockWise = new RotateAnimation(180-0.0f, 180-speed,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    rAnimAntiClockWise.setInterpolator(new LinearInterpolator());
    rAnimAntiClockWise.setDuration(100);
    rAnimAntiClockWise.setFillAfter(true);
    rAnimAntiClockWise.setDuration(10000);
    rAnimAntiClockWise.setRepeatCount(-1);
    rAnimAntiClockWise.setRepeatMode(2);
    pointer1.startAnimation(rAnimAntiClockWise); 
}

private void invokeService() {
    if (conn == null) {

    } else {
        try {
        System.out.println(remoteService);

        int speed = remoteService.getSpeed();

        System.out.println("myspeed" + speed);

        TextView r = (TextView) findViewById(R.id.text2);
        r.setText("" + Integer.toString(speed));
            Log.d(getClass().getSimpleName(), "invokeService()");

        } catch (RemoteException re) {
        Log.e(getClass().getSimpleName(), "RemoteException");
        }
    }
}
Tim M.
  • 53,671
  • 14
  • 120
  • 163
Randroid
  • 3,688
  • 5
  • 30
  • 55
  • *My code is not working exactly* > What is it doing? What's your errors? – Anders Metnik Aug 01 '12 at 06:39
  • @ Anders Metnik, no errors, but its roating 90 to 45 degree in anticlock wise infinitely – Randroid Aug 01 '12 at 06:42
  • Yeah that makes sense, also seems to be what you are asking it to do in your code? – Anders Metnik Aug 01 '12 at 06:52
  • hmmm, but it should change according to the speed changes if the spped is 120 then the needle should be at 120, I have to set the angle of the needle gauge according to speed.as I am new to android I couldnt able to do it.I tried a lot but no result – Randroid Aug 01 '12 at 06:54
  • Hii Raghav did u check this example http://atstechlab.wordpress.com/2010/07/30/gauge-and-dial-widget-for-android/ – dharan Aug 01 '12 at 06:57
  • @dharan,Ya checked it but I didnt get the sample code and its quite different from my scenario I hope – Randroid Aug 01 '12 at 07:01
  • @Raghav looks quite similar in my eyes. But need more of your code to determine the functionality tbh. – Anders Metnik Aug 01 '12 at 07:11
  • @Anders Metnik, What I am saying is the needle is rotating irrespective of the speed,according to speed value, it should change its position.Hope u got my point. – Randroid Aug 01 '12 at 07:17
  • @Raghav..., are you looking for the code for deflecting the needle when the value in your `textview` changes? Also, what is the initial angle of the needle? – JiTHiN Aug 01 '12 at 10:31
  • yes u right, I am keeping the needle vertically at speed value 60 in speedometer – Randroid Aug 01 '12 at 12:11

1 Answers1

3

Try this code:

currentDegree=60;
speedDisplayTxt.addTextChangedListener(new TextWatcher()
    {

        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            fromDegrees= currentDegree;
            String speed=txt.getText().toString();
            toDegree= //convert speed to angle here.
            RotateAnimation NeedleDeflection = new RotateAnimation(fromDegrees, toDegrees, ...){
            protected void applyTransformation(float interpolatedTime,Transformation t) {
                float currentDegree = fromDegrees+(toDegrees-fromDegrees)*interpolatedTime;
                super.applyTransformation(interpolatedTime, t);
                };
            NeedleDeflection.setDuration(duration); 
            NeedleDeflection.setFillAfter(true);
            needle.startAnimation(NeedleDeflection);

                  }
             }          

    }

  });
JiTHiN
  • 6,548
  • 5
  • 43
  • 69