补间动画
动画类型
- AlphaAnimation
- ScaleAnimation
- TranslateAnimation
- RotateAnimation
- AnimationSet:动画集合
插入器
- AccelerateInterpolator:加速
- DecelerateInterpolator:减速
- AccelerateDecelerateInterpolator:加速,减速
- BounceInterpolator:弹跳
- CycleInterpolator:循环
- LinearInterpolator:线性
- AnticipateInterpolator:反向
- OvershootInterpolator:超越,恢复
- AnticipateOvershootInterpolator:反向,超越,恢复
代码示例
Animation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
alphaAnimation.setDuration(1000);
alphaAnimation.setAnimationListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animation animation) {
// 开始动画
}
@Override
public void onAnimationEnd(Animation animation) {
// 结束动画
}
@Override
public void onAnimationRepeat(Animation animation) {
// 开始重复动画
}
});
alphaAnimation.setFillAfter(true); //动画结束后不恢复原状
tempview.startAnimation(alphaAnimation);
Animation animation = new TranslateAnimation(0f, 30f, 0f, 30f);
animation.setDuration(1000);
animation.setFillAfter(true); //动画结束后不恢复原状
AnimationSet set = new AnimationSet(false);
set.addAnimation(animation);
set.addAnimation(animation);
view.startAnimation(set);
XML示例
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.aaaa);
view.startAnimation(alphaAnimation);
帧动画
XML格式
代码播放动画
ImageView tempview = (ImageView) findViewById(R.id.tempview);
tempview.setImageResource(R.drawable.click_loving_anim_1);
AnimationDrawable animation = (AnimationDrawable) tempview.getDrawable();
animation.start();
注意事项
- 帧动画 XML 文件应该放在 drawable 文件夹。
属性动画
ObjectAnimator
简介
-
ObjectAnimator
可用于实现简单属性动画。
简单示例
方式一
ObjectAnimator.ofFloat(view, "RotationX", 0.0f, 360.0f).setDuration(1000).start();
方式二
view.setAlpha(0f);
view.animate().setDuration(1000).alpha(1f);
复杂
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "XXX", 0.0F, 1.0F).setDuration(1500);
animator.addUpdateListener(animation -> {
float cVal = (Float) animation.getAnimatedValue();
view.setScaleX(cVal);
view.setScaleY(cVal);
});
animator.setInterpolator(new LinearInterpolator());
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
// 开始动画时
}
@Override
public void onAnimationEnd(Animator animation) {
// 结束动画时
}
@Override
public void onAnimationCancel(Animator animation) {
// 取消动画时
}
@Override
public void onAnimationRepeat(Animator animation) {
// 动画重新开始时
}
});
animator.start();
XML方式
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.abc);
anim.setTarget(view);
anim.start();
PropertyValuesHolder
简介
-
PropertyValuesHolder
用于拼接多个动画。
简单示例
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start();
ValueAnimator
简介
简单示例
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setObjectValues(new PointF(0, 0), new PointF(0, 0));
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator() {
@Override
public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
// fraction与进度有关,在0-1之间
// 根据三个参数来计算出返回值
return new PointF();
}
});
valueAnimator.addUpdateListener(animation -> {
PointF point = (PointF) animation.getAnimatedValue();
view.setX(point.x);
view.setY(point.y);
});
valueAnimator.start();
AnimatorSet
简介
-
AnimatorSet
用于控制多个属性动画的行为。
简单示例
ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 2f).setDuration(1000);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 2f).setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(anim1, anim2);
animatorSet.playTogether(anim1, anim2);
animatorSet.play(anim1).before(anim2);
animatorSet.play(anim1).with(anim2);
animatorSet.play(anim1).after(anim2);
animatorSet.start();
布局动画
简介
简单示例
mTransition = new LayoutTransition();
mTransition.setAnimator(LayoutTransition.APPEARING, ObjectAnimator.ofFloat(null, "scaleX", 0f, 1f));
mTransition.setAnimator(LayoutTransition.CHANGE_APPEARING,mTransition.getAnimator(LayoutTransition.CHANGE_APPEARING));
mTransition.setAnimator(LayoutTransition.DISAPPEARING, mTransition.getAnimator(LayoutTransition.DISAPPEARING));
mTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,mTransition.getAnimator(LayoutTransition.CHANGE_DISAPPEARING));
mGridLayout.setLayoutTransition(mTransition);