简体   繁体   中英

Rotating speedometer needle gauge around its center as the speed changes

车速表

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");
        }
    }
}

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);

                  }
             }          

    }

  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM