Animation参考(View动画)

来源:互联网 发布:mkv剪辑软件 编辑:程序博客网 时间:2024/06/06 15:52

该文章介绍在xml中定义View动画。以下会将常见的属性方法写出,实际开发中不一定全都用到,具体情况具体分析。

set动画集合 ##

<set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="[integer]"// 动画时长    android:repeatMode="reverse|restart"// 动画重复模式    android:repeatCount="infinite"// 循环次数,infinite无限循环    android:fillAfter="true|false"// 结束时停留在最后一帧    android:fillBefore="true|false"// 结束时停留在第一帧    android:startOffset="[integer]"// 延迟[integer]开始动画(包括重复)    android:shareInterpolator="true|false"// 是否设置动画效果、速率    android:interpolator="@android:anim/...">// 定义动画效果、速率    <alpha        android:fromAlpha=""// 起始透明度        android:toAlpha=""/>// 终止透明度    <rotate        android:fromDegrees=""// 起始角度        android:toDegrees=""// 终止角度        android:pivotX=""// 中心点x坐标        android:pivotY=""/>// 中心点y坐标    <scale        android:fromXScale=""// 起始x缩放值        android:fromYScale=""// 起始y缩放值        android:toXScale=""// 终止x缩放值        android:toYScale=""// 终止y缩放值        android:pivotX=""// 中心点x坐标        android:pivotY=""/>// 中心点y坐标    <translate        android:fromXDelta=""// 起始x位置        android:fromYDelta=""// 起始y位置        android:toXDelta=""// 终止x位置        android:toYDelta=""/>// 终止y位置</set>

AnimationSet

常见构造函数:
AnimationSet(boolean shareInterpolator)
该类对应xml中的,其方法也对应set的属性,无限循环时fillAfter和fillBefore失效

AnimationSet set = new AnimationSet(true);set.setAnimationListener(new Animation.AnimationListener() {    @Override    public void onAnimationStart(Animation animation) {}    @Override    public void onAnimationEnd(Animation animation) {}    @Override    public void onAnimationRepeat(Animation animation) {}});set.setDuration(1000);set.setRepeatMode(Animation.REVERSE);set.setRepeatCount(Animation.INFINITE);// 发觉不会作用到动画里set.setFillAfter(true);set.setFillBefore(false);// 默认动画结束时回到初始状态set.setStartOffset(200);set.setInterpolator(new AccelerateDecelerateInterpolator());set.addAnimation(animation);// 将动画添加到集合中view.setAnimation(set);// 将动画绑定到view中set.start();// 开始动画

AlphaAnimation

透明度动画可用来实现闪烁效果
常用构造函数:
AlphaAnimation(float fromAlpha, float toAlpha)
对应xml中的

AlphaAnimation alpha = new AlphaAnimation(0.5f, 1f);alpha.setDuration(1000);alpha.setStartOffset(100);alpha.setRepeatMode(Animation.REVERSE);alpha.setRepeatCount(Animation.INFINITE);alpha.setFillAfter(true);alpha.setFillBefore(false);alpha.setInterpolator(new AccelerateDecelerateInterpolator());view.setAnimation(alpha);alpha.start();添加到动画集合:set.addAnimation(alpha);

RotateAnimation

(toDegrees-fromDegrees)正为顺时针旋转,负为逆时针旋转
常用构造函数:
// 默认旋转点为左上角(0,0)
RotateAnimation(float fromDegrees, float toDegrees)
// (pivotX,pivotY)为旋转中心坐标
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
对应xml中的

RotateAnimation rotate = new RotateAnimation(0, 30);RotateAnimation rotate = new RotateAnimation(0, 30, 360, 540);rotate.setDuration(1000);rotate.setStartOffset(200);rotate.setRepeatMode(Animation.REVERSE);rotate.setRepeatCount(Animation.INFINITE);rotate.setFillBefore(true);rotate.setFillAfter(false);rotate.setInterpolator(new AccelerateDecelerateInterpolator());img.setAnimation(rotate);rotate.start();

ScaleAnimation

对应xml中的
构造函数:
// 默认缩放中心点为左上角(0,0)
ScaleAnimation(float fromX, float toX, float fromY, float toY)
// (pivotX,pivotY)为缩放中心坐标
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)

ScaleAnimation scale = new ScaleAnimation(0.5f, 1.5f, 0.5f, 1.5f);ScaleAnimation scale = new ScaleAnimation(0.5f, 1.5f, 0.5f, 1.5f, 360, 540);scale.setDuration(1000);scale.setStartOffset(200);scale.setRepeatMode(Animation.REVERSE);scale.setRepeatCount(Animation.INFINITE);scale.setFillBefore(true);scale.setFillAfter(false);scale.setInterpolator(new AccelerateDecelerateInterpolator());view.setAnimation(scale);scale.start();

TranslateAnimation

对应xml中的
常见构造函数:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

TranslateAnimation translate = new TranslateAnimation(0, 100, 0, 100);translate.setDuration(1000);translate.setStartOffset(200);translate.setRepeatMode(Animation.REVERSE);translate.setRepeatCount(Animation.INFINITE);translate.setFillBefore(true);translate.setFillAfter(false);translate.setInterpolator(new AccelerateDecelerateInterpolator());view.setAnimation(translate);translate.start();

Interpolator

Interpolator官方解释

 1. AccelerateDecelerateInterpolator     动画开始和结束缓慢但加速通过中间。 2. AccelerateInterpolator    动画开始缓慢,然后加速。 3. AnticipateInterpolator    动画开始向后,然后向前飞行。    4. AnticipateOvershootInterpolator    动画开始向后,然后向前飞行并且超过目标值,最终返回到最终值。 5. BounceInterpolator    动画结束时弹起。 6. CycleInterpolator(float cycles)    重复动画指定的循环次数cycles。 7. DecelerateInterpolator    动画开始快速启动然后减速。 8. LinearInterpolator    动画速率是恒定的。 9. OvershootInterpolator    动画结束时再超出一定值后返回。
阅读全文
0 0
原创粉丝点击