2012-08-01 49 views
0

speedometer旋轉速度計針規圍繞其中心隨着速度的改變

大家好,

我寫一個應用程序,我有一個豎置90度針速度計,我試圖旋轉圍繞其中心的針以每秒變化的速度(我正在以隨機從0變爲120的文本視圖中顯示速度)

我從遠程服務獲取速度並在文本視圖中顯示。

以及因速度變化而引起的車速錶針距應在其中心相應變化。我的意思是,如果速度是30,針應該在30度,而速度表等。

我的代碼不能正常工作,如何解決這個問題?

幫助總是感激,謝謝。

這裏是我的代碼:

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

*我的代碼無法正常工作正是*>它正在做什麼?你的錯誤是什麼? – 2012-08-01 06:39:47

+0

@安德斯·梅特尼克,沒有錯誤,但它無限制地逆時針旋轉90到45度 – Randroid 2012-08-01 06:42:09

+0

是的,這是有道理的,也似乎是你要求它在你的代碼中做什麼? – 2012-08-01 06:52:08

回答

2

試試這個代碼:

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

        } 
      }   

    } 

    }); 
+0

@IHaN JiTHiN,感謝代碼 – Randroid 2012-08-02 09:05:23

+0

@IHaN JiTHiN,我有一些錯誤可以ü迴應請..... – 2013-01-08 10:45:19

+0

@SubbaReddy PolamReddy,Sure – JiTHiN 2013-01-08 12:34:32