属性动画常用属性和方法

属性动画

  • 实现Animation框架的的功能
  • 属性动画常用属性演示
  • 动画的监听事件

ImageView imageview=(ImageView)findViewById(R.id.img);
ObjectAnimator.ofFloat(imageView,”translationX”,0F,200F).setDuration(200).start();

参数一为控件,参数二为移动的方向,参数移动的开始和结束

ImageView imageview=(ImageView)findViewById(R.id.img);
ObjectAnimator.ofFloat(imageView,”rotation”,0F,360F).setDuration(200).start();

  • 旋转360

只要属性有get和set方法就可以通过属性动画操作它

同时执行,多个动作一起执行

ImageView imageview=(ImageView)findViewById(R.id.img);

ObjectAnimator.ofFloat(imageView,"translationX",0F,200F).setDuration(200).start();//平移X
ObjectAnimator.ofFloat(imageView,"rotation",0F,360F).setDuration(200).start();//旋转
ObjectAnimator.ofFloat(imageView,"translationY",0F,200F).setDuration(200).start();//平移Y

ObjectAnimator.ofFloat(imageView,"alpha",0F,1F).setDuration(200).start();//透明度渐变

三个动画可使用类PropertyValuesHolder融合在一起,更加优化,性能提升

PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation",0F,360F);
PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX",0F,200F);
PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY",0F,200F);
ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();

也可执行AnimatorSet把所有动画添加到一起播放

AnimatorSet set=new AnimatorSet();
ObjectAnimator am1=ObjectAnimator.ofFload(imageView,"rotation",0F,360F);

ObjectAnimator am2=ObjectAnimator.ofFloat(imageView,"translationX",0F,200F);

ObjectAnimator am2=ObjectAnimator.ofFloat(imageView,"translationY",0F,200F);
set.playTogether(an1,an2,an3);
set.setDuration();
set.start();

把这句set.playTogether(an1,an2,an3);
变为

set.playSequentially(an1,an2,an3);

可实现按顺序播放动画

实现同时播放和后播放的效果:

//先执行an2和an3,后执行an1
set.play(an2).with(an3);
set.play(an1).after(an2);

动画的监听事件

ObjectAnimator an=ObjectAnimator.ofFloat(imageView,"alpha",0F,1F).setDuration(200).start();//旋转

想实现动画结束的监听只要实现

an.addListener(new Animator.AnminatorListener(){
    //实现里面所有的方法


});

但是如果我们只需要实现动画里面的某个方法,可以使用监听AnimatorListenerAdapter

an.addListener(new AnimatorListenerAdapter(){

    //只需要实现动画结束的方法

    public void onAnimationEnd(Animator animation){
        super.onAnimationEnd(animation);

        //实现动画结束的逻辑
    }
});

实现插值器

animation.setInterpolator(new BounceInterpolator());

BounceInterpolator只是其中一种插值器效果

ValueAnimator

使用ValueAnimator实现一个计时器的效果:

//点击事件中实现计时器
public void click(View view){
    final Button but=(Button)view;
    ValueAnimator anim=ValueAnimator.ofInt(0,100);
    anim.setDuration(5000);
    anima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){

        public void onAnimationUpdate(ValueAnimator animation){
            Integer value=(Integer)animation.getAnimatedValue();
            but.settext(value+"");
        }

    });
    anim.start();
}

总结属性-常用

  • translationX\translationY
  • rotation、rotationX\rotationY
  • scaleX\scaleY
  • X/Y
  • alpha

总结–常用方法、类

  • ValueAnimator
  • ObjectAnimator
  • AnimatorUpdateListener
  • AnimatorListenerAdapter
  • PropertyValuesHolder
  • AnimatorSet
  • TypeEvaluators
  • Interpolators
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值