不管是view动画,还是属性动画, 都有以下4 种效果.
alpha (透明度)
translate(平移)
rotate(旋转)
scale (缩放)
一:第一类view动画
两种实现方式
1.用代码来实现
每种动画都是new 出来的.注意, 会设置动画的时长, 重复的次数, 重复的类型,示例如下:
// 从布局文件中拿到控件 iv = (ImageView) findViewById(R.id.iv); // 透明度动画 publicvoid alpha(View v){ // 创建AlphaAnimation对象 // AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); AlphaAnimation aa =newAlphaAnimation(1.0f,0.0f); // 设置持续的时间 aa.setDuration(3000); // 重复的次数 aa.setRepeatCount(AlphaAnimation.INFINITE); // 设置重复的方式 aa.setRepeatMode(AlphaAnimation.REVERSE); // 开启动画 iv.startAnimation(aa); } // 缩放动画 publicvoid scale(View v){ // 创建ScaleAnimation对象 ScaleAnimation sa =newScaleAnimation(0.5f,3f,0.5f,3f,ScaleAnimation.RELATIVE_TO_SELF,1f, ScaleAnimation.RELATIVE_TO_SELF,1f); // 设置持续的时间 sa.setDuration(3000); // 重复的次数 sa.setRepeatCount(RotateAnimation.INFINITE); // 设置持续的时间 sa.setRepeatMode(RotateAnimation.REVERSE); // 开启动画 iv.startAnimation(sa); } // 旋转动画 publicvoid rotate(View v){ // 创建RotateAnimation对象 RotateAnimation ra =newRotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,2.0f, RotateAnimation.RELATIVE_TO_SELF,2.0f); // 设置持续的时间 ra.setDuration(3000); // 重复的次数 ra.setRepeatCount(RotateAnimation.INFINITE); // 设置持续的时间 ra.setRepeatMode(RotateAnimation.REVERSE); // 开启动画 iv.startAnimation(ra); } // 平移动画 publicvoid translate(View v){ // 创建ScaleAnimation对象 TranslateAnimation ta =newTranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF,-1.0f, TranslateAnimation.RELATIVE_TO_SELF,2.0f,TranslateAnimation.RELATIVE_TO_SELF,-1.0f, TranslateAnimation.RELATIVE_TO_SELF,2.0f); // 设置持续的时间 ta.setDuration(3000); // 重复的次数 ta.setRepeatCount(RotateAnimation.INFINITE); // 设置持续的时间 ta.setRepeatMode(RotateAnimation.REVERSE); // 开启动画 iv.startAnimation(ta); } // 动画集(多种动画效果) publicvoid animset(View v){ //透明动画 AlphaAnimation aa =newAlphaAnimation(1.0f,0.0f); // 设置持续的时间 aa.setDuration(2000); // 重复的次数 aa.setRepeatCount(AlphaAnimation.INFINITE); // 设置重复的方式 aa.setRepeatMode(AlphaAnimation.REVERSE); //缩放动画 ScaleAnimation sa =newScaleAnimation(0.5f,3f,0.5f,3f,ScaleAnimation.RELATIVE_TO_SELF,1f, ScaleAnimation.RELATIVE_TO_SELF,1f); // 设置持续的时间 sa.setDuration(2000); // 重复的次数 sa.setRepeatCount(RotateAnimation.INFINITE); // 设置持续的时间 sa.setRepeatMode(RotateAnimation.REVERSE); // 旋转动画 RotateAnimation ra =newRotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,2.0f, RotateAnimation.RELATIVE_TO_SELF,2.0f); // 设置持续的时间 ra.setDuration(2000); // 重复的次数 ra.setRepeatCount(RotateAnimation.INFINITE); // 设置持续的时间 ra.setRepeatMode(RotateAnimation.REVERSE); // 平移动画 TranslateAnimation ta =newTranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF,-1.0f, TranslateAnimation.RELATIVE_TO_SELF,1.0f,TranslateAnimation.RELATIVE_TO_SELF,-1.0f, TranslateAnimation.RELATIVE_TO_SELF,1.0f); // 设置持续的时间 ta.setDuration(2000); // 重复的次数 ta.setRepeatCount(RotateAnimation.INFINITE); // 设置持续的时间 ta.setRepeatMode(RotateAnimation.REVERSE); AnimationSet as =newAnimationSet(true); as.addAnimation(aa); as.addAnimation(sa); as.addAnimation(ra); as.addAnimation(ta); iv.startAnimation(as); } 2. xml 配置文件来实现, 是为了对代码进行 复用 , 所以可以采用 配置文件来实现 . 注: 需要到 res 目录下 , 去新建 一个 anim 的文件夹 , 在这个文件夹下去建相应的配置文件 <?xml version="1.0" encoding="utf-8"?> //透明动画 <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" android:repeatCount="infinite" android:repeatMode="restart"> </alpha> <?xml version="1.0" encoding="utf-8"?> //旋转动画 <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="270" android:duration="2000" android:repeatCount="infinite" android:repeatMode="restart"> </rotate> <?xml version="1.0" encoding="utf-8"?> //缩放动画 <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.5" android:toXScale="2.0" android:fromYScale="0.5" android:toYScale="2.0" android:duration="2000" android:repeatCount="infinite" android:repeatMode="restart"> </scale> <?xml version="1.0" encoding="utf-8"?> //平移动画 <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="100" android:fromYDelta="0" android:toYDelta="100" android:duration="3000" android:repeatCount="infinite" android:repeatMode="restart"> </translate> <?xml version="1.0" encoding="utf-8"?> //多种动画效果 <set> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:repeatCount="infinite" android:repeatMode="restart" android:toDegrees="360"> </rotate> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0.5" android:fromYScale="0.5" android:repeatCount="infinite" android:repeatMode="restart" android:toXScale="2.0" android:toYScale="2.0"> </scale> </set> // 透明的动画 publicvoid alpha(View v){ // 通过xml文件实现动画 Animation aa =AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_anim); iv.startAnimation(aa); } // 缩放的动画 publicvoid scale(View v){ // 通过xml文件实现动画 Animation sa =AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_anim); iv.startAnimation(sa); } // 旋转的动画 publicvoid rotate(View v){ // 通过xml文件实现动画 Animation ra =AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_anim); iv.startAnimation(ra); } // 平移的动画 publicvoid translate(View v){ // 通过xml文件实现动画 Animation ta =AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_anim); iv.startAnimation(ta); } // 动画集(多种动画效果) publicvoid animset(View v){ Animation as =AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animset_anim); iv.startAnimation(as); } 二:第二类属性动画 两种实现方式 : 1. 代码来实现 这种属性动画不是 new 出来的 . //iv.setAlpha(alpha) /* iv控件具有一个alpha的属性,所以ofFloat调用时传入的是alpha属性值,具体的alpha透明度的值可以设置为多少需要去查 看setAlpha的API解释 */ ObjectAnimator oa =ObjectAnimator.ofFloat(love,"alpha",0,1); oa.setDuration(2000); //时长 oa.setRepeatCount(ObjectAnimator.INFINITE); //重复次数 oa.setRepeatMode(ObjectAnimator.REVERSE); //重复模式 oa.start(); //开启动画 2. xml 配置文件来实现 注: 需要到 res 目录下 , 去新建 一个 anim 的文件夹 , 在这个文件夹下去建相应的配置文件 <?xml version="1.0" encoding="UTF-8"?> <objectAnimatorandroid:repeatMode="restart" android:repeatCount="infinite" android:duration="2000" android:valueTo="1.0" android:valueFrom="0" android:propertyName="alpha" xmlns:android="http://schemas.android.com/apk/res/android"> </objectAnimator> //平移动画 public void translate(View v){ Animator animator =AnimatorInflater.loadAnimator(this, R.animator.animator_translate); // 设置作用的控件 animator.setTarget(iv); //开启动画 animator.start(); } // 旋转动画 publicvoid rotate(View v){ Animator animator =AnimatorInflater.loadAnimator(this,R.animator.animator_rotate); // 设置作用的控件 animator.setTarget(iv); //开启动画 animator.start(); } // 缩放动画 publicvoid scale(View v){ Animator animator =AnimatorInflater.loadAnimator(this,R.animator.animator_scale); // 设置作用的控件 animator.setTarget(iv); animator.start(); } // 动画集 publicvoid animset(View v){ Animator animator =AnimatorInflater.loadAnimator(this,R.animator.animator_set); // 设置作用的控件 animator.setTarget(iv); animator.start(); }