Android-Animator属性动画( ObjectAnimator , AnimatorSet , ValueAnimator )

本文详细介绍了Android中Animation与Animator的区别及应用,包括属性动画的实现方式、ObjectAnimator的使用方法、AnimatorSet的组合动画技巧以及PropertyValuesHolder的多属性动画设置等。此外,还探讨了动画监听、动画菜单实现及各种插值器的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.回顾

  上篇学习了 Animation 的实现方式 , 后转型了 Ubuntu 下的 Andorid Studio ;

  初次使用Android Studio 开始进行一些 快捷键的设置:

 /**
     * 这里的快捷键有:
     * alt+/  :智能提示
     * alt+enter : 错误补全
     * <p/>
     * 自定义的快捷键设置:
     * Keymap->Main Menu->Code->Completion 里面设置
     * ctrl + shift + L : 提示实现接口
     * ctrl + shift + n :提示重写的方法
     */


2.重点

  (1)Animation和 Animator

  (2)Animator 属性动画的实现方式

  (3)ValueAnimator

3.Animation 和 Animator

  上篇已经学习过有关 Animation 的先关知识,我们知道 通过 Animation 实现的动画,仅仅是 改变了控件或图片的显示效果相当于 重绘了界面 ,而其属性 没有改变;

  Animation 的局限性就在于:只是重绘了画面,而没有改变控件属性;

  示例: 通过按钮 移动图片 Animation 实现

      ImageView imageView = (ImageView) findViewById(R.id.imageView);

            /**
             *   参数: rotation
             translationX
             translationY
             */

            //Animation 方式执行动画
            TranslateAnimation animation=new TranslateAnimation(0.0f,100.0f,0.0f,100.0f);
            animation.setDuration(2000);
            animation.setFillAfter(true);
            imageView.startAnimation(animation);
   测试的话 ,可以给 图片添加一个点击事件!

4.Animator属性动画实现

  4.1 ObjectAnimator

     (1)使用方法

      ObjectAnimator.ofFloat(操作的对象,操作的动画,变化范围)
      .setDuration()
    .start();
  参数: rotation
        translationX
        translationY

     (2)示例

 

        ObjectAnimator animation=ObjectAnimator.ofFloat(imageView,"translationX",0.0f,200.0f);
            animation.setDuration(1000);
            animation.start();

     (3)多个动画 :同时执行了

      /**
             * ObjectAnimator 方式执行方式
             * 设置三个动画 同时执行了;
             */
            ObjectAnimator animation=ObjectAnimator.ofFloat(imageView,"translationX",0.0f,200.0f);
            animation.setDuration(1000);
            animation.start();

            ObjectAnimator animation1=ObjectAnimator.ofFloat(imageView,"translationY",0.0f,200.0f);
            animation1.setDuration(1000);
            animation1.start();

            ObjectAnimator animation2=ObjectAnimator.ofFloat(imageView,"rotation",0.0f,200.0f);
            animation2.setDuration(1000);
            animation2.start();

   4.2 AnimatorSet + Animator 实现

    (1)使用方式

  多个动画集合
  AnimatorSet  set=new AnimatorSet();
   //同时执行
   .playTogether(属性集合/ObjectAnimator);
   //依次执行
   .playSequentially(ObjectAnimator items);
  .setDuration(时间);
  .start();


  顺序控制:
  .play().with().after().before();

    (2)实现

 

 /**
             * AnimatorSet
             * 控制 多个动画的运行
             */
            ObjectAnimator animation=ObjectAnimator.ofFloat(imageView,"translationX",0.0f,200.0f);

            ObjectAnimator animation1=ObjectAnimator.ofFloat(imageView,"translationY",0.0f,200.0f);

            ObjectAnimator animation2=ObjectAnimator.ofFloat(imageView,"rotation",0.0f,200.0f);

            AnimatorSet set=new AnimatorSet();
            //多个动画同时执行
            // set.playTogether(animation,animation1,animation2);
            //多个动画按放入位置执行
            // set.playSequentially(animation,animation1,animation2);
            //顺序控制执行
            set.play(animation1).with(animation);
            set.play(animation2).after(animation);

            set.setDuration(1000);
            set.start();

  4.3 PropertyValuesHolder + Animator 

    (1)PropertyValuesHolder

  多个属性集合holder
  PropertyValuesHolder.pfFloat();

    (2)示例

    /**
             * PropertyValuesHolder
             * 属性集合
             * 可以方便调用使用;
             */
            PropertyValuesHolder holder = PropertyValuesHolder.ofFloat("translationX", 0.0f, 200.0f);
            PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("translationY", 0.0f, 200.0f);
            //通过ofPropertyValuesHolder 实现
            ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView, holder, holder1);
            objectAnimator.setDuration(1000);
            objectAnimator.start();


 4.4 监听事件

       //过程监听
   new AnimatorListener()
