温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中Animation如何使用

发布时间:2021-06-26 15:04:39 来源:亿速云 阅读:144 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关Android中Animation如何使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

Android使用Animation方法一:在xml中定义动画:

Xml代码

  1. < ?xml version="1.0" encoding="utf-8"?>   

  2. < set xmlns:android=
    "http://schemas.android.com/apk/res/android">   

  3. < rotate   

  4. android:interpolator="@android:anim/accelerate_
    decelerate_interpolator"   

  5. android:fromDegrees="0"   

  6. android:toDegrees="+360"   

  7. android:duration="3000" />   

  8. < !-- rotate 旋转动画效果   

  9. 属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化   

  10. fromDegrees 属性为动画起始时物件的角度   

  11. toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针   

  12. duration 属性为动画持续时间,以毫秒为单位   

  13. -->   

  14. < /set>   

  15. < ?xml version="1.0" encoding="utf-8"?> 

  16. < set xmlns:android=
    "http://schemas.android.com/apk/res/android"> 

  17. < rotate   

  18. android:interpolator=
    "@android:anim/accelerate_decelerate_interpolator" 

  19. android:fromDegrees="0"   

  20. android:toDegrees="+360" 

  21. android:duration="3000" /> 

  22. < !-- rotate 旋转动画效果  

  23. 属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化  

  24. fromDegrees 属性为动画起始时物件的角度   

  25. toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针  

  26. duration 属性为动画持续时间,以毫秒为单位  

  27. --> 

  28. < /set> 

使用动画的Java代码,程序的效果是点击按钮,TextView旋转一周:

Java代码

  1. package com.ray.animation;   

  2. import android.app.Activity;   

  3. import android.os.Bundle;   

  4. import android.view.View;   

  5. import android.view.View.OnClickListener;   

  6. import android.view.animation.Animation;   

  7. import android.view.animation.AnimationUtils;   

  8. import android.widget.Button;   

  9. import android.widget.TextView;   

  10. public class TestAnimation extends Activity 
    implements OnClickListener{   

  11. public void onCreate(Bundle savedInstanceState) {   

  12. super.onCreate(savedInstanceState);   

  13. setContentView(R.layout.main);   

  14. Button btn = (Button)findViewById(R.id.Button01);   

  15. btn.setOnClickListener(this);   

  16. }   

  17. @Override   

  18. public void onClick(View v) {   

  19. Animation anim = AnimationUtils.loadAnimation(this, 
    R.anim.my_rotate_action);   

  20. findViewById(R.id.TextView01).startAnimation(anim);   

  21. }   

  22. }   

  23. package com.ray.animation;  

  24. import android.app.Activity;  

  25. import android.os.Bundle;  

  26. import android.view.View;  

  27. import android.view.View.OnClickListener;  

  28. import android.view.animation.Animation;  

  29. import android.view.animation.AnimationUtils;  

  30. import android.widget.Button;  

  31. import android.widget.TextView;  

  32. public class TestAnimation extends Activity 
    implements OnClickListener{  

  33. public void onCreate(Bundle savedInstanceState) {  

  34. super.onCreate(savedInstanceState);  

  35. setContentView(R.layout.main);  

  36. Button btn = (Button)findViewById(R.id.Button01);  

  37. btn.setOnClickListener(this);   

  38. }  

  39. @Override  

  40. public void onClick(View v) {  

  41. Animation anim = AnimationUtils.loadAnimation(this, 
    R.anim.my_rotate_action);  

  42. findViewById(R.id.TextView01).startAnimation(anim);  

  43. }  

Android使用Animation方法二:直接在代码中定义动画(效果跟方法一类似):

Java代码

  1. package com.ray.animation;   

  2. import android.app.Activity;   

  3. import android.os.Bundle;   

  4. import android.view.View;   

  5. import android.view.View.OnClickListener;   

  6. import android.view.animation.AccelerateDecelerateInterpolator;   

  7. import android.view.animation.Animation;   

  8. import android.view.animation.RotateAnimation;   

  9. import android.widget.Button;   

  10. public class TestAnimation extends Activity 
    implements OnClickListener{   

  11. public void onCreate(Bundle savedInstanceState) {   

  12. super.onCreate(savedInstanceState);   

  13. setContentView(R.layout.main);   

  14. Button btn = (Button)findViewById(R.id.Button);   

  15. btn.setOnClickListener(this);   

  16. }   

  17. public void onClick(View v) {   

  18. Animation anim = null;   

  19. anim = new RotateAnimation(0.0f,+360.0f);   

  20. anim.setInterpolator(new AccelerateDecelerateInterpolator());   

  21. anim.setDuration(3000);   

  22. findViewById(R.id.TextView01).startAnimation(anim);   

  23. }   

关于Android中Animation如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI