大家好,
我写了一个应用程序,我有一个速度表,针头垂直设置在90度,我试图围绕其中心旋转针,速度每隔一秒变化一次(我在文本中显示速度视图从0到120随机变化
我从远程服务获得速度并在textview中显示。
当速度改变时,车速表针规应相应地改变其中心位置。我的意思是如果速度是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 :(得分: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);
}
}
}
});