目录
ObjectAnimator是什么?
由于ObjectAnimator是派生自ValueAnimator的,所以ValueAnimator中所能使用的方法,在ObjectAnimator中都可以正常使用。
但ObjectAnimator也重写了几个方法,比如ofInt(),ofFloat()等。
传入需要移动的对象,移动方式,从什么位置开始移动,什么位置结束,调用.start()方法即可实现动画。当开始和结束位置一样时无动画效果。
动画效果设置
水平轴方向移动
setTranslationX与setTranslationY
setTranslationX(float translationX) :表示在X轴上的平移距离,以当前控件为原点,向右为正方向,参数translationX表示移动的距离。
setTranslationY(float translationY) :表示在Y轴上的平移距离,以当前控件为原点,向下为正方向,参数translationY表示移动的距离。
X轴上的平移距离
以下示例水平x轴方向移动
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", 0, 200, -200,0);
animator.setDuration(2000);
animator.start();
Y轴平移
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", 0, 200, -100,0);
animator.setDuration(2000);
animator.start();
改变透明度
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"alpha",1,0,1);
animator.setDuration(2000);
animator.start();
旋转效果
setRotationX、setRotationY与setRotation
setRotationX(float rotationX):表示围绕X轴旋转,rotationX表示旋转度数
setRotationY(rotationY):表示围绕Y轴旋转,rotationY表示旋转度数
setRotation(float rotation):表示围绕Z旋转,rotation表示旋转度数
围绕Z轴旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotation",0,180,0);
animator.setDuration(2000);
animator.start();
围绕X轴旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotationX",0,270,0);
animator.setDuration(2000);
animator.start();
围绕Y轴旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotationY",0,180,0);
animator.setDuration(2000);
animator.start();
缩放
setScaleX与setScaleY
- setScaleX(float scaleX):在X轴上缩放,scaleX表示缩放倍数
- setScaleY(float scaleY):在Y轴上缩放,scaleY表示缩放倍数
X轴缩放
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleX", 0, 3, 1);
animator.setDuration(2000);
animator.start();
Y轴缩放
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleY", 0, 3, 1);
animator.setDuration(2000);
animator.start();
ObjectAnimator监听器
使用mMenuShow控制菜单的隐藏或显示,添加动画结束监听器,改变mMenuShow值,恢复菜单项显示
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.back:
if (mMenuShow && (!isShowChannelManageItem())) {
//恢复menu界面的菜单项(设置相关控件可见)
restoreMenuItem();
} else if (mMenuShow) {
toggleMenu();
}
break;
case R.id.menu:
if (!mMenuShow) {
toggleMenu();
}
break;
case R.id.item_channel_manage:
//切换channelManage界面的菜单项(设置相关控件可见)
toggleChannelManageItem();
break;
default:
break;
}
}
private void toggleMenu() {
startTranslateAnimation(mMenuShow ? 0f : -mMenu.getRight(),
mMenuShow ? -mMenu.getRight() : 0f, mMenu, new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (mMenuShow)
restoreMenuItem();
mMenuShow = !mMenuShow;
}
});
}
private void startTranslateAnimation(float start, float end, Object target, AnimatorListener listener) {
@SuppressLint("ObjectAnimatorBinding") ObjectAnimator animator = ObjectAnimator.ofFloat(target, "translationX", start, end);
if (listener != null) animator.addListener(listener);
animator.setDuration(300);
animator.start();
}
ObjectAnimator动画原理
ObjectAnimator只负责把当前运动动画的数值传给set函数。至于set函数里面怎么来做,是我们自己的事了。
ViewGroup.LayoutParams
使用layoutParams这种方式,无动画效果,相当直接改变控件的大小布局。且此控件移动式有可能影响到其他控件的位置。
如使得viewGroup的mMenu控件在水平上移动。
ViewGroup.LayoutParams layoutParams = mMenu.getLayoutParams();
layoutParams.width = 235;
mMenu.requestLayout();
if (mMenuShow || (!mMenuShow && !isShowChannelManageItem())) restoreMenuItem();
mMenuShow = !mMenuShow;
AnimationSet
在 AnimatorSet 中设置以后,会覆盖单 ObjectAnimator 中的设置。也就是说, 如果在 AnimatorSet 中没有设置,那么以 ObjecAnimator中的设置为准;在 AnimatorSet 中设置以后, ObjectAnimator中的设置就会无效。
AnimationSet可以用作动画集合。
AnimatorSet .Builder builder= animatorSet. play(tvlBgAnimator) ;
builder.with(tvlTranslateY) ;
AnimationSet函数
//表示要播放哪个动画
public Builder play (Animator anim)
//和前面的动画一起执行
public Builder with(Animator an m)
//先执行这个动画,再执行前面的动画
public Builder before (Animator aηim)
//在执行前面的动画后才执行该动画
public Builder after(Animator anim)
//延迟n毫秒之后执行动画
public Builder after (long delay)
使用 Builder 对象逐个添加动画
AnimatorSet .Builder builder = animatorSet. play (tvlTranslateY);
builder.with(tv2TranslateY)
builder.after(tvlBgAnimator) ;
串行方式
animatorSet . play(tvlTranslateY) . with(tv2TranslateY) . after(tvlBgAnimator);
AnimationSet监听器
AnimatorSet 的监昕函数 是用来监昕 AnimatorSet的状态的,与其中的动画无关。
AnimatorSet 中并没有设置循环的函数,所以动画执行一次就结束了,永远无法执行onAnimationRepeat函数中。
public static interface AnimatorListener {
/**
*当 Animator Set 开始时调用
**/
void onAnimationStart(Animator animation) ;
/**
*当 AnimatorSet 结束时调用*/
void onAnimationEnd(Animator animation)
/**
*当 Animator Set 取消时调用
*/
void onAnimationCancel(Animator animation) ;
/**
*当 AnimatorSet 复时调用 由于 Animator Set 没有设置重复的函数,所以这个函数永远不会被调用
*/
void onAnimationRepeat(Animator animation) ;
}
添加监听器的方法
public void addListener(AnimatorListener listener) ;
setTarget(Object target )函数
只要通过 AnimatorSet setTarget() 函数设置了目标控件,那么单个动画中的目标控件都以 AnimatorSet 设置的为准