1.什么是Animations
- Animations提供了一系列的动画效果,这些效果可以应用在绝大多数的控件.
2.Animations的分类
- 代码中实现动画
- xml中使用动画
总体分两大类
第一类:
Tweened Animations
该类Animations提供了旋转,移动,伸展和淡出等等效果1.
Alpha
:淡入淡出效果2.
Scale
:缩放效果3.
Rotate
:旋转效果4.
Translate
:移动效果第二类:
Frame-by-Frame Animations
这一类Animations
可以创建一个Drawable
序列,这些Drawable
可以按照指定的时间间歇一个一个的显示;-
使用
Tweened Animations
的步骤
1.创建一个AnimationSet
对象
2.根据需要创建相应的Animation
对象
3.根据软件动画的需求,为Animation
对象设置相应的数据
4.将Animation对象添加到AnimationSet
对象当中
5.使用空间对象开始执行AnimationSet
-
Tween Animation的通用属性
1.setDuration(long durationMils)
设置动画持续时间(单位毫秒)
2.setFillAfter(boolean fillAfter)
如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
3.setFillBefore(boolean fillBefore)
如果fillBefore的值为true;则动画执行后,控件将回到动画执行之前的状态;
4.setStartOffset(long startOffSet)
设置动画执行之前的等待时间
5.setRepeatCount(int repearCount)
设置动画重复执行的次数
(在代码中实现动画)实例代码:
<pre><code>package mars.animations01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ImageView imageView = null;
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageViewId);
rotateButton = (Button) findViewById(R.id.rotateButtonId);
rotateButton.setOnClickListener(new RotateButtonListener());
scaleButton = (Button) findViewById(R.id.scaleButtonId);
scaleButton.setOnClickListener(new ScaleButtonListener());
alphaButton = (Button) findViewById(R.id.alphaButtonId);
alphaButton.setOnClickListener(new AlphaButtonListener());
translateButton = (Button) findViewById(R.id.translateButtonId);
translateButton.setOnClickListener(new TranslateButtonListener());
}
private class RotateButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
}
private class ScaleButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
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);
imageView.startAnimation(animationSet);
}
}
private class AlphaButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
//创建一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间(单位:毫秒)
alphaAnimation.setDuration(1000);
//将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
//使用ImageView的startAnimation方法开始执行动画
imageView.startAnimation(animationSet);
}
}
private class TranslateButtonListener implements OnClickListener {
@Override
public void onClick(View view) {
AnimationSet 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);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
}
}</pre></code>
(在xml文件中实现):
首先需要在res文件夹下新建anim文件夹
然后可以在anim文件夹中新建xml文件
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500" />
</set>
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" />
</set>
-
android:pivotX
="50" 这种方法是用绝对位置定位; -
android:pivotX
="50%"这种方法相对于控件本身定位; -
android:pivotX
="50%p"这种方法相对于控件的父控件定位
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
</set>
translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="50%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000" />
</set>
如何在java代码中使用anim:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
AnimationSet的使用方法
什么是AnimationSet
1.AnimationSet是Animation的子类,可以整合各种动画效果
2.一个AnimationSet包含了一系列的Animation
3.针对AnimationSet设置一些Animation的常见属性,可以被包含在AnimationSet当中的Animation集成
实例,实现渐变和旋转
public class MainActivity extends Activity {
private Button button = null;
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageViewId);
button = (Button) findViewById(R.id.scaleButtonId);
button.setOnClickListener(new AnimationButtonListener());
}
private class AnimationButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
/**
* Animation animation =
* AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
* imageView.startAnimation(animation);
*/
// 声明一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(false);
//false 设置android:shareInterpolator为false,true设置android:shareInterpolator为true
//animationSet.setInterpolator(new AccelerateInterpolator());
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setInterpolator(new DecelerateInterpolator());
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setInterpolator(new AccelerateInterpolator());
animationSet.addAnimation(alpha);
animationSet.addAnimation(rotate);
animationSet.setDuration(2000);
animationSet.setStartOffset(500);
imageView.startAnimation(animationSet);
}
}
}
Interpolator 的使用方法-控制动画使用效果(ex:前慢后快)
Interpolator
定义了动画变化的速率,在Animations
框架当中定义了以下几种
Interpolator
- AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速
- AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
- CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
- DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
-
LinearInterpolator:动画以均匀的速率改变
在xml文件中设置Interpolator
<?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>
如果shereInterpolator
= "false" 则需要对AnimationSet
中的每一个动画设置Interpolator
属性
Frame-By-Frame Animations的使用方法
在res/drawable当中创建一个xml文件,用于定义Animations的动画序列
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/nv1" android:duration="500" />
<item android:drawable="@drawable/nv2" android:duration="500" />
<item android:drawable="@drawable/nv3" android:duration="500" />
<item android:drawable="@drawable/nv4" android:duration="500" />
</animation-list>
使用方法
1.为imageView设置背景资源
imageView.setBackgroundResource(R.drawable.anim_nv);
2.通过ImageView得到AnimationDrawable
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
3.开始执行动画
animationDrawable.start();
LayoutAnimationController的使用方法
-
什么是LayoutAnimationController
- LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果;
2.每一个控件都有相同的动画效果
3.这些控件的动画效果在不同的时间显示出来
4.LayoutAnimationController可以在xml文件当中设置,也可以在代码当中进行设置。
在xml文件中使用LayoutAnimationController
在anim文件夹下创建一个新文件,名为list_anim_layout.xml文件
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="2"
android:animationOrder="normal" //有三个值random,normal,reverse
android:animation="@anim/list_anim" />
list_anim.xml文件
<?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="0.0"
android:toAlpha="1.0"
android:duration="2000" />
</set>
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"
//当在代码中使用LayoutAnimationController时,去掉此属性
/>
<Button
android:id="@+id/buttonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试" />
</LinearLayout>
代码中使用LayoutAnimationController
1.创建一个Animation对象
2.使用如下代码创建LayoutAnimationController对象:
LayoutAnimationController lac = new LayoutAnimationController(animation);
3.设置控件显示的顺序:
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
4.为ListView设置LayoutAnimationController属性
listView.setLayoutAnimation(lac);
Animation animation = (Animation) AnimationUtils.loadAnimation(
MainActivity.this, R.anim.list_anim);
LayoutAnimationController lac = new LayoutAnimationController(
animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
lac.setDelay(0.5f);
listView.setLayoutAnimation(lac);
AnimationListenter的使用方法
1.AnimationListenter是一个监听器
2.该监听器在动画执行的各个阶段会得到通知,而调用相应的方法
3.主要包含以下的三个方法
1.onAnimationEnd(Animation animation)
2.onAnimationRepeat(Animation animation)
3.onAnimationStart(Animation animation)
代码示例:
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);
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");
}
}
}