Android 里有3种动画,帧动画,补间动画,属性动画。
View Animation: 视图动画在古老的Android版本系统中就已经提供了,只能被用来设置View的动画。
Drawable Animation: 这种动画(也叫Frame动画、帧动画)其实可以划分到视图动画的类别,专门用来一个一个的显示Drawable的resources,就像放幻灯片一样。
Property Animation: 属性动画只对Android 3.0(API 11)以上版本的Android系统才有效,这种动画可以设置给任何Object,包括那些还没有渲染到屏幕上的对象。这种动画是可扩展的,可以让你自定义任何类型和属性的动画。
一、帧动画 路径为 res/drawable
帧动画使用的类是 AnimationDrawable
帧动画实现的方式有2种
一、1.在res/drawable文件夹新建animation-list 的xml文件frame_animation
2.在文件中添加图片
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/alipay_msp_success1" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success2" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success3" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success4" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success5" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success6" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success7" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success8" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success9" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success10" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success11" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success12" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success13" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success14" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success15" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success16" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success17" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success18" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success19" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success20" android:duration="100"/> <item android:drawable="@drawable/alipay_msp_success21" android:duration="100"/> </animation-list>
根节点是animation-list(动画列表),里面有一个或者多个item节点组成,oneshot属性表示是否只播放一次,true表示只会播放一次,false表示一直循环播放,内部用item节点声明一个动画帧,android:drawable指定此帧动画所对应的图片资源,android:druation代表此帧持续的时间,整数,单位为毫秒。
在activity中获取该动画
private void initView() { iv = (ImageView) findViewById(R.id.iv); //获取image view的背景 AnimationDrawable background = (AnimationDrawable) iv.getBackground(); background.start(); // iv.setBackgroundResource(R.drawable.frame_animation); AnimationDrawable background1 = (AnimationDrawable) iv.getBackground(); // AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable( R.drawable.frame_animation); iv.setBackground(animationDrawable); }
二、直接在activity中使用帧动画
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) private void frameAnimation(){ AnimationDrawable animationDrawable = new AnimationDrawable(); // 为AnimationDrawable添加动画帧 animationDrawable.addFrame( getResources().getDrawable(R.drawable.alipay_msp_success1), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.alipay_msp_success2), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.alipay_msp_success3), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.alipay_msp_success4), 50); // 设置为循环播放 animationDrawable.setOneShot(false); iv.setBackground(animationDrawable); animationDrawable.start(); }
二、补间动画 Tween动画 ObjectAnimation 路径为res/anim 文件夹下 只能给view用
补间动画的分类 四种
alpha: 透明度动画效果 AlphaAnimation
scale: 缩放动画效果 ScaleAnimation
translate: 位置移动动画效果 TranslateAnimation
rotate: 旋转动画效果 RotateAnimation
这四种动画能够分别带来不同的效果体验,又能混合在一起完成酷炫的动画效果。
如何使用
和帧动画一样分两种方式
一、在xml中定义
1.在 res/anim
的文件夹里创建动画效果.xml
文件
2.定义各种动画的xml文件
二、在java代码中直接使用
//平移动画 TranslateAnimation translateAnimation = new TranslateAnimation( 0, 500, 0, 500); translateAnimation.setDuration(2000); iv.startAnimation(translateAnimation); //淡入淡出动画 AlphaAnimation alphaAnimation=new AlphaAnimation(0,1); alphaAnimation.setDuration(2000); iv.startAnimation(alphaAnimation); //旋转动画 RotateAnimation rotateAnimation=new RotateAnimation(0,90); rotateAnimation.setDuration(2000); iv.startAnimation(rotateAnimation); //缩放动画 ScaleAnimation scaleanimation =new ScaleAnimation(1f,2.0f,1f,2.0f); scaleanimation.setDuration(2000); iv.startAnimation(scaleanimation);
公共属性
2 平移动画translate
4、roate动画,旋转动画
5.set集合
就是把以上几种动画放到一起
也是可以通过xml 或者java代码形式实现
xml形式
<set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:duration="2000" android:fromAlpha="0" android:toAlpha="1"> </alpha> <rotate xmlns:android ="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="5000"> </rotate> <translate xmlns:android ="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="300" android:duration="5000"> </translate> </set>
Animation animation4= AnimationUtils.loadAnimation(this, R.anim.set);
iv.startAnimation(animation4);
java代码形式
//组合动画 AnimationSet setAnimation = new AnimationSet(true); // 步骤1:创建组合动画对象(设置为true) // 步骤2:设置组合动画的属性 // 特别说明以下情况 // 因为在下面的旋转动画设置了无限循环(RepeatCount = INFINITE) // 所以动画不会结束,而是无限循环 // 所以组合动画的下面两行设置是无效的 setAnimation.setRepeatMode(Animation.RESTART); setAnimation.setRepeatCount(1);// 设置了循环一次,但无效 setAnimation.addAnimation(alphaAnimation); setAnimation.addAnimation(scaleanimation); setAnimation.addAnimation(rotateAnimation); setAnimation.addAnimation(translateAnimation); iv.startAnimation(setAnimation);
属性动画,下章再讲