首先,view动画有4种,AlphaAnimation(透明度)、ScaleAnimation(缩放)、TranslateAnimation(位移)、RotateAnimation(旋转)
还有一种是这四个动画的集合AnimationSet
他们可以在java代码中实现,也可以在XML中实现,下面附上代码
动画位置在res/anim/anim_name.xml
<pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package]:anim/res_interpolator" android:shareInterpolator="boolean" > <alpha android:fromAlpha="float" android:toAlpha="float" /> <rotate android:fromDegrees="float" android:pivotX="float|fraction" android:pivotY="float|fraction" android:toDegrees="float" /> <translate android:fromXDelta="float|fraction" android:fromYDelta="float|fraction" android:toXDelta="float|fraction" android:toYDelta="float|fraction" /> <scale android:duration="int"<!-- 单位ms --> android:fromXScale="float|fraction" android:fromYScale="float|fraction" android:pivotX="float|fraction" android:pivotY="float|fraction" android:toXScale="float|fraction" android:toYScale="float|fraction" /> </set>
Animation animation01 = AnimationUtils.loadAnimation(this, R.anim.scale02); bt.startAnimation(animation01);
TranslateAnimation translateAnimation = new TranslateAnimation( Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0); translateAnimation.setDuration(500); translateAnimation.setFillAfter(true); bt.startAnimation(translateAnimation);
public static interface AnimationListener { /** * <p>Notifies the start of the animation.</p> * * @param animation The started animation. */ void onAnimationStart(Animation animation); /** * <p>Notifies the end of the animation. This callback is not invoked * for animations with repeat count set to INFINITE.</p> * * @param animation The animation which reached its end. */ void onAnimationEnd(Animation animation); /** * <p>Notifies the repetition of the animation.</p> * * @param animation The animation which was repeated. */ void onAnimationRepeat(Animation animation); }
public class MyAnimation extends Animation { @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); } }
initialize()进行一些初始化的工作
applyTransformation()中进行相应的矩阵变化,结合Camera和Matrix来简化矩阵的变化,可以参考android的Camera和Matrix
TranslateAnimation translateAnimation = new TranslateAnimation( Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 0); translateAnimation.setDuration(500); translateAnimation.setFillAfter(true); bt.startAnimation(translateAnimation);