您的位置:首页 > 移动开发 > Android开发

Android Animation简述

2015-11-13 14:34 681 查看
Animation动画效果。提供了一系列的动画效果,可以应用大多数 的控件。

一、Animations从总体上来说可以分为两大类:

1、TweenedAnimations:该类提供了旋转,移动,伸展,和淡出竺效果;

2、Frame-by-FrameAmimations:这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示。

二、TwenedAnimations的分类

a) Alpha:淡入淡出效果

b) Scale:缩放效果

c) Rotate:旋转效果

d) Translate:移动效果

这四种动画效果对应四个不同的类,都有不同的参数,但是这四个不同的动画效果有共同的参数:下面简单介绍下面几种:

setDuration(long durationMills)设置动画持续时间,单位毫秒 ;

setFillAfter(boolean fillAfter)如果为true的话,动画执行后,控件停留在执行结束的状态;

setFillBefore(boolean fillBefore)

setStartOffSet(long startOffSet)设置动画执行之前等待时间;

setRepeatCount(int repeatCount)设置动画重复的次数
下面简单介绍一下,TewnedAnimation的使用步骤:

1、创建一个AnimationSet,存放动画集合,也可以只有一个动画。

2、根据需要创建对应的Animation

3、根据动画的需求,为Animation创建相对应的数据。

4、将Animation对象添加到AnimationSet

5、使用控件开始执行Animation:textView.startAnimation(AnimateionSet)当然里面的参数也可以是Animate,由于Animation是AnimationSet的父类,所以不会出错

我们来做一个简单的例子,了解Animation的用法 :

1、 在main.xml文件里面添加四个按钮控件,和一个图片控件,用来进行测试,图片用于显示动画效果;

[html] view
plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<ImageView android:id="@+id/image"

android:src="@drawable/icon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="100dip"/>

<Button android:id="@+id/alpha"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/alpha"/>

<Button android:id="@+id/scale"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/scale"/>

<Button android:id="@+id/rotate"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/rotate"/>

<Button android:id="@+id/translate"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/translate"/>

</LinearLayout>

2、编写Activity类,添加监听:并且在对应的监听事件上 加上动画效果(按上面介绍了的步骤进行)

[java] view
plaincopyprint?

public class HelloAnimationActivity extends Activity {

/** Called when the activity is first created. */

Button b_alpha;

Button b_scale;

Button b_rotate;

Button b_translate;

ImageView iv;

AnimationSet animationSet;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

iv = (ImageView)findViewById(R.id.image);

b_alpha = (Button)findViewById(R.id.alpha);

b_scale = (Button)findViewById(R.id.scale);

b_rotate = (Button)findViewById(R.id.rotate);

b_translate = (Button)findViewById(R.id.translate);

b_alpha.setOnClickListener(new AlpahListener());

b_scale.setOnClickListener(new ScaleListener());

b_rotate.setOnClickListener(new RotateListener());

b_translate.setOnClickListener(new TranslateListener());

}

class AlpahListener implements OnClickListener{

@Override

public void onClick(View v) {

//创建一个AnimationSet对象

AnimationSet animationSet = new AnimationSet(true);

//创建一个AlphaAnimation对象

AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

//设置动画执行的时间(单位:毫秒)

alphaAnimation.setDuration(1000);

//将AlphaAnimation对象添加到AnimationSet当中

animationSet.addAnimation(alphaAnimation);

//使用ImageView的startAnimation方法开始执行动画

iv.startAnimation(animationSet);

}

}

class ScaleListener implements OnClickListener{

/**

* 从大到小的动画,x轴1到0.1,y轴1到0.1。中心为图片的中心(0.5f,0.5f)

*/

@Override

public void onClick(View arg0) {

AnimationSet animationSet = new AnimationSet(true);

ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

animationSet.addAnimation(scaleAnimation);

animationSet.setStartOffset(1000);

animationSet.setFillAfter(true);

animationSet.setFillBefore(false);

animationSet.setDuration(2000);

iv.startAnimation(animationSet);

}

}

class RotateListener implements OnClickListener{

/**

* 以图片中心旋转360度;(0.5f,0.5f)表示图片的中心

*/

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

animationSet = new AnimationSet(true);

RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

rotateAnimation.setDuration(5000);

animationSet.addAnimation(rotateAnimation);

iv.setAnimation(rotateAnimation);

}

}

class TranslateListener implements OnClickListener{

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

animationSet = new AnimationSet(true);

TranslateAnimation translateAnimation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0f,

Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0f,

Animation.RELATIVE_TO_SELF, 1.0f);

animationSet.addAnimation(translateAnimation);

iv.setAnimation(animationSet);

}

}

}

下面来看看Animation的第二种用法 :

这种方法跟上一种差不多,只不过是将动画的布局,在xml文件里配置,配置动画;这样做的好处是一个动画配置可以重复应用,这种方法在面向对象的语言中是很注重的;

在上一例子的基础上,在res目录下新建目录anim;在下面新建xml文件,就做为Animation的布局文件。

xml文件编写方法:

1、首先加入set标签。<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">

2、再在该标签当中加入rotate,alpha,scale,translate标签;

在代码中用AnimationUtils静态函数,加载动画xml文件。

如:

[html] view
plaincopyprint?

<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">

<scale

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromXScale="1.0"

android:toXScale="1.4"

android:fromYScale="1.0"

