补间动画
动画类型
插入器
代码示例
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示例
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!--android:shareInterpolator="false" 若true应用外部interpolator,若false应用内部interpolator-->
<rotate
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="-360"
android:duration="2000"
android:fillAfter="false"/>
<rotate
android:interpolator="@android:anim/decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:startOffset="2000"
android:fillAfter="false"/>
</set>
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.aaaa);
view.startAnimation(alphaAnimation);
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5"/>
帧动画
XML格式
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item
android:drawable="@drawable/loving_a1"
android:duration="100"/>
<item
android:drawable="@drawable/loving_a2"
android:duration="100"/>
<item
android:drawable="@drawable/loving_a3"
android:duration="100"/>
<item
android:drawable="@drawable/loving_a4"
android:duration="100"/>
<item
android:drawable="@drawable/loving_a5"
android:duration="100"/>
</animation-list>
代码播放动画
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.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方式
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="1000"
android:propertyName="scaleX"
android:valueFrom="1"
android:valueTo="0.5">
</objectAnimator>
<objectAnimator
android:duration="1000"
android:propertyName="scaleY"
android:valueFrom="1"
android:valueTo="0.5">
</objectAnimator>
</set>
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.abc);
anim.setTarget(view);
anim.start();
简介
-
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 = new ValueAnimator();
valueAnimator.setObjectValues(new PointF(0, 0), new PointF(0, 0));
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
@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
用于控制多个属性动画的行为。
简单示例
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);