简介
- 3.0后
- AnimationSet, ObjectAnimator 类
- ObjectAnimator类动画时直接改变其相关属性
- View改变,响应点击的位置一样改变
例子
ObjectAnimator animator = ObjectAnimator.ofFloat(
view, //目标view
"translationX", //属性
300) //属性值
animator.setDuration(300)
animator.start()
属性值:
translationX, translationY
rotation, rotationX, rotationY
scaleX, scaleY
pivotX, pivotY
x,y
alpha
监听事件:
addUpdateListener
addListener(new AnimatorListener(){}
属性集
PropertyValuesHolder实现:
PropertyValuesHolder pv1 = PropertyValuesHolder.ofFloat("translationX", 300f)
PropertyValuesHolder pv2 = PropertyValuesHolder.ofFloat("scaleX", 1f,0.1f)
PropertyValuesHolder pv3 = PropertyValuesHolder.ofFloat("alpha", 0.5f)
ObjectAnimator.ofPropertyValuesHolder (view, pv1,pv2,pv3).setDuration(1000).start()
AnimatorSet实现:
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 300)
ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleX", 2.0f,0.1f)
ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 0.5f)
AnimatorSet set = new AnimatorSet()
set.setDuration(1000)
set.playTogether(animator,animator1,animator2)
set.start()
XML实现
<objectAnimator xmlns:android="..."
android:duration="1000"
android:propertyName="scaleX"
android:valueFrom="1.0"
android:valueTo="2.0"
android:valueType="floatType">
</objectAnimator>
使用
Animator anim=AnimatorInflater.loadAnimator(this, R.animator.scalex)
anim.setTarget(view)
anim.start()