一、前言
属性动画可以实现补间动画的所有效果,且还能实现补间动画实现不了的效果,接下来就来学习一下属性动画。
二、ValueAnimator的使用
//创建一个从1到100的整型动画
//获取屏幕宽度
int widthPixels = getResources().getDisplayMetrics().widthPixels;// ;
//创建一个从1到100的整型动画
ValueAnimator valueAnimator = ValueAnimator.ofInt(- (int) mTvHello.getPaint().measureText(getResources().getString(R.string.app_name)),widthPixels);
//给动画设置一个线性的插值器
valueAnimator.setInterpolator(new LinearInterpolator());
//设置动画的执行时长
valueAnimator.setDuration(4000);
// 设置动画的重复次数
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
//设置动画的重复模式,REVERSE表示逆向重复,RESTART表示连续重复
valueAnimator.setRepeatMode(ValueAnimator.RESTART);
//给动画设置一个估值器
valueAnimator.setEvaluator(new IntEvaluator());
//设置动画的当前时间,取值范围为0~duration
// valueAnimator.setCurrentPlayTime(1000);
//设置动画的时间因子
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
// valueAnimator.setCurrentFraction(0.5f);
// }
//给动画设置启动延迟,例如当前就是延迟1s启动动画
valueAnimator.setStartDelay(1000);
//给动画设置当前动态监听
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedFraction = animation.getAnimatedFraction();
Integer integer = (Integer) animation.getAnimatedValue();
mTvHello.setTranslationX(integer.floatValue());
Log.d(TAG, "onAnimationUpdate: integer:" + integer + " animatedFraction" + animatedFraction);
}
});
//给动画设置状态监听
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
//动画开始
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
//动画结束
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
//取消动画
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
//重复动画
Log.d(TAG, "onAnimationRepeat: ");
}
});
//启动动画
valueAnimator.start();
通过代码可以看到,ValueAnimator并不能直接让某个控件平移、缩放、渐变、旋转,而是它能在一段时间内动态的计算出一些值,要实现动画效果,那么可以将控件的属性设置为该值,当这些属性连续变化时,就能出现动画效果。以上代码的动画效果图如下:
三、ObjectAnimator的使用
- 渐变
ObjectAnimator animator = ObjectAnimator.ofFloat(mTvHello, "Alpha", 1.0f, 0f);
animator.setDuration(3000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float animatedValue = (Float) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+animatedValue);
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
- 缩放
ObjectAnimator animator = ObjectAnimator.ofFloat(mTvHello, "ScaleX", 1.0f, 0f);
animator.setDuration(3000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float animatedValue = (Float) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+animatedValue);
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(mTvHello, "ScaleY", 1.0f, 0f);
animator.setDuration(3000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float animatedValue = (Float) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+animatedValue);
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
- 平移
ObjectAnimator animator = ObjectAnimator.ofFloat(mTvHello, "TranslationX", 0.0f, getResources().getDisplayMetrics().widthPixels/2);
animator.setDuration(3000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float animatedValue = (Float) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+animatedValue);
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
ObjectAnimator animator = ObjectAnimator.ofFloat(mTvHello, "TranslationY", 0.0f, getResources().getDisplayMetrics().widthPixels/2);
animator.setDuration(3000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float animatedValue = (Float) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+animatedValue);
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(mTvHello, "Rotation", 0.0f, 360f);
animator.setDuration(3000);
animator.setInterpolator(new LinearInterpolator());
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.d(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float animatedValue = (Float) animation.getAnimatedValue();
Log.d(TAG, "onAnimationUpdate: "+animatedValue);
}
});
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.start();