分享

Android 动画之View动画效果和Activity切换动画效果

 dmw_zgl 2014-11-21

View动画效果:

1.>>Tween动画
通过对View的内容进行一系列的图形变换(平移、缩放、旋转、透明度变换)实现动画效果,补间动画需要使用<set>节点作为根节点,子节点里可以为下表格中的四种动画标签,也可以包继续含<set>标签;动画的定义xml文件需要添加到res/anim文件夹中;

动画类型 Xml定义动画使用的节点 编码定义动画使用的类
渐变透明度动画效果 <alpha/> AlphaAnimation
渐变尺寸缩放动画效果 <scale/> ScaleAnimation
画面位置移动动画效果 <translate/> TranslateAnimation
画面旋转动画效果 <rotate/> RotateAnimation
复制代码
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas./apk/res/android"
android:shareInterpolator="false" > <alpha android:duration="5000" android:fromAlpha="1.0"<!-- 1代表可见,0代表不可见 --> android:toAlpha="0" >   <!-- 持续时间duration --> </alpha> <rotate android:duration="5000" android:fromDegrees="0" android:pivotX="50%" // 旋转参考点 android:pivotY="50%" // 旋转参考点 android:toDegrees="180" > </rotate> <scale android:duration="5000" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="5" android:toYScale="5" /> <!-- 动画开始时的比例大小,结束时的比例,缩放参考点 --> <translate android:duration="5000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="50" android:toYDelta="50" /> <!-- 从(0,0)到(50,50) --> </set>
复制代码

在java代码中设置开始动画:

 Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);// 使用rotate.xml生成动画效果对象
 animation.setFillAfter(true);// 动画停止时保持在该动画结束时的状态
ImageView.startAnimation(animation);
Animation animation1 = new RotateAnimation(0, 270, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 编码方式

2.>>Frame动画,即顺序播放事先做好的图像,开发步骤:
(1)把定义好的图片放进项目res/drawable下;
(2)在项目的res目录下创建anim文件夹,在此文件夹下定义动画xml文件,每一项静态的图像添加到drawable目录中,或者用编码方式定义动画效果;
(3)为View控件绑定动画效果,调用代表动画的AnimationDrawable的start()方法开始动画;

复制代码
        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.>>属性动画
属性动画可以使对象的属性值在一定时间间隔内变化到某一个值,如在1000毫秒内移动控件的位置(改变x,y的值),在0.5秒内改变alpha属性的值以改变控件透明度,属性动画资源文件位于res/animator目录中;如下是一个属性动画的定义

复制代码
<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       // 顺序
LayoutAnimaionController .ORDER_REVERSE     //反序
LayoutAnimaionController .ORDER_RANDOM      //随机

还可以设置延迟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已经定义了几个该接口的直接子类,在程序中可以直接调用
——AccelerateInterpolator:动画从开始到结束,变化率是一个加速的过程。
——DecelerateInterpolator:动画从开始到结束,变化率是一个减速的过程。
——CycleInterpolator:动画从开始到结束,变化率是循环给定次数的正弦曲线。
——Anticipate/Overshoot:往起点方向偏移少量距离再开始加速动画/往终点方向偏移少量距离再结束动画。
——Bounce:弹性效果。
——AccelerateDecelerateInterpolator:动画从开始到结束,变化率是先加速后减速的过程。
——LinearInterpolator:动画从开始到结束,变化率是线性变化。

复制代码
<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切换动画效果:
  关于Activity切换动画效果,网上比较普遍的是overridePendingTransition(enterAnim, exitAnim),但这种方式其实有一些问题;例如:被打开的Activity退出时,并没有动画效果;并且,如果需要当前Activity销毁返回到前一个Activity时,当前Activity和前一个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执行的动画;
进入B时A执行的动画;
离开B返回A时A执行的动画;
离开B返回A时B执行的动画;

两个google官方参考链接:
http://developer./reference/android/R.attr.html
http://developer./reference/android/R.styleable.html#WindowAnimation

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多