android:toYScale="0.6"

android:pivotX="50%"

android:pivotY="50%"

android:fillAfter="false"

android:duration="1000" />

<set android:interpolator="@android:anim/decelerate_interpolator">

<scale

android:fromXScale="1.4"

android:toXScale="0.0"

android:fromYScale="0.6"

android:toYScale="0.0"

android:pivotX="50%"

android:pivotY="50%"

android:startOffset="700"

android:duration="400"

android:fillBefore="false" />

<rotate

android:fromDegrees="0"

android:toDegrees="50"

android:toYScale="1.0"

android:pivotX="50%"

android:pivotY="50%"

android:startOffset="700"

android:duration="1400" />

</set>

</set>

上面在一个xml文件中定义了 一系列的动画效果,注意:

android:pivotX="50"这种方法使用绝对位置定位,相当于代码中的 Animation.ABSOLUTE

android:pivotX="50%"这种方法相对于控件本身定位,相当于代码中的 Animation.RELATIVE_TO_SELF

android:pivotX="50%p" 这种方法相对于控件 的父控件定位,相当于代码中上 Animation.RELATIVE_TO_PARENT

如果需要调用 这个动画效果到某个控件 ,可以用下面的方法,给图片添加上面的一系列的动画效果,如果只希望有一个动画效果 ,那么也是一个xml文件:

[java] view
plaincopyprint?

class AlpahListener implements OnClickListener{

@Override

public void onClick(View v) {

Animation animation = AnimationUtils.loadAnimation(HelloAnimationActivity.this, R.anim.animation_test);

iv.startAnimation(animation);

}

}

这只是一段监听器代码。

另外,如果我们直接在xml文件里面,直接加两个或多个效果:如下面:

[html] view
plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

android:interpolator="@android:anim/accelerate_interpolator"

android:shareInterpolator="true">

<alpha

android:fromAlpha="1.0"

android:toAlpha="0.0"

android:startOffset="500"

android:duration="2000" />

<rotate android:fromDegrees="0"

android:toDegrees="360"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000" />

</set>

这时候,如果给控件添加这个动画效果,那么这个xml文件里面的alpha和rotate会叠加,而且这两个动画效果共享@android:anim/accelerate_interpolator.都是逐渐加速;如果需要单个设置速率就需要在每个效果里添加属性

[html] view
plaincopyprint?

android:interpolator="@android:anim/accelerate_interpolator"

Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

AccelerateDecelerateInterpolator在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator在动画的以均匀的速率改变
Frame-by-FrameAmimations这一类可以创建一个Drawable序列:这些Drawable可以按照指定的时间间歇一个一个的显示,就像win7桌面背景图片切换一样,循环切换;

使用方法:定义资源文件xml:

[html] view
plaincopyprint?

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

android:oneshot="true">

<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />

<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />

<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />

</animation-list>

其中item是轮流切换的图片;

在代码中使用资源文件:

[java] view
plaincopyprint?

AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);

rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

}

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

rocketAnimation.start();

return true;

}

return super.onTouchEvent(event);

}

如果我们要给每一个动画添加监听器:如下面代码 :

[java] view
plaincopyprint?

public class MainActivity extends Activity {

/** Called when the activity is first created. */

private Button removeButton = null;

private Button addButton = null;

private ImageView imageView = null;

private ViewGroup viewGroup = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

removeButton = (Button)findViewById(R.id.removeButtonId);

imageView = (ImageView)findViewById(R.id.imageViewId);

removeButton.setOnClickListener(new RemoveButtonListener());

viewGroup = (ViewGroup)findViewById(R.id.layoutId);//这里viewGroup是LinearLayout的id,这个viewGroup包括了下面所有的控件。

addButton = (Button)findViewById(R.id.addButtonId);

addButton.setOnClickListener(new AddButtonListener());

}

private class AddButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

//创建了一个淡入效果的Animation对象

AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);

animation.setDuration(1000);

animation.setStartOffset(500);

//创建一个新的ImageView

ImageView imageViewAdd = new ImageView(MainActivity.this);

imageViewAdd.setImageResource(R.drawable.icon);

//将新的ImageView添加到viewGroup当中

viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

//启动动画

imageViewAdd.startAnimation(animation);

}

}

private class RemoveButtonListener implements OnClickListener{

@Override

public void onClick(View v) {

//创建一个淡出效果的Animation对象

AlphaAnimation animation = new AlphaAnimation(1.0f,0.0f);

//为Animation对象设置属性

animation.setDuration(1000);

animation.setStartOffset(500);

//为Animation对象设置监听器

animation.setAnimationListener(new RemoveAnimationListener());

imageView.startAnimation(animation);

}

}

private class RemoveAnimationListener implements AnimationListener{

//该方法在淡出效果执行结束之后被调用

@Override

public void onAnimationEnd(Animation animation) {

System.out.println("end");

//从viewGroup当中删除掉imageView控件

viewGroup.removeView(imageView);

}

//该方法在动画重复执行的时候调用

@Override

public void onAnimationRepeat(Animation animation) {

System.out.println("repeat");

}

//该方法在动画开始执行的时候,调用 。

@Override

public void onAnimationStart(Animation animation) {

System.out.println("start");

}

}

}

在实际应用中,后者可能会多一些,但是由于写xml的时候,编译器很多地方不会报错,所以维护也是一个问题;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: