1. package com.example.demoandroid;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.view.MotionEvent;
6. import android.view.animation.Animation;
7. import android.view.animation.Animation.AnimationListener;
8. import android.view.animation.LinearInterpolator;
9. import android.view.animation.RotateAnimation;
10. import android.widget.Button;
11. import android.widget.Toast;
12.
13. public class MainActivity extends Activity {
14.
15. private Button bt;
16. private Animation an;
17.
18. @Override
19. protected void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.activity_main);
22. bt = (Button) findViewById(R.id.bt);
23.
24. new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
25. new LinearInterpolator());//不停顿
26. 1);//重复次数
27. true);//停在最后
28. 4000);
29. new AnimationListener() {
30. @Override
31. public void onAnimationStart(Animation animation) {
32. this, "开始了", 0).show();
33. }
34. @Override
35. public void onAnimationRepeat(Animation animation) {
36. this, "重复了", 0).show();
37. }
38. @Override
39. public void onAnimationEnd(Animation animation) {
40. this, "结束了", 0).show();
41. }
42. });
43. //动画开始
44. bt.startAnimation(an);
45. }
46.
47. @Override
48. public boolean onTouchEvent(MotionEvent event) {
49. switch (event.getAction()) {
50. case MotionEvent.ACTION_DOWN:
51. bt.clearAnimation();
52. break;
53. case MotionEvent.ACTION_UP:
54. bt.startAnimation(an);
55. break;
56.
57. default:
58. break;
59. }
60. return super.onTouchEvent(event);
61. }
62. }