ViewPropertyAnimator
package com.zhoujian.retrofit.activity;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewPropertyAnimator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import com.zhoujian.retrofit.R;
public class AnimatorActivity extends AppCompatActivity {
//属性动画:ViewPropertyAnimator
private ViewPropertyAnimator viewPropertyAnimator;
//属性动画:ObjectAnimator
private ObjectAnimator objectAnimator;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
ImageView imageView = (ImageView) findViewById(R.id.image);
//属性动画:ViewPropertyAnimator
properAnimation(imageView);
//属性动画:ObjectAnimator
//objectAnimation(imageView);
}
/**
* 属性动画:ObjectAnimator
*
* @param imageView
*/
private void objectAnimation(ImageView imageView) {
objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 100);
//设置动画时长
objectAnimator.setDuration(500);
//设置动画重复模式
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
//设置动画是否自动取消
objectAnimator.setAutoCancel(true);
//设置动画延时执行
objectAnimator.setStartDelay(100);
//设置动画重复次数
objectAnimator.setRepeatCount(2);
//动画插值器
objectAnimator.setInterpolator(new LinearInterpolator());
//动画暂停的监听
objectAnimator.addPauseListener(new Animator.AnimatorPauseListener() {
@Override
public void onAnimationPause(Animator animation) {
}
@Override
public void onAnimationResume(Animator animation) {
}
});
//动画更新的监听
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
}
});
//动画设置监听
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
//开始动画
objectAnimator.start();
}
/**
* 属性动画:ViewPropertyAnimator
*
* @param imageView
*/
private void properAnimation(ImageView imageView) {
//获取属性动画
viewPropertyAnimator = imageView.animate();
//沿X轴,向右移动100像素
viewPropertyAnimator.translationX(100);
//设置动画时长
viewPropertyAnimator.setDuration(500);
//设置动画插值器:线性插值器,匀速
viewPropertyAnimator.setInterpolator(new AnticipateInterpolator());
//设置动画延时执行
viewPropertyAnimator.setStartDelay(100);
//设置动画更新监听
viewPropertyAnimator.setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
}
});
//动画开始时被调用
viewPropertyAnimator.withStartAction(new Runnable() {
@Override
public void run() {
}
});
//动画结束时被调用
viewPropertyAnimator.withEndAction(new Runnable() {
@Override
public void run() {
}
});
//设置动画监听
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
/**
* 动画开始
* @param animation
*/
@Override
public void onAnimationStart(Animator animation) {
}
/**
* 动画结束
* @param animation
*/
@Override
public void onAnimationEnd(Animator animation) {
}
/**
* 动画取消
* @param animation
*/
@Override
public void onAnimationCancel(Animator animation) {
}
/**
* 动画重复执行
* @param animation
*/
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
ObjectAnimator
使用方式:
如果是自定义控件,需要添加setter / getter 方法
使用ObjectAnimator.ofxxx() 创建ObjectAnimator对象
start() 方法执行动画
package com.zhoujian.retrofit.activity;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewPropertyAnimator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import com.zhoujian.retrofit.R;
public class AnimatorActivity extends AppCompatActivity {
//属性动画:ViewPropertyAnimator
private ViewPropertyAnimator viewPropertyAnimator;
//属性动画:ObjectAnimator
private ObjectAnimator objectAnimator;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
ImageView imageView = (ImageView) findViewById(R.id.image);
//属性动画:ViewPropertyAnimator
//properAnimation(imageView);
//属性动画:ObjectAnimator
objectAnimation(imageView);
}
/**
* 属性动画:ObjectAnimator
*
* @param imageView
*/
private void objectAnimation(ImageView imageView) {
objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 100);
//设置动画时长
objectAnimator.setDuration(500);
//设置动画重复模式
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
//设置动画是否自动取消
objectAnimator.setAutoCancel(true);
//设置动画延时执行
objectAnimator.setStartDelay(100);
//设置动画重复次数
objectAnimator.setRepeatCount(2);
//动画插值器
objectAnimator.setInterpolator(new LinearInterpolator());
//动画暂停的监听
objectAnimator.addPauseListener(new Animator.AnimatorPauseListener() {
@Override
public void onAnimationPause(Animator animation) {
}
@Override
public void onAnimationResume(Animator animation) {
}
});
//动画更新的监听
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
}
});
//动画设置监听
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
//开始动画
objectAnimator.start();
}
/**
* 属性动画:ViewPropertyAnimator
*
* @param imageView
*/
private void properAnimation(ImageView imageView) {
//获取属性动画
viewPropertyAnimator = imageView.animate();
//沿X轴,向右移动100像素
viewPropertyAnimator.translationX(100);
//设置动画时长
viewPropertyAnimator.setDuration(500);
//设置动画插值器:线性插值器,匀速
viewPropertyAnimator.setInterpolator(new AnticipateInterpolator());
//设置动画延时执行
viewPropertyAnimator.setStartDelay(100);
//设置动画更新监听
viewPropertyAnimator.setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
}
});
//动画开始时被调用
viewPropertyAnimator.withStartAction(new Runnable() {
@Override
public void run() {
}
});
//动画结束时被调用
viewPropertyAnimator.withEndAction(new Runnable() {
@Override
public void run() {
}
});
//设置动画监听
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
/**
* 动画开始
* @param animation
*/
@Override
public void onAnimationStart(Animator animation) {
}
/**
* 动画结束
* @param animation
*/
@Override
public void onAnimationEnd(Animator animation) {
}
/**
* 动画取消
* @param animation
*/
@Override
public void onAnimationCancel(Animator animation) {
}
/**
* 动画重复执行
* @param animation
*/
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
通用的插值器:
LinearInterpolator:线性插值器:匀速
AccelerateDecelerateInterpolator:先加速后减速
AccelerateInterpolator:持续加速
DecelerateInterpolator:持续减速
AnticipateInterpolator:先回拉一下在进行正常的动画轨迹
OvershootInterpolator:有回弹效果的运动轨迹
AnticipateOvershootInterpolator:开始前回拉,超过回弹
BounceInterpolator:弹跳效果的运动轨迹
CycleInterpolator:余弦曲线
PathInterpolator:自定义动画完成度
FastOutLinearInInterpolator:加速运动
// FastOutSlowInInterpolator:先加速再减速
// LinearOutSlowInInterpolator:持续减速
蓝枫amy 发布了272 篇原创文章 · 获赞 68 · 访问量 40万+ 关注