我正在制作一个摆摆动画,它有一个图像,当手机倾斜到一定程度时,它应该沿着x轴移动,当手机保持直线时,它会回到原来的状态。
以下是我所做的:-
Main.Java
public class MainActivity extends Activity implements SensorEventListener
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgPend= (ImageView)findViewById(R.id.imgBell);
sensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
rAnimation= new RotateAnimation(0.0f, 30.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rAnimation.setFillAfter(true);
}
@Override
public void onSensorChanged(SensorEvent event)
{
// TODO Auto-generated method stub
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
getAccelerometer(event);
}
private void getAccelerometer(SensorEvent event)
{
// TODO Auto-generated method stub
float[] values = event.values;
float x = values[0];
float y = values[1];
// float z = values[2];
float accelationSquareRoot = (x * x + y * y )//+ z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 2) //
{
if (actualTime - lastUpdate < 200)
{
return;
}
lastUpdate = actualTime;
rAnimation= new RotateAnimation(0.0f, 30.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rAnimation.setFillAfter(true);
rAnimation.setRepeatMode(rAnimation.REVERSE);
imgPend.startAnimation(rAnimation);
}
}问题是动画不是很流畅,只有在我摇电话的时候才会发生。我希望它是光滑的,只要倾斜和后退。
发布于 2014-01-13 11:23:08
我觉得你应该试试
rAnimation.setRepeatCount(rAnimation.RESTART);发布于 2014-01-13 10:05:33
if (actualTime - lastUpdate < 200)
在这里,您基本上允许5 5FPS。减少到50毫秒左右,以获得更流畅的动画。
https://stackoverflow.com/questions/21087833
复制相似问题