我制作的钟摆动画有一个图像,当手机倾斜到一定程度时,该图像应沿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);
}
}
问题是动画不是很流畅,只有在我摇动手机时才会发生。我希望它只是倾斜和背部光滑。
答案 0 :(得分:1)
我想你应该试试
rAnimation.setRepeatCount(rAnimation.RESTART);
答案 1 :(得分:0)
if(actualTime - lastUpdate&lt; 200)
这里你基本上允许5FPS。将其减少到50ms左右,以获得更流畅的动画效果。