Android之 动画总结

一 动画种类

1.1 动画在Android中运用也非常广泛,如点击按钮,加载框,Activity的转场等都有动画的身影

1.2 常用的动画有以下以下几种

逐帧动画【Frame Animation】,即顺序播放事先准备的图片

补间动画【Tween Animation】,View的动画效果可以实现简单的平移、缩放、旋转。

属性动画【Property Animation】,补间动画增强版,支持对对象执行动画。

过渡动画【Transition Animation】,实现Activity或View过渡动画效果。包括5.0之后的MD过渡动画等。

二 逐帧动画 

2.1 定义一个动画xml,设置图片集合

<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00000" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00001" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00002" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00003" />
    <item android:duration="100" android:drawable="@drawable/new_shuaxin_00004" />
</animation-list>

2.2 java设置动画

//定义组件
ImageVIew ivImage = findViewById(R.id.iv_refresh_header);
//开始动画
ivImage.setImageResource(R.drawable.anim_loading);
mAnimationDrawable = (AnimationDrawable) ivImage.getDrawable();
mAnimationDrawable.start();

//停止动画
ivImage.clearAnimation();
if (mAnimationDrawable != null){
	mAnimationDrawable.stop();
}

三 补间动画

2.1 补间动画种类

透明度变化,大小缩放变化,位移变化,旋转变化

2.2 透明度动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <alpha 
        android:duration="1000" 
        android:fromAlpha="0.0" 
        android:toAlpha="1.0" /> 
</set>

java定义

AlphaAnimation alpha = new AlphaAnimation(0, 1); 
alpha.setDuration(500);          //设置持续时间 
alpha.setFillAfter(true);                   //动画结束后保留结束状态 
alpha.setInterpolator(new AccelerateInterpolator());        //添加差值器 
ivImage.setAnimation(alpha);

2.3 大小缩放动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <scale 
        android:duration="1000" 
        android:fillAfter="false" 
        android:fromXScale="0.0" 
        android:fromYScale="0.0" 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:toXScale="1.4" 
        android:toYScale="1.4" /> 
</set>

java定义

ScaleAnimation scale = new ScaleAnimation(1.0f, scaleXY, 1.0f, scaleXY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
scale.setDuration(durationMillis); 
scale.setFillAfter(true); 
ivImage.setAnimation(scale);

2.4 位移动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <translate 
        android:duration="2000" 
        android:fromXDelta="30" 
        android:fromYDelta="30" 
        android:toXDelta="-80" 
        android:toYDelta="300" /> 
</set>

Java定义

TranslateAnimation translate = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta); 
translate.setDuration(durationMillis); 
translate.setFillAfter(true); 
ivImage.setAnimation(translate);

2.5 旋转动画,两种定义方式

xml定义

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" > 
    <rotate 
        android:duration="3000" 
        android:fromDegrees="0" 
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:toDegrees="+350" /> 
</set>

java定义

RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
rotate.setDuration(durationMillis); 
rotate.setFillAfter(true); 
ivImage.setAnimation(rotate);

2.6 组合动画AnimationSet

RelativeLayout rlRoot = (RelativeLayout) findViewById(R.id.rl_root);

//旋转动画
RotateAnimation animRotate = new RotateAnimation(0, 360,
			Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
			0.5f);
animRotate.setDuration(1000);// 动画时间
animRotate.setFillAfter(true);// 保持动画结束状态

//缩放动画
ScaleAnimation animScale = new ScaleAnimation(0, 1, 0, 1,
			Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
animScale.setDuration(1000);
animScale.setFillAfter(true);// 保持动画结束状态

//渐变动画
AlphaAnimation animAlpha = new AlphaAnimation(0, 1);
animAlpha.setDuration(2000);// 动画时间
animAlpha.setFillAfter(true);// 保持动画结束状态

//动画集合
AnimationSet set = new AnimationSet(true);
set.addAnimation(animRotate);
set.addAnimation(animScale);
set.addAnimation(animAlpha);

//启动动画
rlRoot.startAnimation(set);

三 属性动画

3.1 xml+java调用方式

定义animator.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <animator 
        android:valueFrom="0" 
        android:valueTo="100" 
        android:valueType="intType" 
        android:duration="3000" 
        android:startOffset ="1000" 
        android:fillBefore = "true" 
        android:fillAfter = "false" 
        android:fillEnabled= "true" 
        android:repeatMode= "restart" 
        android:repeatCount = "0" 
        android:interpolator="@android:anim/accelerate_interpolator"/> 
</set>

    java调用

Button button = (Button) findViewById(R.id.button); 
Animator mAnim = AnimatorInflater.loadAnimator(this, R.animator.animator); 
mAnim.setTarget(button); 
mAnim.start();

3.2 纯java方式

ObjectAnimator mAnimator = ObjectAnimator.ofFloat(view, type, start, end); 

// 设置动画重复播放次数 = 重放次数+1 
// 动画播放次数 = infinite时,动画无限重复 
mAnimator.setRepeatCount(ValueAnimator.INFINITE); 
// 设置动画运行的时长 
mAnimator.setDuration(time); 
// 设置动画延迟播放时间 
mAnimator.setStartDelay(0); 
// 设置重复播放动画模式 
mAnimator.setRepeatMode(ValueAnimator.RESTART); 
// ValueAnimator.RESTART(默认):正序重放 
// ValueAnimator.REVERSE:倒序回放 
//设置差值器 
mAnimator.setInterpolator(new LinearInterpolator()); 
return mAnimator; 

3.3 ValueAnimator值动画,主要负责值的计算和过度,以及动画的播放次数、播放模式和动画监听等

ValueAnimator animator = ValueAnimator.ofFloat(1, 0.5f, 1);
animator.setDuration(3000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float value = (Float) animation.getAnimatedValue();
        imageView.setScaleX(value);
    }
});
animator.start();

