温馨提示×

温馨提示×

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

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

Android中Animation如何使用

发布时间:2021-06-26 15:04:39 阅读:180 作者:Leah 栏目:移动开发
Android开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

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

Xml代码

?xml version="1.0" encoding="utf-8"?>   < set xmlns:android="http://schemas.android.com/apk/res/android">   < rotate   android:interpolator="@android:anim/accelerate_decelerate_interpolator"   android:fromDegrees="0"   android:toDegrees="+360"   android:duration="3000" />   < !-- rotate 旋转动画效果   属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化   fromDegrees 属性为动画起始时物件的角度   toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针   duration 属性为动画持续时间,以毫秒为单位   -->   < /set>   < ?xml version="1.0" encoding="utf-8"?> < set xmlns:android="http://schemas.android.com/apk/res/android"> < rotate   android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0"   android:toDegrees="+360" android:duration="3000" /> < !-- rotate 旋转动画效果  属性:interpolator 指定一个动画的插入器,用来控制动画的速度变化  fromDegrees 属性为动画起始时物件的角度   toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针  duration 属性为动画持续时间,以毫秒为单位  --> < /set> 

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

Java代码

package com.ray.animation;   import android.app.Activity;   import android.os.Bundle;   import android.view.View;   import android.view.View.OnClickListener;   import android.view.animation.Animation;   import android.view.animation.AnimationUtils;   import android.widget.Button;   import android.widget.TextView;   public class TestAnimation extends Activity implements OnClickListener{   public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   Button btn = (Button)findViewById(R.id.Button01);   btn.setOnClickListener(this);   }   @Override   public void onClick(View v) {   Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);   findViewById(R.id.TextView01).startAnimation(anim);   }   }   package com.ray.animation;  import android.app.Activity;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClickListener;  import android.view.animation.Animation;  import android.view.animation.AnimationUtils;  import android.widget.Button;  import android.widget.TextView;  public class TestAnimation extends Activity implements OnClickListener{  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  Button btn = (Button)findViewById(R.id.Button01);  btn.setOnClickListener(this);   }  @Override  public void onClick(View v) {  Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);  findViewById(R.id.TextView01).startAnimation(anim);  }  } 

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

Java代码

package com.ray.animation;   import android.app.Activity;   import android.os.Bundle;   import android.view.View;   import android.view.View.OnClickListener;   import android.view.animation.AccelerateDecelerateInterpolator;   import android.view.animation.Animation;   import android.view.animation.RotateAnimation;   import android.widget.Button;   public class TestAnimation extends Activity implements OnClickListener{   public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   Button btn = (Button)findViewById(R.id.Button);   btn.setOnClickListener(this);   }   public void onClick(View v) {   Animation anim = null;   anim = new RotateAnimation(0.0f,+360.0f);   anim.setInterpolator(new AccelerateDecelerateInterpolator());   anim.setDuration(3000);   findViewById(R.id.TextView01).startAnimation(anim);   }   } 

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×