View动画效果: 1.>>Tween动画
<?xml version="1.0" encoding="utf-8"?> 在java代码中设置开始动画: Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);// 使用rotate.xml生成动画效果对象 animation.setFillAfter(true);// 动画停止时保持在该动画结束时的状态 2.>>Frame动画,即顺序播放事先做好的图像,开发步骤: TextView tv = (TextView) findViewById(R.id.tv); tv.setBackgroundResource(R.drawable.frame); // 绑定frame动画,会发送一个消息到主线程的消息队列处理器等待处理,绑定事件完成之后才能进行动画启动 final AnimationDrawable drawable = (AnimationDrawable) findViewById(R.id.tv).getBackground(); // Looper().myQueue():取得消息队列,要消息(事件)处理完成之后才执行addIdleHandler添加进的handler中的方法 Looper.myQueue().addIdleHandler( new MessageQueue.IdleHandler() { @Override public boolean queueIdle() { drawable.start();// 启动动画,要绑定事件处理完成之后才能启动动画 return false;// 只要执行操作之后就会从消息队列中移出 } });// 取得处理主线程中处理的消息队列 注意: Frame动画必须使用<animation-list>作为根节点,下面是frame.xml文件的定义; <animation-list xmlns:android="http://schemas./apk/res/android" android:oneshot="true" ><!-- 动画只播放一次 --> <item android:drawable="@drawable/a1" android:duration="500"></item> <!--第一项为一个静态的图像--> <item android:drawable="@drawable/a2" android:duration="500"></item> <item android:drawable="@drawable/a3" android:duration="500"></item> </animation-list> 3.>>属性动画 <set xmlns:android="http://schemas./apk/res/android" android:ordering="sequentially" > // 动画执行顺序,默认是同时执行,在此设置为按顺序执行 <set> // 定义x,y的值在0.5秒内移动到(400,300) 在此未设置android:ordering属性,此set则为同时执行 <objectAnimator android:duration="500" android:propertyName="x" android:valueTo="400" android:valueType="intType" /> <objectAnimator android:duration="500" android:propertyName="y" android:valueTo="300" android:valueType="intType" /> </set> <objectAnimator android:duration="500" android:propertyName="alpha" android:valueTo="1f" /> </set> 加载属性动画资源文件: // 加载动画资源 AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.aim); // 设置要控制的对象 set.setTarget(null); set.start(); 4.>>LayoutAnimaionController LayoutAnimaionController为Layout或者viewGroup里的控件设置动画效果,特点是它会使其中的每个控件都有相同的动画效果,这些控件的动画效果可以在不同的时间显示出来。 mLayout = (LinearLayout) findViewById(R.id.ll); Animation anim = new TranslateAnimation(0, 200, 0, 0); anim.setDuration(500); anim.setFillAfter(true); LayoutAnimationController layoutAnim = new LayoutAnimationController(anim); // 设置动画 layoutAnim.setOrder(LayoutAnimationController.ORDER_NORMAL); // 设置子View动画顺序 mLayout.setLayoutAnimation(layoutAnim); 关于LayoutAnimaionController,它需要一个Animation对象用于实例化,可以设置子View动画顺序,有三种顺序方式
LayoutAnimaionController .ORDER_NORMAL // 顺序 还可以设置延迟setDelay(param); 5.>>Interpolator动画速率 /** * An interpolator defines the rate of change of an animation. This allows * the basic animation effects (alpha, scale, translate, rotate) to be * accelerated, decelerated, repeated, etc. */ public interface Interpolator extends TimeInterpolator { } 查看接口Interpolator的定义,Interpolator接口定义动画改变的速度,如基本的动画效果(透明度、比例、移动、旋转)的加速、减速、重复等; /** * TimeInterpolator定义动画速率的改变,允许动画有非固定的运动如加速、减速 */ public interface TimeInterpolator { /** * @param input 一个介于0到1的参数标识当前点的位置,0代表开始,1代表结束 * @return 返回一个动画补插值.这个值可以小于1{在目标的后面}或大于1{在目标的后面} */ float getInterpolation(float input); } Android已经定义了几个该接口的直接子类,在程序中可以直接调用 <set xmlns:android="http://schemas./apk/res/android" android:shareInterpolator="true" > <!--表明在下面的动画中分享Interpolator--> <scale android:duration="100" android:fillAfter="false" android:fromXScale="0.5" android:fromYScale="0.5"android:interpolator="@android:anim/accelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:startOffset="-50" android:toXScale="1.2" android:toYScale="1.2" /> </set> 上面是使用系统自带的Interpolator的方法,也可以自定义Interpolator public class MyInter implements Interpolator { // ... @Override public float getInterpolation(float input) { // ... return 0; } // ... } 在使用的时候,使用如下的方式; ImageView iv = new ImageView(getContext()); Animation animation = AnimationUtils.loadAnimation(getContext(), 0x70982743); animation.setInterpolator(new MyInter()); iv.startAnimation(animation); Activity切换动画效果: 言归正传,直接上我淘到的方法:通过Theme对Activity动画效果进行设置 <style name="AppTheme" parent="@android:style/Theme"> <!-- 设置activity切换动画 --> <item name="android:windowAnimationStyle">@style/activityAnimation</item> </style> <!-- animation 样式 --> <style name="activityAnimation" parent="@android:style/Animation"> <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item> <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item> <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item> <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item> </style> 上面activityAnimation下面四个item对应的动画分别为(假设从Activity A 进入到Activity B): 进入B时B执行的动画; 两个google官方参考链接:
|
|