1.在drawable文件中定义帧动画的xml文件
<?xml version="1.0" encoding="utf-8"?>
<!-- oneshot为true:只播放一次 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/cat1"
android:duration="80"/>
<item android:drawable="@drawable/cat2"
android:duration="80"/>
<item android:drawable="@drawable/cat3"
android:duration="80"/>
</animation-list>
2.在Activity布局中添加播放动画的View
3.添加背景drawable为动画xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AnimActivity">
<ImageView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame_anim"
android:id="@+id/iv_anim"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="播放"
android:id="@+id/btn_start"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:text="暂停"
android:id="@+id/btn_stop"/>
</LinearLayout>
</LinearLayout>
4.开始/暂停动画
AnimationDrawable mAnimationDrawable;
mAnimationDrawable = (AnimationDrawable) mIvAnim.getBackground();
mAnimationDrawable.start();
mAnimationDrawable.stop();
android提供的补间动画大致有四种:透明渐变、位移、旋转和缩放;还有就是将这四种组合而来的组合渐变动画。还有一种用于控制动画变化速度的插值器:Interpolator,其种类如下所示:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1">
</alpha>
//Activity中的使用
Animation mAnimation;
case R.id.btn_alpha:{
mAnimation = AnimationUtils.loadAnimation(this,R.anim.alpha_anim);
mIvAnim2.startAnimation(mAnimation);
break;
}
case R.id.btn_scale:{
mAnimation = AnimationUtils.loadAnimation(this,R.anim.scale_anim);
mIvAnim2.startAnimation(mAnimation);
break;
}
case R.id.btn_tran:{
mAnimation = AnimationUtils.loadAnimation(this,R.anim.trans_anim);
mIvAnim2.startAnimation(mAnimation);
break;
}
case R.id.btn_rotate:{
mAnimation = AnimationUtils.loadAnimation(this,R.anim.rotate_anim);
mIvAnim2.startAnimation(mAnimation);
break;
}
case R.id.btn_com:{
mAnimation = AnimationUtils.loadAnimation(this,R.anim.com_anim);
mIvAnim2.startAnimation(mAnimation);
break;
}
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="100"
android:duration="2000">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="2000"
android:repeatCount="1">
<!-- fromDegrees、toDegrees:开始/结束角度-->
<!-- repeatCount:重复次数(第一次结束后再次播放的次数)-->
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5"
android:duration="2000">
<!-- fromXScale、fromYScale:缩放起始比例-->
<!-- pivotX、pivotY:旋转中心-->
<!-- toXScale、toYScale:缩放目标比例-->
</scale>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toYScale="1.5"
android:toXScale="1.5"
android:duration="2000"/>
<alpha android:fromAlpha="1"
android:toAlpha="0.1"
android:duration="2000"/>
</set>
//旋转动画:对象属性动画
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(tvShow, "rotation", 0f, 360f);
objectAnimator2.setInterpolator(new LinearInterpolator());
objectAnimator2.setEvaluator(new FloatEvaluator());
//背景颜色动画:纯值属性动画
ValueAnimator valueAnimator = ValueAnimator.ofInt(0xFF000000, 0x00FFFFFF);
//添加值变化监听来改变属性值
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
tvShow.setBackgroundColor(animatedValue);
}
});
valueAnimator.setInterpolator(new LinearInterpolator());//线性插值器
valueAnimator.setEvaluator(new IntEvaluator());//int估值器
//定义属性动画集合
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {//动画结束
super.onAnimationEnd(animation);
Toast.makeText(MainActivity.this, "animation end!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationStart(Animator animation) {//动画开始
super.onAnimationStart(animation);
Toast.makeText(MainActivity.this, "animation start!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationPause(Animator animation) {//动画暂停
super.onAnimationPause(animation);
Toast.makeText(MainActivity.this, "animation pause!", Toast.LENGTH_SHORT).show();
}
});
animatorSet.setTarget(tvShow);
animatorSet.play(objectAnimator2).with(valueAnimator);//with:同时执行;before、after之前、之后
animatorSet.setDuration(4000);
animatorSet.start();
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="rotation"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="360" />
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:valueType="intType"
android:valueFrom="@color/black"
android:valueTo="@color/white"
android:interpolator="@android:anim/linear_interpolator" />
<!-- java中使用 -->
ValueAnimator valueAnimator1 = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.value_anim);
valueAnimator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
tvShow.setBackgroundColor(animatedValue);
}
});
ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.object_anim);
objectAnimator.setTarget(tvShow);
AnimatorSet animatorSet1 = new AnimatorSet();
animatorSet1.setTarget(tvShow);
animatorSet1.setDuration(3000);
animatorSet1.play(objectAnimator).with(valueAnimator1);
animatorSet1.start();