Android 动画

1.动画分类

补间动画View Animation:图形变换 (包括平移、缩放、旋转、改变透明度)

分为AlphaAnimation、 TranslateAnimation、RotateAnimation、ScaleAnimation

android:duration 动画持续时间,以毫秒为单位
android:fillAfter 如果设置为true,控件动画结束时,将保持动画最后时的状态
android:fillBefore 如果设置为true,控件动画结束时,还原到开始动画前的状态
android:fillEnabled 与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
android:repeatCount 重复次数
android:repeatMode 重复类型,有reverse和restart两个值,reverse表示倒序回放,restart表示重新放一遍,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
android:interpolator 设定插值器,其实就是指定的动作效果,比如弹跳效果等,

使用 1.代码使用

AlphaAnimation alphaAnimation = new AlphaAnimation(1,0.1f);
alphaAnimation.setDuration(2000); 动画持续时间
alphaAnimation.setFillAfter(true); 动画结束后不返回起始位置
button.startAnimation(alphaAnimation);开启动画

2.xml使用 

res/anim/filename.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:repeatMode="restart">

   <translate android:fromXDelta="100" android:fromYDelta="100" android:toXDelta="120" android:toYDelta="120"/>
    <alpha android:fromAlpha="10" android:toAlpha="12" />
</set>

 java代码里加载xml文件

Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha);
button.startAnimation(animation);

帧动画Drawable Animation:播放一组图片,形成动画

AnimationDrawable的start()方法不能在Activity的onCreate方法中调运,因为AnimationDrawable还未完全附着到window上,所以最好的调运时机是onWindowFocusChanged()方法中。

使用

res/drawable/filename.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- oneshot="true" 表示动画只播放一次,播放完成后停止在最后一帧 oneshot="false" 表示动画循环播放-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
    <item android:drawable="@drawable/ic_launcher_background" android:duration="200"/>
    <item android:drawable="@drawable/ic_launcher_background" android:duration="200"/>
    <item android:drawable="@drawable/ic_launcher_background" android:duration="200"/>
    <item android:drawable="@drawable/ic_launcher_background" android:duration="200"/>

</animation-list>
//        将帧动画xml 设置为控件的背景
        button.setBackgroundResource(R.drawable.frameanmi);
//        将控件的背景转换为AnimationDrawable
        AnimationDrawable animationDrawable = (AnimationDrawable) button.getBackground();
//       开启动画
        animationDrawable.start();

属性动画 Property Animation :通过修改控件的属性值实现动画效果。

res/animator/filename.xml

<set
  android:ordering=["together" | "sequentially"]>

    <objectAnimator
        android:propertyName="string"
        android:duration="int"
        android:valueFrom="float | int | color"
        android:valueTo="float | int | color"
        android:startOffset="int"
        android:repeatCount="int"
        android:repeatMode=["repeat" | "reverse"]
        android:valueType=["intType" | "floatType"]/>

    <animator
        android:duration="int"
        android:valueFrom="float | int | color"
        android:valueTo="float | int | color"
        android:startOffset="int"
        android:repeatCount="int"
        android:repeatMode=["repeat" | "reverse"]
        android:valueType=["intType" | "floatType"]/>

    <set>
        ...
    </set>
</set>

<set>属性解释:

xml属性解释
android:ordering控制子动画启动方式是先后有序的还是同时进行。sequentially:动画按照先后顺序;together(默认):动画同时启动;

<objectAnimator>属性解释:

xml属性解释
android:propertyNameString类型,必须要设置的节点属性,代表要执行动画的属性(通过名字引用),辟如你可以指定了一个View的”alpha” 或者 “backgroundColor” ,这个objectAnimator元素没有对外说明target属性,所以你不能在XML中设置执行这个动画,必须通过调用 loadAnimator()方法加载你的XML动画资源,然后调用setTarget()应用到具备这个属性的目标对象上(譬如TextView)。
android:valueTofloat、int或者color类型,必须要设置的节点属性,表明动画结束的点;如果是颜色的话,由6位十六进制的数字表示。
android:valueFrom相对应valueTo,动画的起始点,如果没有指定,系统会通过属性的get方法获取,颜色也是6位十六进制的数字表示。
android:duration动画的时长,int类型,以毫秒为单位,默认为300毫秒。
android:startOffset动画延迟的时间,从调用start方法后开始计算,int型,毫秒为单位。
android:repeatCount一个动画的重复次数,int型,”-1“表示无限循环,”1“表示动画在第一次执行完成后重复执行一次,也就是两次,默认为0,不重复执行。
android:repeatMode重复模式:int型,当一个动画执行完的时候应该如何处理。该值必须是正数或者是-1,“reverse”会使得按照动画向相反的方向执行,可实现类似钟摆效果。“repeat”会使得动画每次都从头开始循环。
android:valueType关键参数,如果该value是一个颜色,那么就不需要指定,因为动画框架会自动的处理颜色值。有intType和floatType(默认)两种:分别说明动画值为int和float型。

  AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this,R.animator.property);
        animatorSet.setTarget(button);
        animatorSet.start();

1、ObjectAnimator:继承自ValueAnimator,允许你指定要进行动画的对象以及该对象 的一个属性。该类会根据计算得到的新值自动更新属性。大多数的情况使用ObjectAnimator就足够了,因为它使得目标对象动画值的处理过程变得足 够简单,不用像ValueAnimator那样自己写动画更新的逻辑,但是ObjectAnimator有一定的限制,比如它需要目标对象的属性提供指定 的处理方法(譬如提供getXXX,setXXX方法),这时候你就需要根据自己的需求在ObjectAnimator和ValueAnimator中看 哪种实现更方便了。

       //继承自ValueAnimator,允许你指定要进行动画的对象以及该对象 的一个属性
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(button, "customerDefineAnyThingName", 0,  1).setDuration(2000);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                
            }
        });

2、PropertyValuesHolder:多属性动画同时工作管理类。有时候我们需要同时修改多个属性,那就可以用到此类,


        PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
        PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("alpha", 0f, 2f);
        ObjectAnimator.ofPropertyValuesHolder(button, propertyValuesHolder, propertyValuesHolder1).setDuration(1000).start();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowang_lj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值