- 属性动画的常用设置
- ValueAnimator
- ObjectAnimator
- 关键帧
- 插值器和估值器
属性动画的常用设置
//设置属性动画持续时间
animator.setDuration(2000);
//设置属性插值器
animator.setInterpolator(new AccelerateInterpolator());
//设置属性动画重复播放模式
animator.setRepeatMode(ValueAnimator.REVERSE);
//设置属性动画重复播放次数
animator.setRepeatCount(0);
//设置属性动画延时播放的时间
animator.setStartDelay(0);
//设置属性动画估值器,用于控制最终属性值(API22)
animator.setCurrentFraction(0.5f);
//设置当前播放时间,其值在Duration范围之内
animator.setCurrentPlayTime(1000);
//设置属性动画估值器,用于控制最终属性值
animator.setEvaluator(new IntEvaluator());
//设置属性动画监听
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i(TAG,animation.getAnimatedValue() + "");
//
}
});
//...
ValueAnimator
// The time interpolator to be used if none is set on the animation
private static final TimeInterpolator sDefaultInterpolator =
new AccelerateDecelerateInterpolator();
void init() {
if (mEvaluator == null) {
// We already handle int and float automatically,but not their Object
// equivalents
mEvaluator = (mValueType == Integer.class) ? sIntEvaluator :
(mValueType == Float.class) ? sFloatEvaluator :
null;
}
if (mEvaluator != null) {
// KeyframeSet knows how to evaluate the common types - only give it a custom
// evaluator if one has been set on this class
mKeyframes.setEvaluator(mEvaluator);
}
}
使用代码创建
private void translation(){
ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
valueAnimator.setDuration(2000);
valueAnimator.setInterpolator(new AccelerateInterpolator());
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
valueAnimator.setRepeatCount(0);
valueAnimator.setStartDelay(0);
// valueAnimator.setCurrentFraction(0.5f);
// valueAnimator.setCurrentPlayTime(1000);
valueAnimator.setEvaluator(new IntEvaluator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i(TAG,animation.getAnimatedValue() + "");
int x = (int) animation.getAnimatedValue();
ivImage.setTranslationX(x);
ivImage.setTranslationY(x);
}
});
valueAnimator.start();
}
使用xml创建
<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:valueFrom="0"
android:valueTo="100"
android:valueType="intType"
android:duration="2000"
android:startOffset ="0"
android:repeatMode = "reverse"
android:repeatCount = "0"
android:interpolator = "@android:anim/accelerate_interpolator">
</animator>
private void translation(){
ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this,R.animator.test_animator);
animator.setTarget(ivImage);
animator.start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i(TAG,animation.getAnimatedValue() + "");
int x = (int) animation.getAnimatedValue();
ivImage.setTranslationX(x);
ivImage.setTranslationY(x);
}
});
}
测试效果
ObjectAnimator
private void alpha(){
ObjectAnimator animator = ObjectAnimator.ofFloat(ivImage,"alpha",1f,1f);
animator.setDuration(3000);
//其他属性动画设置
//...
animator.start();
}
属性 |
作用 |
对应方法 |
Alpha |
控制View的透明度 |
setAlpha |
TranslationX |
控制X方向的位移 |
setTranslationX |
TranslationY |
控制Y方向的位移 |
setTranslationY |
ScaleX |
控制X方向的缩放倍数 |
setScaleX |
ScaleY |
控制Y方向的缩放倍数 |
setScaleY |
Rotation |
控制以屏幕方向为轴的旋转度数 |
setRotation |
RotationX |
控制以X轴为轴的旋转度数 |
setRotationX |
RotationY |
控制以Y轴为轴的旋转度数 |
setRotationY |
关键帧
/**
* 关键帧的使用
*/
private void keyFrame(){
Keyframe keyframe1 = Keyframe.ofFloat(0,0);
Keyframe keyframe2 = Keyframe.ofFloat(0.25f,300);
//每个KeyFrame可设置自己的插值器
keyframe2.setInterpolator(new AccelerateInterpolator());
Keyframe keyframe3 = Keyframe.ofFloat(0.75f,100);
Keyframe keyframe4 = Keyframe.ofFloat(1,400);
PropertyValuesHolder holder = PropertyValuesHolder.ofKeyframe("translationX",keyframe1,keyframe2,keyframe3,keyframe4);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(ivImage,holder);
animator.setDuration(3000);
animator.start();
}
插值器和估值器
- 插值器(TimeInterpolator) 表示的是整个动画期间动画的变化规律,如加速、减速等。
- 估值器(TypeEvaluator)表示的是在整个动画期间各时刻属性值的具体变化。
/**
* 自定义估值器
* Point封装了坐标x和y
*/
public class SineTypeValue implements TypeEvaluator<Point> {
@Override
public Point evaluate(float fraction,Point startValue,Point endValue) {
//y = sinA
float distance = fraction * (endValue.getX() - startValue.getX());
float x = startValue.getX() + distance;
float y = startValue.getY() + (float) Math.sin(distance / 100 * Math.PI) * 100;
Point point = new Point();
point.setX(x);
point.setY(y);
return point;
}
}
/**
* 自定义估值器的使用
* 正弦运动的估值器
*/
private void sina(){
Point startPoint = new Point(ivImage.getX(),ivImage.getY());
Point endPoint = new Point(ivImage.getX()+500,ivImage.getY());
ValueAnimator valueAnimator = ValueAnimator.ofObject(new SineTypeValue(),startPoint,endPoint);
valueAnimator.setDuration(5000);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i(TAG,animation.getAnimatedValue() + "");
Point point = (Point) animation.getAnimatedValue();
ivImage.setX(point.getX());
ivImage.setY(point.getY());
}
});
valueAnimator.start();
}