Android群英传——第七章Animator属性动画

Animator

顾名思义:动画制作者,用来生成、管理动画。
属性动画的运行机制是通过不断地对值进行操作来实现的,并且它常常与Animation配合实现动画效果

subClass:

有两个接口:
interface Animator.AnimatorListener:监听动画的监听器(如启动、重复、结束)
interface Animator.AnimatorPauseListener:监听动画暂停或是重新开始的监听器

来看一下作为一个Animator拥有了哪些方法(这些方法都是动作):

  • void start():开始动画
  • void end():结束动画
  • void pause():暂停动画
  • void cancel():取消动画
  • Animator clone():复制这个动画生成一个新的动画
  • void setTarget(Object target):设置动画的目标对象

动画时间相关

  • abstract long getDuration():获取动画的时间
  • abstract void setStartDelay(long startDelay):设置动画延迟start()
  • abstract long getStartDelay()
  • long getTotalDuration():获取动画的总时间,包括开始延迟、重复、动画序列(运行时间)
  • abstract void setInterpolator(TimeInterpolator value):设置TimeInterpolator对象,该对象主要实现非匀速运动
  • TimeInterpolator getInterpolator()

还有一些判断:

  • abstract boolean isRunning()
  • boolean isPaused()
  • boolean isStarted()

监听器相关:

  • void addListener(Animator.AnimatorListener listener)
  • void addPauseListener(Animator.AnimatorPauseListener listener)
  • ArrayList<Animator.AnimatorListener> getListeners()
  • void removeListener(Animator.AnimatorListener listener)
  • void removePauseListener(Animator.AnimatorPauseListener listener)
  • void removeAllListeners()

其他:

  • void setupEndValues()
  • void setupStartValues()

ValueAnimator

责任:
它的内部使用一种时间循环的机制来计算值与值之间的动画过渡,我们只需要将初始值和结束值提供给ValueAnimator,
并且告诉它动画所需运行的时长,那么ValueAnimator就会自动帮我们完成从初始值平滑地过渡到结束值这样的效果。
如果我们不想动画匀速运行:就需要AccelerateDecelerateInterpolator这个类了。

创建Animator动画

<animator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType"
    android:repeatCount="1"
    android:repeatMode="reverse"/>

上述动画就是一个属性动画,动画事件为1000ms,value 从 1 变化到 0;并且value是float类型,重复次数1次,重复模式:REVERSE

重复模式:
- int INFINITE:无限循环
- int RESTART:重新播放
- int REVERSE:倒序播放

我们也可以用Java来实现:

ValueAnimator anim = ValueAnimator.ofFloat(1f, 0f);  
anim.setDuration(1000);  
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setRepeatCount(1);
anim.start();  

常用方法:
- static ValueAnimator ofFloat(float... values):注意参数,…,我们想从0->1->2->0可以这么写ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f,2f, 0f);
- static ValueAnimator ofInt(int... values)
- static ValueAnimator ofInt(int... values):厉害了!我的哥!颜色也可以搞

ObjectAnimator(ValueAnimator子类)

这才是我么需要的类,听名字就知道谁都可以搞,不仅限于Value
可以直接对任意对象的任意属性进行动画操作

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueTo="200"
    android:valueType="floatType"
    android:propertyName="y"
    android:repeatCount="1"
    android:repeatMode="reverse"/>

Java:
给一个TextView对象设置透明度动画,我们可以这么设置:

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  //第三个参数后就是不固定的长度了
animator.setDuration(5000);  
animator.start();  

关于第二个参数一定要注意,ObjectAnimator内部的工作机制是通过寻找特定属性的get和set方法,然后通过方法不断地对值进行改变,
所以一定要是控件的属性,并且具备set/get方法。

常用的属性
  • translationX和translationY,控制View从布局容器的左上角坐标的偏移位置
  • rotation、rotationX、rotationY,控制View对象绕支点进行2d,3d的旋转
  • scaleX、scaleY,控制View对象围绕它的支点进行2D、3D旋转
  • pivotX和pivotY,控制View对象的支点位置,围绕这个支点进行旋转或者缩放变换处理。默认情况下,该支点的位置就是View对象的中心点
  • x,y,描述了View对象在容器中的最终位置,他是最初左上角坐标和translationX、translationY值的累和
  • alpha,表示View的透明度。0 ~ 1
通过包装类给属性添加get和set方法
class WrapperView{
    private View mTarget;
    public WrapperView(View target){
        mTarget = target;
   }
   public int getWidth(){
        return mTarget.getLayoutParams().width ;
   }
   public void setWidth(int width){
        mTargetView.getLayoutParams().width = width;
        mTargetView.requestLayout();
  }
}

使用:

WrapperView wrapper = new WrapperView(View);
ObjectAnimator.ofInt(wrapper,"width",500).setDuration(1 * 1000).start();

PropertyValuesHolder

针对同一个动画需要多个View属性时需要此类。
比如,在缩放时,需要同时对x,y轴进行缩放:

PropertyValuesHolder pvh1 = PropertyValuesHolder.ofFloat("translationX", 300f);
PropertyValuesHolder pvh2 = PropertyValuesHolder.ofFloat("scaleX", 1f,0,1f);
PropertyValuesHolder pvh3 = PropertyValuesHolder.ofFloat("scaleX", 1f,0,1f);
ObjectAnimator.ofProperty(view, pvh1, pbh2, pvh3).setDuration(1000).start();

组合动画AnimatorSet

独立的动画能够实现的视觉效果毕竟是相当有限的,因此将多个动画组合到一起播放就显得尤为重要。
我们向这个方法中传入一个Animator对象(ValueAnimator或ObjectAnimator)将会返回一个AnimatorSet.Builder的实例,AnimatorSet.Builder中包括以下四个方法:
- after(Animator anim) 将现有动画插入到传入的动画之后执行
- after(long delay) 将现有动画延迟指定毫秒后执行
- before(Animator anim) 将现有动画插入到传入的动画之前执行
- with(Animator anim) 将现有动画和传入的动画同时执行

当然了,肯定也少不了xml的:

<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:ordering="sequentially" >  

    <objectAnimator  
        android:duration="2000"  
        android:propertyName="translationX"  
        android:valueFrom="-500"  
        android:valueTo="0"  
        android:valueType="floatType" >  
    </objectAnimator>  

    <set android:ordering="together" >  
        <objectAnimator  
            android:duration="3000"  
            android:propertyName="rotation"  
            android:valueFrom="0"  
            android:valueTo="360"  
            android:valueType="floatType" >  
        </objectAnimator>  

        <set android:ordering="sequentially" >  
            <objectAnimator  
                android:duration="1500"  
                android:propertyName="alpha"  
                android:valueFrom="1"  
                android:valueTo="0"  
                android:valueType="floatType" >  
            </objectAnimator>  
            <objectAnimator  
                android:duration="1500"  
                android:propertyName="alpha"  
                android:valueFrom="0"  
                android:valueTo="1"  
                android:valueType="floatType" >  
            </objectAnimator>  
        </set>  
    </set>  

</set>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值