从大的方向上来看,Android动画分为两大类——Property Animation(属性动画)和View Animation(视图动画)。而视图动画依然可以分成两类——Frame Animation和Tween Animation,先来说视图动画。
视图动画
Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画。Frame动画可以被定义在XML文件中,也可以完全编码实现。
关于Frame动画,有一位前辈的文章讲解地非常清楚,我就不班门弄斧了,上链接:详解Android动画之Frame Animation。
而Tween动画这位前辈也讲得相当地有水平,同样是上链接:详解Android动画之Tween Animation.
高山仰止,向前辈学习!
不过关于视图动画我自己也踩过几个坑,这里和大家分享一下,或许对大家有用(大神就别看了,比较初级),先把代码贴上。
import android.app.Activity;
import android.os.Bundle;
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;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnAlpha(View view) {
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
view.startAnimation(aa);
}
public void btnRotate(View view) {
RotateAnimation ra = new RotateAnimation(0, 360, 100, 100);
ra.setDuration(1000);
view.startAnimation(ra);
}
public void btnRotateSelf(View view) {
RotateAnimation ra = new RotateAnimation(0, 360,
RotateAnimation.RELATIVE_TO_SELF, 0.5F,
RotateAnimation.RELATIVE_TO_SELF, 0.5F);
ra.setDuration(1000);
view.startAnimation(ra);
}
public void btnTranslate(View view) {
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
ta.setDuration(1000);
view.startAnimation(ta);
}
public void btnScale(View view) {
ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 2);
sa.setDuration(1000);
view.startAnimation(sa);
}
public void btnScaleSelf(View view) {
ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5F,
Animation.RELATIVE_TO_SELF, 0.5F);
sa.setDuration(1000);
view.startAnimation(sa);
}
public void btnSet(View view) {
AnimationSet as = new AnimationSet(true);
as.setDuration(1000);
AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
as.addAnimation(aa);
TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200);
ta.setDuration(1000);
as.addAnimation(ta);
view.startAnimation(as);
}
}
然后是配套的XML文件:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alpha"
android:layout_margin="10dp"
android:onClick="btnAlpha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate"
android:layout_margin="10dp"
android:onClick="btnRotate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate_self"
android:layout_margin="10dp"
android:onClick="btnRotateSelf" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate"
android:layout_margin="10dp"
android:onClick="btnTranslate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale"
android:layout_margin="10dp"
android:onClick="btnScale" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale_Self"
android:layout_margin="10dp"
android:onClick="btnScaleSelf" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Anim Set"
android:layout_margin="10dp"
android:onClick="btnSet" />
</LinearLayout>
可能有人会问了,全都是按钮,你的动画怎么展示? (其实我建议你自己跑一下这些代码,很有意思的)
这就是我遇到的第一个坑,答案是——动画效果会直接呈现在调用它们的View上,也就是说,在本例中,动画效果会呈现在这些按钮上。
下面介绍一下这些构造方法:
AlphaAnimation aa = new AlphaAnimation(0, 1);
透明度动画,参数范围是从0到1的float
型
(或整型)
数字,0代表完全透明,1代表完全不透明。如果参数是float型,就可以选择两者之间的数字,而如果是整型,那么就只有完全透明和完全不透明两个选项。构造函数的第一个参数是初始状态,第二个参数是结束状态。
RotateAnimation ra = new RotateAnimation(0, 360, 0, 0);
旋转动画,第一个参数为起始角度,第二个参数为终止角度,后面两个参数是旋转中心的X,Y坐标。
这个坐标系的原点是该View的最左上角的点,以右为X正方向,以下为Y正方向。(加粗部分很重要,这是我遇到的第二个坑)
RotateAnimation ra = new RotateAnimation(0, 360,
RotateAnimation.RELATIVE_TO_SELF, 0.5F,
RotateAnimation.RELATIVE_TO_SELF, 0.5F);
旋转动画的一种特殊形式,坐标系依旧是以视图的最左上角的点为原点,右为X正方向,下为Y正方向,但是它所描述坐标的方式是比例——比如第一个0.5F就是说视图X坐标为视图在X方向上长度的一半,后面那个0.5F则是Y方向的。特别注意,这个比例是可以大于1的(
当大于1时,这个旋转点是在视图的外部),也可以小于0(反方向)。
TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 300);
位移动画,坐标系与以上一样,第一个参数是X初始坐标,第二个参数是X终止坐标,第三个参数是Y初始坐标,第四个参数是Y终止坐标。
ScaleAnimation sa = new ScaleAnimation(3, 0, 3, 0);
缩放动画,第一个参数为初始X方向放大倍数,第二个参数为终止X方向放大倍数,第三个参数为初始Y方向放大倍数,第四个参数为终止Y方向放大倍数,
默认以原点为中心,
也可以设置中心的坐标。
ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1,
Animation.RELATIVE_TO_SELF, 0.5F,
Animation.RELATIVE_TO_SELF, 0.5F);
这种情况下缩放中心为View的中心。
AnimationSet as = new AnimationSet(true);
AinmationSet就是View动画的集合,可以将以上几种动画同时使用。