3.4 ObjectAnimator对象动画,继承ValueAnimator,可以直接修改对象的属性

ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "scaleX", 1, 0.5f, 1);
animator.setDuration(3000);
animator.start();

3.5 PropertyValuesHolder,用来保存属性的值

PropertyValuesHolder xHolder = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.5f, 1.0f);
PropertyValuesHolder yHolder = PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.5f, 1.0f);

ObjectAnimator.ofPropertyValuesHolder(imageView, xHolder, yHolder)
        .setDuration(3000)
        .start();

3.6 AnimatorSet,实现多个动画效果

ObjectAnimator xAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f, 1.0f);
ObjectAnimator yAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f, 1.0f);

AnimatorSet animator = new AnimatorSet();
animator.playTogether(xAnimator, yAnimator);
animator.setDuration(3000);
animator.start();

AnimatorSet还可以指定动画的顺序,调用playSequentially()方法依次播放动画 


ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.5f, 1.0f);
animator1.setDuration(2000);

ObjectAnimator xAnimator2 = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f, 1.0f);
ObjectAnimator yAnimator2 = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f, 1.0f);
AnimatorSet scaleAnimator = new AnimatorSet();
scaleAnimator.playTogether(xAnimator2, yAnimator2);
scaleAnimator.setDuration(2000);

ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
animator3.setDuration(2000);

AnimatorSet animator = new AnimatorSet();
animator.playSequentially(animator1, scaleAnimator, animator3);

animator.start();

3.7 Evaluator估值器,告诉动画系统如何从初始值过度到结束值

  • IntEvaluator :用于计算int类型属性值的计算器
  • FloatEvaluator :用于计算float类型属性值的计算器
  • ArgbEvaluator :用于计算十六进制形式表示的颜色值的计算器
  • TypeEvaluator:计算器的接口,我们可以实现该接口来完成自定义计算器

四 过渡动画,即Activity转场动画 

4.1 定义动画xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="270"
        android:fromXDelta="100%p"
        android:toXDelta="0%p" />

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="270"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p" />

</set>

4.2 设置style

<!--左右进出场的activity动画-->
<style name="My_AnimationActivity" mce_bogus="1" parent="@android:style/Animation.Activity">
	<item name="android:activityOpenEnterAnimation">@anim/open_enter</item>
	<item name="android:activityCloseExitAnimation">@anim/close_exit</item>
</style>

<!--上下进出场的activity动画-->
<style name="up_down_activity_anim" mce_bogus="1" parent="@android:style/Animation.Activity">
	<item name="android:activityOpenEnterAnimation">@anim/open_up</item>
	<item name="android:activityCloseExitAnimation">@anim/close_down</item>
</style>

4.3 java调用

startActivity(intent);
overridePendingTransition(R.anim.bottom_top_anim, R.anim.alpha_hide);

finish();
overridePendingTransition(R.anim.alpha_show, R.anim.top_bottom_anim);

4.4 Android5.0之后,Android就自带几种动画特效。 3种转场动画 ,1种共享元素

   三种转场动画

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void explode(View view) {
	intent = new Intent(this, TransitionActivity.class);

	intent.putExtra("flag", 0);

	startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void slide(View view) {
	intent = new Intent(this, TransitionActivity.class);

	intent.putExtra("flag", 1);

	startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void fade(View view) {
	intent = new Intent(this, TransitionActivity.class);

	intent.putExtra("flag", 2);

	startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

}

共享动画

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void share(View view) {
	View fab = findViewById(R.id.fab_button);
	intent = new Intent(this, TransitionActivity.class);

	intent.putExtra("flag", 3);

	//创建单个共享
    //startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, view, "share")
    //            .toBundle());

	//创建多个共享
	startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, Pair.create
			(view, "share"),
			Pair.create(fab,"fab"))
			.toBundle());

}