1、平移动画
//view从原始位置下落到屏幕底部,弹球落地效果
public void translationAnimator(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(
view, "translationY", 0f, height);
//animator.setInterpolator(new LinearInterpolator());//匀速(默认)
//animator.setInterpolator(new DecelerateInterpolator());//减速
//animator.setInterpolator(new AccelerateInterpolator());//加速
animator.setInterpolator(new BounceInterpolator());//弹球落地效果
//animator.setRepeatCount(-1);//设置动画重复次数,-1表示无限
//animator.setRepeatMode(ValueAnimator.RESTART);//设置动画循环模式
animator.setDuration(1000);
animator.start();
}
2、缩放动画
//view从原始尺寸扩大为原来的2倍
public void scaleAnimator(View v) {
ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 2f);
ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 2f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(1000);
//animatorSet.playTogether(animatorX,animatorY);//两个动画同时开始
animatorSet.play(animatorX).with(animatorY);//两个动画同时开始
animatorSet.start();
}
3、旋转动画:
(1)view以中心点为轴点顺时针(逆时针是-360)旋转360度,无限循环
//view以中心点为轴点顺时针(逆时针是-360)旋转360度,无限循环
public void rotateAnimator(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(-1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.start();
}
(2)view以左下角为轴点顺时针(逆时针是-360)旋转360度,无限循环
//view以左下角为轴点顺时针(逆时针是-360)旋转360度,无限循环
public void rotateAnimator(View v) {
//设置轴点为vie的左下角(默认为view的中心点)
view.setPivotX(0);
view.setPivotY(view.getHeight());
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(-1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.start();
}
(3)view以X轴为轴点顺时针(逆时针是-360)旋转360度,无限循环
//view以X轴为轴点顺时针(逆时针是-360)旋转360度,无限循环
public void rotateAnimator(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationX", 0f, 360f);
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(-1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.start();
}
(4)view以Y轴为轴点顺时针(逆时针是-360)旋转360度,无限循环
//view以Y轴为轴点顺时针(逆时针是-360)旋转360度,无限循环
public void rotateAnimator(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", 0f, 360f);
animator.setDuration(2000);
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(-1);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.start();
}
4、透明度动画:
//view透明度从1变到0.1
public void alphaAnimator(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.1f);
animator.setDuration(2000);
animator.start();
}
5、按顺序播放动画:
//按顺序播放动画:view先向右移动width/2,再向下移动height,再向左移动width,再向上移动height,再向右移动width/2
public void animateSequentially(View v) {
//向右移动width/2
ObjectAnimator animator01 = ObjectAnimator.ofFloat(view, "translationX", 0, width / 2);
animator01.setDuration(500);
//向下移动height
ObjectAnimator animator02 = ObjectAnimator.ofFloat(view, "translationY", 0, height);
animator02.setDuration((long) (1000 * height / width));
//向左移动width
ObjectAnimator animator03 = ObjectAnimator.ofFloat(view, "translationX", width / 2, -width / 2);
animator03.setDuration(1000);
//向上移动height
ObjectAnimator animator04 = ObjectAnimator.ofFloat(view, "translationY", height, 0);
animator04.setDuration((long) (1000 * height / width));
//向右移动width/2
ObjectAnimator animator05 = ObjectAnimator.ofFloat(view, "translationX", -width / 2, 0);
animator05.setDuration(500);
//依次播放动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(animator01, animator02, animator03, animator04, animator05);
animatorSet.start();
}
动画效果:
6、若干动画同时进行
//若干动画同时进行
public void animateTogether(View v) {
//向左移动width/2
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0, -width / 2);
animator.setDuration(1000);
//向右移动width
ObjectAnimator animator01 = ObjectAnimator.ofFloat(view, "translationX", width / 2);
animator01.setDuration(2000);
animator01.setInterpolator(new LinearInterpolator());
//向下移动height
ObjectAnimator animator02 = ObjectAnimator.ofFloat(view, "translationY", 0, height);
animator02.setDuration(2000);
animator02.setInterpolator(new AccelerateInterpolator());
//向右、向下动画同时进行
AnimatorSet togetherAnimator = new AnimatorSet();
togetherAnimator.playTogether(animator01, animator02);
//view先向左移动width/2,再做抛物线运动(向右和向下动画同时进行)
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(animator, togetherAnimator);
animatorSet.start();
//动画结束1s后使view回到初始位置
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
view.setTranslationX(0);
view.setTranslationY(0);
}
},1000);
}
});
}
7、综合运用
如下图所示,这是我在某个app看到的一个效果,页面刚打开时,顶部布局先上移一段距离,同时缩小为原来大小的一半(此过程瞬间完成)。然后开始下落,落底后,边上移边缩放至原来大小。
代码:
private void startAnimation() {
final LinearLayout topLayout = findViewById(R.id.layout_top);
topLayout.post(new Runnable() {
@Override
public void run() {
int height = topLayout.getHeight();
//动画开始前
topLayout.setTranslationY(-height);//layout瞬间上移动height
topLayout.setScaleX(0.5f);//layout宽度瞬间缩放到原来的一半
topLayout.setScaleY(0.5f);//layout高度瞬间缩放到原来的一半
//下落动画
ObjectAnimator downAnimator = ObjectAnimator.ofFloat(topLayout, "translationY", height);
downAnimator.setDuration(1000);
downAnimator.setInterpolator(new BounceInterpolator());
//上移动画
ObjectAnimator upAnimator = ObjectAnimator.ofFloat(topLayout, "translationY", 0);
//缩放动画
ObjectAnimator animatorX = ObjectAnimator.ofFloat(topLayout, "scaleX", 1f);
ObjectAnimator animatorY = ObjectAnimator.ofFloat(topLayout, "scaleY", 1f);
AnimatorSet scaleAnimatorSet = new AnimatorSet();
//scaleAnimatorSet.play(animatorX).with(animatorY);//两个动画同时开始
scaleAnimatorSet.playTogether(animatorX, animatorY);//两个动画同时开始
//上移和缩放动画同时进行
AnimatorSet togetherAnimatorSet = new AnimatorSet();
togetherAnimatorSet.playTogether(upAnimator, scaleAnimatorSet);
togetherAnimatorSet.setDuration(2000);
//设置依次播放动画
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(downAnimator, togetherAnimatorSet);
animatorSet.start();
}
});
}
8、为动画添加监听事件
方式1(四个方法全实现):
/**
* 为动画添加监听事件方式一
*/
private void addAnimatorListener01(ObjectAnimator animator) {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
//动画开始前调用此方法
Toast.makeText(ObjectAnimatorActivity.this, "start", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animator animator) {
//动画结束后调用此方法
Toast.makeText(ObjectAnimatorActivity.this, "end", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
}
方式2(选择实现方法)
/**
* 为动画添加监听事件方式二
*/
private void addAnimatorListener02(ObjectAnimator animator) {
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
//动画开始前调用此方法
Toast.makeText(ObjectAnimatorActivity.this, "start", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
//动画结束后调用此方法
Toast.makeText(ObjectAnimatorActivity.this, "end", Toast.LENGTH_SHORT).show();
}
});
}