Android-属性动画Animator

本文介绍了Android的属性动画系统,重点讲解了ObjectAnimator、动画监听事件和ValueAnimator的使用。ObjectAnimator提供丰富的动画控制,ValueAnimator作为其基类,通过数值控制动画,可以实现灵活的动画效果。同时,文章总结了常用属性、方法和类,帮助开发者更好地掌握Android动画的运用。

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

由于Animation在功能上具有一定局限性,谷歌推出了Animator。

1. ObjectAnimator

  • ofFloat方法
// 比如设置一个ImageView平移的动画,时间1s
ObjectAnimator.ofFloat(imageView, "rotation", 0, 360f).setDuration(1000).start();
// 多个动作同时执行,是个异步的过程
ObjectAnimator.ofFloat(imageView, "rotation", 0, 360f).setDuration(1000).start();
ObjectAnimator.ofFloat(imageView, "translationX", 0, 200f).setDuration(1000).start();
ObjectAnimator.ofFloat(imageView, "translationY", 0, 200f).setDuration(1000).start();
  • ofPropertyValuesHolder方法
// PropertyValuesHolder对动画进行了一些优化,下面的代码跟前面效果一致。
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0, 360f);
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX", 0, 200f);
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY", 0, 200f);
ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2, p3).setDuration(1000).start();
  • AnimatorSet
// 效果跟前面一致
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationX", 0, 200f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "translationY", 0, 200f);
AnimatorSet set = new AnimatorSet();
set.playTogether(animator1, animator2, animator3);
set.setDuration(1000);
set.start();

AnimatorSet还提供了更加丰富的动画控制效果,比如:

// 顺序执行三个动作
set.playSequentially(animator1, animator2, animator3);
// 先同时执行后面两个动作,再执行第一个旋转的动作
set.play(animator2).with(animator3);
set.play(animator1).after(animator2);

2. 动画的监听事件

  • 方式1:
    动画调用addListener(Animator.Animator.Listener listener)方法,为动画添加监听器,如果这些方法,我们就可以在动画的各个阶段执行一些逻辑。
animator.setListener(new Animator.AnimatorListener() {
	@Override 
	pubic void onAnimationStart(Animator animation) {
	}
	@Override 
	public void onAnimationEnd(Animator animation) {
	}
	@Override 
	public void onAnimationCancel(Animator animation) {
	}
	@Override 
	public void onAnimationRepeat(Animator animation) {
	}
}
  • 方式2:
// 只实现其中一部分方法
animator.addListener(new AnimatorListenerAdapter() {
	@Override 
	public void onAnimationEnd(Animator animation) {
		super.onAnimationEnd(animation);
		Toast.makeText(MyActivity.this, "anim end", Toast.LENGTH_SHORT).show();
	}
}

3. ValueAnimator

  • ValueAnimator是什么?
    通过数值来控制动画,它是ObjectAnimator的基类。
  • ValueAnimator怎么用?
Button button = view;
ValueAnimator animator = ValueAnimator.ofInt(0, 100);
animator.setDuration(5000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
	@Override 
	public void onAnimationUpdate(ValueAnimator animation) {
		Integer value = (Integer) animation.getAnimationValue();
		// 实现一个计时器的动画效果
		button.setText("" + value);
	}
}
animator.start();

4. 总结

4.1 常用属性

  • translationXtranslationY
  • rotationrotationXrotationY
  • scaleXscaleY
  • XY
  • alpha

4.2 常用方法和类

  • ValueAnimator 数值发生器,可以实现很多很灵活的动画效果
  • ObjectAnimator ValueAnimator的一个子类,封装了ValueAnimator,可以更加轻松地使用
  • AnimatorUpdateListener
  • AnimatorListenerAdapter 做监听事件
  • PropertyValuesHolder
  • AnimatorSet 控制动画集合的显示效果、顺序以及流程控制等
  • TypeEvaluators
  • Interpolators 值计算器和插入器,用来控制产生数值的变化规律、变化状态等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值