英文较好的可以移步官方介绍
Tween动画(补间动画),可以在一个容器(布局)内执行透明度变化,旋转,大小变化,位移等动画,它是通过ParentView来不断调整ChildView的不同坐标来实现的。
package:android.view.animation; 抽象类 Animation;
xml属性java方法解释android:detachWallpapersetDetachWallpaper(boolean)是否在壁纸上运行android:durationsetDuration(long)动画持续时间,毫秒为单位android:fillAftersetFillAfter(boolean)控件动画结束时是否保持动画最后的状态android:fillBeforesetFillBefore(boolean)控件动画结束时是否还原到开始动画前的状态android:fillEnabledsetFillEnabled(boolean)与android:fillBefore效果相同android:interpolatorsetInterpolator(Interpolator)设定插值器(指定的动画效果,譬如回弹等)android:repeatCountsetRepeatCount(int)重复次数android:repeatModesetRepeatMode(int)重复类型有两个值,reverse表示倒序回放,restart表示从头播放android:startOffsetsetStartOffset(long)调用start函数之后等待开始运行的时间,单位为毫秒android:zAdjustmentsetZAdjustment(int)表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normalpackage:android.view.animation;
Java类动画AlphaAnimation渐变透明度动画效果RotateAnimation画面转移旋转动画效果ScaleAnimation渐变尺寸伸缩动画效果AnimationSet动画容器TranslateAnimation画面转移位置移动动画效果Animation的实现类每一个都具有Animation的属性。
分别看一下每种动画的属性:
动画容器,没有自己特有的属性,继承自Animation的属性对标签下所有生效。
使用示例:
//Alpha动画 ImageView imageView = (ImageView) findViewById(R.id.imageView); Animation animation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1f toAlpha 1.0f animation.setDuration(1000);//动画持续时间 animation.setRepeatCount(-1);// 设置动画的重复次数,-1表示无限重复,0表示不重复 animation.setRepeatMode(Animation.REVERSE);// 设置动画重复时的切换模式(RESTART表示重新执行,REVERSE表示动画反操作) imageView.startAnimation(animation);//开始动画 //imageView.setAnimation(animation); //animation.start();其余几种使用方式大同小异,不同的构造方法传不同的参数。
//AnimationSet使用 ImageView imageView = (ImageView) findViewById(R.id.imageView); AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1ftoAlpha 1.0f alphaAnimation.setDuration(1000); TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200); translateAnimation.setDuration(1000); animationSet.addAnimation(alphaAnimation);//添加Alpha动画 animationSet.addAnimation(translateAnimation);//添加Translate动画 imageView.startAnimation(animationSet);在res/anim目录下新建资源文件
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fromAlpha="1.0" android:toAlpha="0.0"> </alpha>在要使用的地方:
ImageView imageView = (ImageView) findViewById(R.id.imageView); Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha); imageView.setAnimation(animation);其他的类似。
Interpolator定义了动画变化的速率或规律 Interpolator接口的实现:
java类xml id值描述AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator动画始末速率较慢,中间加速AccelerateInterpolator@android:anim/accelerate_interpolator动画开始速率较慢,之后慢慢加速AnticipateInterpolator@android:anim/anticipate_interpolator开始的时候从后向前甩AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator类似上面AnticipateInterpolatorBounceInterpolator@android:anim/bounce_interpolator动画结束时弹起CycleInterpolator@android:anim/cycle_interpolator循环播放速率改变为正弦曲线DecelerateInterpolator@android:anim/decelerate_interpolator动画开始快然后慢LinearInterpolator@android:anim/linear_interpolator动画匀速改变OvershootInterpolator@android:anim/overshoot_interpolator向前弹出一定值之后回到原来位置PathInterpolator新增,定义路径坐标后按照路径坐标来跑Java方式
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1f toAlpha 1.0f alphaAnimation.setDuration(1000); alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); //AnimationSet AnimationSet animationSet = new AnimationSet(true); animationSet.setInterpolator(new AccelerateDecelerateInterpolator());xml方式
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:shareInterpolator="true" android:fillAfter="true"> //也可以为单个动画设置 <alpha android:interpolator="@android:anim/anticipate_overshoot_interpolator" android:duration="3000" android:fromAlpha="1.0" android:toAlpha="0.0" />可以为每一个动画来注册一个监听,来监听动画的状态
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); //fromAlpha 0.1f toAlpha 1.0f alphaAnimation.setDuration(1000); alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { //动画开始时 } @Override public void onAnimationEnd(Animation animation) { //动画重复时 } @Override public void onAnimationRepeat(Animation animation) { //动画结束时 } });前面我们讲过View动画是通过ParentView来不断调整ChildView的不同坐标来实现的,动画的实现也就是通过变化像素矩阵的点来实现动画的,通过这个我们其实可以自己定制矩阵的变化来实现各种效果,像三维变换等。 在抽象类Animation中有一个重要的方法:
protected void applyTransformation(float interpolatedTime, Transformation t)其实AlphaAnimation,RotateAnimation,ScaleAnimation|,TranslateAnimation都是通过定制这个方法来实现的。 系统通过不断调用这个方法来实现动画的变换,那我们要自己定制动画的话就通过定制 Transformation t 来告诉系统具体的变换方法。 float interpolatedTime 可以理解为动画的进度 在Transformation中有两个属性:
protected Matrix mMatrix;//矩阵 protected float mAlpha;//透明度ok,实现一个抛物线的动画看看效果。
public class ParabolaAnimation extends Animation { private final float mPara; /** * Creates a new animation with a duration of 0ms, the default interpolator, with * fillBefore set to true and fillAfter set to false */ public ParabolaAnimation(float mPara) { this.mPara = mPara; } /** * Initialize this animation with the dimensions of the object being * animated as well as the objects parents. (This is to support animation * sizes being specified relative to these dimensions.) * <p> * <p>Objects that interpret Animations should call this method when * the sizes of the object being animated and its parent are known, and * before calling {@link #getTransformation}. * * @param width Width of the object being animated * @param height Height of the object being animated * @param parentWidth Width of the animated object's parent * @param parentHeight Height of the animated object's parent */ @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); } /** * Helper for getTransformation. Subclasses should implement this to apply * their transforms given an interpolation value. Implementations of this * method should always replace the specified Transformation or document * they are doing otherwise. * * @param interpolatedTime The value of the normalized time (0.0 to 1.0) * after it has been run through the interpolation function. * @param t The Transformation object to fill in with the current */ @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); float paraX = mPara * interpolatedTime;//横坐标随着时间匀速增加 float paraY = paraX*paraX;//纵坐标为横坐标的平方 final Matrix matrix = t.getMatrix();//拿到矩阵 matrix.setTranslate(10*paraX,paraY);//调用矩阵的Translate方法 } }使用:
final ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ParabolaAnimation parabolaAnimation = new ParabolaAnimation(imageView.getWidth()); parabolaAnimation.setDuration(4000); imageView.startAnimation(parabolaAnimation); } });看看效果:
这只是一个简单的例子,更复杂一点的靠我们仔细去研究,但是方法都是差不多的。
http://blog.csdn.net/yanbober/article/details/46481171