//某个过程监听
  new AnimatorListenerAdapter()


 4.5 动画菜单实现

  private int[] imgIds = {R.id.icon_down, R.id.icon_error, R.id.icon_left, R.id.icon_light,
            R.id.icon_location, R.id.icon_none, R.id.icon_true};

    private ImageView iv_up;
    private List<ImageView> imageViews = new ArrayList<ImageView>();

    //控制按钮
    private boolean flag = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_id = (Button) findViewById(R.id.btn_id);
        tv = (TextView) findViewById(R.id.tv_show);
        btn_id.setOnClickListener(new btnidListener());

        iv_up = (ImageView) findViewById(R.id.icon_up);
        iv_up.setOnClickListener(this);


        //初始化ImageView
        for (int id : imgIds) {
            ImageView imageView = (ImageView) findViewById(id);
            //给每个菜单设置监听事件
            imageView.setOnClickListener(this);
            imageViews.add(imageView);

        }

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.icon_up:
                if (flag) {
                    startAnim();
                    flag = false;
                } else {
                    closeAnim();
                    flag = true;
                }

                break;
            default:
                Toast.makeText(this, "点击的图片ID为" + v.getId(), Toast.LENGTH_SHORT).show();
                break;

        }

    }

    /**
     * 创建展开动画
     */
    private void startAnim() {

        for (int i = 0; i < imageViews.size(); i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(imageViews.get(i), "translationY", 0, i * 100f);
            animator.setDuration(500);
            //设置延时 一个一个出来
            //不设置 同时出来
            //设置插入器 Android 自带有插入器
            animator.setInterpolator(new BounceInterpolator());
            animator.setStartDelay(i * 300);
            animator.start();
        }

    }

    /**
     * 创建收回动画
     */
    private void closeAnim() {
        for (int i = 0; i < imageViews.size(); i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(imageViews.get(i),
                    "translationY", i * 100f, 0);
            animator.setDuration(500);
            //设置延时 一个一个出来
            //不设置 同时出来
            //设置插入器 Android 自带有插入器
            animator.setInterpolator(new BounceInterpolator());
            animator.setStartDelay(i * 300);
            animator.start();
        }
    }

    4.6 插值器

(1)AccelerateDecelerateInterpolator
  :开始与结束的地方速率改变比较慢,在中间的时候加速


  (2)AccelerateInterpolator
:开始的地方速率改变比较慢,然后开始加速


(3) AnticipateInterpolator
:开始的时候向后然后向前甩


(4)AnticipateOvershootInterpolator
:开始的时候向后然后向前甩一定值后返回最后的值


(5)LinearInterpolator
:以常量速率改变


  (6)DecelerateInterpolator
:在开始的地方快然后慢


  (7)BounceInterpolator
:动画结束的时候弹起


(8) OvershootInInterpolator
:向前甩一定值后再回到原来位置


(9) CycleInterpolator
:循环播放特定的次数,速率改变沿着正弦曲线



5.ValueAnimator 

  (1)ObjectAnimator继承自ValueAnimator

  是什么?
  数值产生器;
  怎么用?
  ValueAnimator.ofInt();
  AnimatorUpdateListener


  ValueAnimator.ofObject();
  TypeEvaluator<pointF>


  (2)示例 : 计时器

            //ValueAnimator
            ValueAnimator valueAnimator=ValueAnimator.ofInt(0,100);
            valueAnimator.setDuration(5000);
            //添加监听事件
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    btn_id.setText(animation.getAnimatedValue().toString());
                }
            });
            valueAnimator.start();

6.总结

  从Eclipse 转过来的 ,现在很喜欢 AS ; 


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值