使用xml的方式设置动画属性java
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android"> 3 <!-- 4 fromAlpha:动画的初始状态 5 toAlpha:动画的最终状态 6 duration:动画从初始状态到最终状态的持续时间 7 repeatCount:不包括第一次显示的,显示次数 8 --> 9 <alpha 10 android:fromAlpha="0.1" 11 android:toAlpha="1.0" 12 android:duration="2000" 13 android:repeatCount="3" 14 /> 15 </set>
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 tools:context="com.test2_23.test2_23.AlphaAnimation_Activity"> 5 6 <ImageView 7 android:id="@+id/img_Alpha" 8 android:src="@drawable/img_bird" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_centerInParent="true" 12 /> 13 14 </RelativeLayout>
1 public class AlphaAnimation_Activity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_alpha_animation_); 7 //获取到要设置动画的控件 8 ImageView iv = (ImageView) findViewById(R.id.img_Alpha); 9 //获取动画文件 10 Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation); 11 //清除以前的动画 12 iv.clearAnimation(); 13 //开始动画 14 iv.startAnimation(animation); 15 } 16 }
使用java的方式来设置动画属性android
1 public class javaAlpha_Activity extends Activity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_java_alpha_); 7 ImageView img = (ImageView) findViewById(R.id.img_Alpha_java); 8 //使用java代码实现也很是简单,直接设置一个Animation类的对象就能够了 9 //AlphaAnimation 是Animation的子类 10 //在构造函数中设置他的初始状态fromAlpha和最终状态toAlpha,是float类型 11 Animation animation = new AlphaAnimation(0.1f,1.0f); 12 //设置持续时间 13 animation.setDuration(2000); 14 //设置重复次数 15 animation.setRepeatCount(3); 16 //清除原有的动画,避免屡次点击出现重复的效果 17 img.clearAnimation(); 18 //开始执行动画 19 img.startAnimation(animation); 20 } 21 }
补间动画(TweenAnimation):补间动画就是肯定了动画的启始和终点两个端点状态以后,由系统自动计算中间的各个状态,并补充到两个端点之间,造成一个连续的动画效果。ide
补间动画能够分为4种类型:函数
渐变更画(AlphaAnimation)动画
缩放动画 (scaleAnimation)this
平移动画(TranslateAnimation)spa
旋转动画(RotateAnimation)code
这几种动画均可以用xml文件和java代码的方式实现,xml
渐变更画xml实现方式:对象
1.在res目录下建立文件夹anim
2.在anim文件夹下建立animation.xml动画文件
3.在animation.xml中描述某种动画的属性
4.在java代码中用AnimationUtils.loadAnimation方法加载动画
5.使用View的startAnimation方法启动动画
渐变更画效果:能够演示一个视图在透明度上的渐变效果。
主要属性:
FromAlpha:动画启始透明度,取值在0.0-1.0之间
toAlpha:动画结束时透明度,取值在0.0-1.0之间
duration:动画从开始到结束的持续时间以毫秒作单位
repeatCount :动画的重复次数,不包括第一次播放