我们在开发中,UI经常需要使用到动画:animation 英 ænɪ’meɪʃ(ə)n 。下面介绍几种常见的动画。
一,补间动画 Tweened animation
- alpha,透明度变化,fromAlpha toAlpha,xml文件:R.anim.anim_alpha如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:duration="2000"
/>
</set>
- scale,缩放,fromXScale toXScale 水平方向缩放比例
"><set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<!--使动画保持在播放的最后一帧,必须加在<set>标签这里才起作用-->
<scale
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
</set>
- translate,位移,垂直方向位移变化,fromYDelta toYDlta
"><set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<translate
android:fromXDelta="10"
android:toXDelta="100"
android:fromYDelta="10"
android:toYDelta="100"
/>
</set>
- rotate,旋转,角度变化。fromDegrees,toDegress
"><set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<!--pivotX pivotY 旋转的中心点。(0.5f,0.5f)表示View本身的中心 -->
<rotate
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
</set>
PS:duration是指动画的时间,FillEnable = false 时,FillBefore 和FillAfter的值将被忽略不起作用.
动画集:多个Tween动画连起来成为动画集。
private void startAnimation() {
//从布局文件中加载动画
Animation alphaAnimation = AnimationUtils.loadAnimation(mContext, R.anim.anim_alpha);
Animation scaleAnimation = AnimationUtils.loadAnimation(mContext, R.anim.anim_scale);
Animation rotateAnimation = AnimationUtils.loadAnimation(mContext, R.anim.anim_rotate);
//动画集
AnimationSet set = new AnimationSet(true);
set.addAnimation(alphaAnimation);
set.addAnimation(scaleAnimation);
set.addAnimation(rotateAnimation);
//设置动画时间 (作用到每个动画)
set.setDuration(1400);
iv_splash_circle.startAnimation(set);
}
在代码中实现动画
只要用对应的API即可,例如:
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间(单位:毫秒)
alphaAnimation.setDuration(1000);
//将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用view的startAnimation方法开始执行动画
view.startAnimation(animationSet);
帧动画 Frame by frame animation
一个画面就是一个frame,就像flash中的帧动画一样,将每个帧连起来的动画,就像gif图片一样的效果。
逐帧动画其实很简单,下面我们来看一个例子:
xml布局文件:R.drawable.animation。
"><!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item android:drawable="@drawable/icon1" android:duration="150"/>
<item android:drawable="@drawable/icon2" android:duration="150"/>
<item android:drawable="@drawable/icon3" android:duration="150"/>
</animation-list>
将三张图片连起来。可以在ImageView的xml布局文件中直接引入Drawable,也可以在代码中引用。比如:
AnimationDrawable animationDrawable =
(AnimationDrawable) imageView.getDrawable();
animationDrawable.start();
注:常见的动画插入器
Interpolator属性是Animation类的一个XML属性,alpha、scale、rotate、translate、set都会继承得到这个属性。
accelerate_decelerate_interpolator
加速-减速 动画插入器
accelerate_interpolator
加速-动画插入器
decelerate_interpolator
减速- 动画插入器
提供几个封装好的创建动画的方法:
/**
* 位移动画
*/
public static void translateAnimation(View v,float fromXDelta,float toXDelta, float fromYDelta, float toYDelta,long durationMillis){
TranslateAnimation animation=new TranslateAnimation(fromXDelta,toXDelta,fromYDelta,toYDelta);
animation.setDuration(durationMillis);
animation.setFillAfter(true);
v.startAnimation(animation);
LogUtils.e("AnimationUtil", "TranslateAnimation");
}
/** 创建一个淡入放大的动画 */
public static Animation createZoomInNearAnim() {
AnimationSet ret;
Animation anim;
ret = new AnimationSet(false);
// 创建一个淡入的动画
anim = new AlphaAnimation(0f, 1f);
anim.setDuration(MEDIUM);
anim.setInterpolator(new LinearInterpolator());
ret.addAnimation(anim);
// 创建一个放大的动画
anim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(MEDIUM);
anim.setInterpolator(new DecelerateInterpolator());
ret.addAnimation(anim);
return ret;
}
其他动画,属性动画,动画框架下次再总结。
欢迎与杜工交流:Dusan,QQ291902259。