Android valueAnimator和ObjectAnimator浅谈(一)

public class MainActivity extends AppCompatActivity {

TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView= (TextView) findViewById(R.id.tv);

    /**
     * valueAnimator 动画类
     * ValueAnimator是整个属性动画机制当中最核心的一个类,
     * 属性动画的运行机制是通过不断地对值进行操作来实现的,
     * 而初始值和结束值之间的动画过渡就是由ValueAnimator这个类来负责计算的。
     * 它的内部使用一种时间循环的机制来计算值与值之间的动画过渡,
     * 我们只需要将初始值和结束值提供给ValueAnimator,并且告诉它动画所需运行的时长,
     * 那么ValueAnimator就会自动帮我们完成从初始值平滑地过渡到结束值这样的效果
     */
    ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,1);
    valueAnimator.setDuration(3000);
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            //显示出:0-1之间值的,每个值代表一个动画的一帧
            System.out.println("============="+animation.getAnimatedValue());
        }
    });

   //        valueAnimator.setStartDelay(1000);//动画播放延迟的时间
   //        valueAnimator.setRepeatCount(3);//动画模式播放的次数

   //循环模式包括RESTART和REVERSE两种,分别表示重新播放和倒序播放的意思
   //        valueAnimator.setRepeatMode(ValueAnimator.RESTART);

   valueAnimator.start();

注:ValueAnimator.ofFloat(0,1),为什么就是返回的(0,1)之间的数据呢?那,我要怎么才能返回自己需要的数据呢?这个就是涉及到TypeEvaluator类了,后续会着重讲到这个接口。这是个重点,切记切记。


    /**
     * ObjectAnimator 动画类
     * 继承于valueAnimator 类
     * ofFloat( Object target, String propertyName, float... values)的3个参数
     * target:目标,例如:控件
     * propertyName:属性,目标所具有的属性
     * values:该属性对应的一系列值
     */

    //该textView 先淡出,在显示。因为设置的是:alpha属性。
    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(textView,"alpha",1f,0f,1f);
    objectAnimator.setDuration(6000);
    objectAnimator.start();

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //该textView旋转,先旋转360后,在旋转回来
    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(textView,"rotation",0f,360f,0f);
    objectAnimator.setDuration(6000);
    objectAnimator.start();

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //获取当前控件的水平位置
    float currentX=textView.getTranslationX();
    //该textView从当前位置,先向左平移500个单位,在平移回到当前位置。
    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(textView,"translationX",currentX,-500,currentX);
    objectAnimator.setDuration(5000);
    objectAnimator.start();

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // 该textView横向或者纵向拉伸
    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(textView,"scaleY",1f,3f,1f);
    objectAnimator.setDuration(3000);
    objectAnimator.start();

    /**
     * 组合动画
     *
     *  实现组合动画功能主要需要借助AnimatorSet这个类,这个类提供了一个play()方法,
     *  如果我们向这个方法中传入一个Animator对象(ValueAnimator或ObjectAnimator)将会返回一个AnimatorSet.Builder的实例,
     *  AnimatorSet.Builder中包括以下四个方法:
     *  after(Animator anim)   将现有动画插入到传入的动画之前执行
     *  after(long delay)   将现有动画延迟指定毫秒后执行
     *  before(Animator anim)   将现有动画插入到传入的动画之后执行
     *  with(Animator anim)   将现有动画和传入的动画同时执行
     */

    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(textView,"alpha",1f,0f,1f);
    ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(textView,"rotation",0f,360f,0f);
    float currentX=textView.getTranslationX();
    ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(textView,"translationX",currentX,-500,currentX);
    ObjectAnimator objectAnimator3=ObjectAnimator.ofFloat(textView,"scaleY",1f,3f,1f);
    ObjectAnimator objectAnimator4=ObjectAnimator.ofFloat(textView,"scaleX",1f,3f,1f);

    AnimatorSet animatorSet=new AnimatorSet();
    animatorSet.play(objectAnimator)
            .after(objectAnimator2)
            .with(objectAnimator1)
            .with(objectAnimator3)
            .with(objectAnimator4);

    animatorSet.setDuration(5000);
    animatorSet.start();

    /**
     * Animator监听器
     * Animator类当中提供了一个addListener()方法,这个方法接收一个AnimatorListener,我们只需要去实现这个AnimatorListener就可以监听动画的各种事件了。
     */

    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(textView,"alpha",1f,0f,1f);
    objectAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            //动画开始
        }

        @Override
        public void onAnimationEnd(Animator animation) {
           //动画结束
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            //动画取消
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
           //动画重复
        }
    });

    /**
     * 是也许很多时候我们并不想要监听那么多个事件,可能我只想要监听动画结束这一个事件,
     * 那么每次都要将四个接口全部实现一遍就显得非常繁琐。没关系,为此Android提供了一个适配器类,
     * 叫作AnimatorListenerAdapter,使用这个类就可以解决掉实现接口繁琐的问题了.
     */
    objectAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
        }
    });
}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值