补间动画
通过api设置参数启动动画
xml文件补间动画
通过xml文件配置参数启动动画
帧动画
通过xml文件制定加载图片的顺序来顺序加载显示动画
补间动画
透明度动画 AlphaAnimation
位移动画 TranslateAnimation
缩放动画 ScaleAnimation
旋转动画 RotateAnimation
动画组合 AnimationSet
参数中的0.5等应该是百分比,也就是50%
<RelativeLayout 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" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:onClick="rotate" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋转" /> <Button android:onClick="scale" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="缩放" /> <Button android:onClick="trans" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="位移" /> <Button android:onClick="alpha" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="透明度" /> <Button android:onClick="set" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="组合动画" /> </LinearLayout> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>
package com.itheima.tween; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //透明度动画 public void alpha(View view){ //从完全透明到完全不透明 0.0是完全透明 1.0是完全不透明 AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); //动画播放的时间 aa.setDuration(2000); //设置重复播放的次数 3的话就是重复播放三次,那么总共就是4次 //-1是永远不停的播放 aa.setRepeatCount(1); //反序播放,先从完全透明到不完全透明,然后又慢慢回到完全透明 aa.setRepeatMode(Animation.REVERSE); //true就是播放后是什么效果就是什么效果 //false是播放后就回到最初的效果 aa.setFillAfter(true); //开始播放 iv.startAnimation(aa); } //位移动画 public void trans(View view){ //位移动画类 fromx tox fromy toy TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); ta.setDuration(2000); ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); iv.startAnimation(ta); } //缩放动画 //前四位参数fromx tox fromy toy 后四位参数是中心点对应的参照位置和x y public void scale(View view){ ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); iv.startAnimation(sa); } //旋转动画 public void rotate(View view){ //前两个参数说明从多少度到多少度 //后面四个参数说明相对那个位置的xy旋转 RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); iv.startAnimation(ra); } //动画组合 public void set(View view){ //设置是否共同播放速度 false是每个都按照自己的播放速度 AnimationSet set = new AnimationSet(false); TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); ta.setDuration(2000); ta.setRepeatCount(1); ta.setRepeatMode(Animation.REVERSE); ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f); ra.setDuration(2000); ra.setRepeatCount(1); ra.setRepeatMode(Animation.REVERSE); set.addAnimation(ra); //set.addAnimation(ta); set.addAnimation(sa); iv.startAnimation(set); } }
xml文件补间动画
使用android提供的AnimationUtils.loadAnimation方法加载xml文件
package com.itheima.tween; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } //透明度动画 public void alpha(View view){ Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha); iv.startAnimation(aa); } //位移动画 public void trans(View view){ Animation ta = AnimationUtils.loadAnimation(this, R.anim.trans); iv.startAnimation(ta); } //缩放动画 public void scale(View view){ Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale); iv.startAnimation(sa); } //旋转动画 public void rotate(View view){ Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate); iv.startAnimation(ra); } //动画组合 public void set(View view){ Animation set = AnimationUtils.loadAnimation(this, R.anim.set); iv.startAnimation(set); } }透明度动画
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" android:fillAfter="true" > </alpha>位移动画
<?xml version="1.0" encoding="utf-8"?> <!-- 有p代表父亲,没有p代表自己 --> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="-50%p" android:toXDelta="50%p" android:fromYDelta="0" android:toYDelta="0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" > </translate>缩放动画
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.1" android:toXScale="2.0" android:fromYScale="0.1" android:toYScale="2.0" android:duration="2000" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" > </scale>旋转动画
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" > </rotate>动画组合
<?xml version="1.0" encoding="utf-8"?> <set> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromAlpha="0.0" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="1.0" > </alpha> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toDegrees="360" > </rotate> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXScale="0.1" android:fromYScale="0.1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:toXScale="2.0" android:toYScale="2.0" > </scale> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromXDelta="-50%p" android:fromYDelta="0" android:repeatCount="1" android:repeatMode="reverse" android:toXDelta="50%p" android:toYDelta="0" > </translate> </set>帧动画
通过AnimationDrawable来开启帧动画
<RelativeLayout 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" tools:context=".MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout>
package com.itheima.frameanimation; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.view.Menu; import android.view.MotionEvent; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; private AnimationDrawable mAnimationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); // 把xml文件的动画资源设置为iv背景 iv.setBackgroundResource(R.drawable.girl); // 获取设置的动画资源。 执行可能需要花费一定的时间,这是个异步的操作 mAnimationDrawable = (AnimationDrawable) iv.getBackground(); //正因为是异步的 所以不能直接start,因为资源还没准备好 //所以就设置为触摸屏幕后才开始播放 } public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { mAnimationDrawable.start(); return true; } return super.onTouchEvent(event); } }
<?xml version="1.0" encoding="utf-8"?> <!-- oneshot为true就只会播放一次,而false就循环播放 200是设置每个帧的时间 --> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/girl_1" android:duration="200"/> <item android:drawable="@drawable/girl_2" android:duration="200"/> <item android:drawable="@drawable/girl_3" android:duration="200"/> <item android:drawable="@drawable/girl_4" android:duration="200"/> <item android:drawable="@drawable/girl_5" android:duration="200"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_6" android:duration="400"/> <item android:drawable="@drawable/girl_7" android:duration="400"/> <item android:drawable="@drawable/girl_8" android:duration="200"/> <item android:drawable="@drawable/girl_9" android:duration="200"/> <item android:drawable="@drawable/girl_10" android:duration="200"/> <item android:drawable="@drawable/girl_11" android:duration="200"/> </animation-list>