1.涉及的类及接口
AnimationsAnimationSet
Interpolator
Frame-By-Frame Animations
LayoutAnimationsController
AnimationListener
2.Animations介绍
Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。
3.Animation分类
Android提供了两种动画机制补间动画和帧动画。补间动画进行一系列的简单的视图变换(透明度、位置、大小、旋转等);而帧动画进行逐一加载一系列绘制资源。两种动画可以用在任
何视图的目的是提供简单旋转定时器,活动图标,和其他有用的UI元素。补间动画是调用系统
包(android.view.animation)办理;帧动画由AnimationDrawable类处理。
4.Animation类
1.常量
intABSOLUTE:指定的尺寸是象素的绝对数目int INFINITE:无限重复
int RELATIVE_TO_PARENT:指定的浮动维度,并应进行动画的高度或宽度对象的与父控件相应相乘
int RELATIVE_TO_SELF:指定的浮动维度,并应进行动画的高度或宽度对象的与本身相应相乘
int RESTART:当动画到达末尾和重复次数是无限REPEATER或正值,动画从头重新开始
int REVERSE:当动画到达末尾和重复次数是无限REPEATER或正值,动画向后(然后再向前)播放
int START_ON_FIRST_FRAME:可以作为开始时间,指示的开始时间应该是当getTransformation(长,变换)被调用用于第一动画帧的当前时间
int ZORDER_BOTTOM:动画在其他动画执行后再执行
int ZORDER_NORMAL:动画保持当前Z顺序
int ZORDER_TOP:动画优于其他动画执行
2.常用属性及方法
XML属 性:android:detachWallpaper关联方法:setDetachWallpaper(boolean)
注释说明:特殊选项窗口动画:如果这个窗口是墙纸的顶部,墙纸不与窗口同时动画
XML属 性:android:duration
关联方法:setDuration(long)
注释说明:动画执行时间
XML属 性:android:fillAfter
关联方法:setFillAfter(boolean)
注释说明:如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
XML属 性:android:fillBefore
关联方法:setFillBefore(boolean)
注释说明:如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
XML属 性:android:fillEnabled
关联方法:setFillEnabled(boolean)
注释说明:如果设置为true,将fillBefore设置考虑在内
XML属 性:android:interpolator
关联方法:setInterpolator(Interpolator)
注释说明:设置动画的变化速率
XML属 性:android:repeatCount
关联方法:setRepeatCount(int)
注释说明:设置动画重复执行的次数
XML属 性:android:repeatMode
关联方法:setRepeatMode(int)
注释说明:当到达结束和重复计数大于0或无穷大,定义动画行为。
XML属 性:android:startOffset
关联方法:setStartOffset(long)
注释说明:设置动画执行之前的等待时间
XML属 性:android:zAdjustment
关联方法:setZAdjustment(int)
注释说明:允许内容的Z-次序的调整被动画的动画的持续时间
3.常用方法:
void cancel():取消动画boolean hasEnded():动画是否结束
boolean hasStarted():动画是否开始
void reset():重置动画
boolean getTransformation(long currentTime, Transformation outTransformation, float scale)
获取转换的指定时间点
void start():启动动画首次getTransformation(长,转换)被调用
void startNow():在当前时间以毫秒为单位启动动画
boolean getTransformation(long currentTime, Transformation outTransformation, float scale)
boolean getTransformation(long currentTime, Transformation outTransformation)
获取动画指定时间currentTime的Transformation(此方法用于重写)
4.子类
AnimationSet:AnimationSet包含了一系列的AnimationAlphaAnimation:淡入淡出效果
TranslateAnimation:移动效果
ScaleAnimation:缩放效果
RotateAnimation:旋转效果
5.AlphaAnimation类 - 淡入淡出效果
1.构造方法
public AlphaAnimation (Context context, AttributeSet attrs)参数:
context:当前上下文
attrs:xml中读取的属性设置
public AlphaAnimation (float fromAlpha, float toAlpha)
参数:
fromAlpha:开始的透明度
toAlpha:结束的透明度
2.动画实例
1.XML属性
a)xml定义alha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
</set>
b)代码加载
// 使用AnimationUtils装载动画配置文件
Animation animation = AnimationUtils.loadAnimation(
this, R.anim.alpha);
// 启动动画
image.startAnimation(animation);
2.代码加载
// 1.创建一个AnimationSet对象,参数为Boolean型
// true表示使用Animation的interpolator,false则是使用自己的
AnimationSet as = new AnimationSet(true);
// 2.创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
// 第二个参数:toAlpha 结束的透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
// 3.设置动画属性
//设置动画执行的时间
alphaAnimation.setDuration(3000);
// 4.将alphaAnimation对象添加到AnimationSet当中
as.addAnimation(alphaAnimation);
// 5.使用ImageView的startAnimation方法执行动画
img.startAnimation(as);
6.ScaleAnimation类 - 缩放效果
1.构造方法
public ScaleAnimation(Context context, AttributeSet attrs)参数:
context:当前上下文
attrs:xml中读取的属性设置
public ScaleAnimation(float fromX, float toX, float fromY, float toY)
参数:
fromX:x轴的初始值
toX:x轴收缩后的值
fromY:Y轴的初始值
toY:Y轴收缩后的值
public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
参数:
fromX:x轴的初始值
toX:x轴收缩后的值
fromY:Y轴的初始值
toY:Y轴收缩后的值
pivotX:该点关于该对象正在被按比例,指定为绝对数目,其中0是左侧边缘的X坐标(即使对象改变大小,这点保持不变)
pivotY:该点关于该对象正在被按比例,指定为绝对数目,其中0是顶部边缘的Y坐标(即使对象改变大小,这点保持不变)
实际上默认坐标类型为RELATIVE_TO_SELF
public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
参数:
fromX:x轴的初始值
toX:x轴收缩后的值
fromY:Y轴的初始值
toY:Y轴收缩后的值
pivotXType:确定x轴坐标的类型
pivotXValue:x轴的值为 pivotXValue * 相对值(父控件或自身)
pivotYType:确定Y轴坐标的类型
pivotYValue:Y轴的值为 pivotXValue * 相对值(父控件或自身)
2.动画实例
1.XML属性
a)xml定义 - 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">
<span style="white-space:pre"> </span> <!--
<span style="white-space:pre"> </span>起始x轴坐标
止x轴坐标
始y轴坐标
<span style="white-space:pre"> </span>止y轴坐标
轴的坐标
轴的坐标 -->
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
ndroid:duration="1000"/>
</set>
b)代码加载
Animation animation = AnimationUtils.loadAnimation(
this, R.anim.scale);
image.startAnimation(animation);
2.代码加载
<span style="white-space:pre"> </span>AnimationSet animationSet = new AnimationSet(true);
//参数1:x轴的初始值
//参数2:x轴收缩后的值
//参数3:y轴的初始值
//参数4:y轴收缩后的值
//参数5:确定x轴坐标的类型
//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数7:确定y轴坐标的类型
//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
ScaleAnimation scaleAnimation = new ScaleAnimation(
1f, 1f,1.1f,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0f);
scaleAnimation.setDuration(3000);
animationSet.addAnimation(scaleAnimation);
animationSet.setFillAfter(true);
layout.startAnimation(animationSet);
7.TranslateAnimation类 - 移动效果
1.构造方法
public TranslateAnimation(Context context, AttributeSet attrs)参数:
context:当前上下文
attrs:xml中读取的属性设置
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
参数:
fromXDelta:动画开始时X坐标
toXDelta:动画结束时X坐标
fromYDelta:动画开始时坐标
toYDelta:动画结束时Y坐标
实际上默认坐标类型为RELATIVE_TO_SELF
public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
参数:
fromXType:动画开始时,指定x坐标维度类型
fromXValue:动画开始时X坐标
toXType:动画结束时,指定x坐标维度类型
toXValue:动画结束时X坐标
fromYType:动画开始时,指定Y坐标维度类型
fromYValue:动画开始时Y坐标
toYType:动画结束时,指定Y坐标维度类型
toYValue:动画结束时Y坐标
2.动画实例
1.XML属性
a)xml定义 - 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">
<!--
始x轴坐标
止x轴坐标
始y轴坐标
止y轴坐标
-->
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"/>
</set>
b)代码加载
<pre name="code" class="java"> Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(animation);
AnimationSet animationSet = new AnimationSet(true);
//参数1~2:x轴的开始位置
//参数3~4:y轴的开始位置
//参数5~6:x轴的结束位置
//参数7~8:x轴的结束位置
TranslateAnimation translateAnimation =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,1f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,1f);
translateAnimation.setDuration(3000);
animationSet.addAnimation(translateAnimation);
img.startAnimation(animationSet);
8.RotateAnimation类 - 旋转效果
1.构造方法
2.动画实例
1.XML属性
a)xml定义 - 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">
<!--
fromDegrees:开始的角度
toDegrees:结束的角度,+表示是正的
pivotX:用于设置旋转时的x轴坐标
例
1)当值为"50",表示使用绝对位置定位
2)当值为"50%",表示使用相对于控件本身定位
3)当值为"50%p",表示使用相对于控件的父控件定位
pivotY:用于设置旋转时的y轴坐标
-->
<rotate
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
b)代码加载
Animation animation = AnimationUtils.loadAnimation(
this, R.anim.rotate);
image.startAnimation(animation);<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
2.代码加载
AnimationSet animationSet = new AnimationSet(true);
//参数1:从哪个旋转角度开始
//参数2:转到什么角度
//后4个参数用于设置围绕着旋转的圆的圆心在哪里
//参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数5:确定y轴坐标的类型
//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0f);
// 设置旋转属性
rotateAnimation.setDuration(3000);
animationSet.addAnimation(rotateAnimation);
img.startAnimation(animationSet);
9.AnimationSet - Animation系列
1.构造方法
AnimationSet(Context context, AttributeSet attrs)参数
context:当前上下文
attrs:xml中读取的属性设置
AnimationSet(boolean shareInterpolator)
参数
shareInterpolator:是否使用自身的插入器,false使用自身的插入器
2.常用方法
void addAnimation(Animation a) 加入动画List<Animation>getAnimations() 获取动画列表
void reset() 重置动画
10.Interpolator - 动画的变化速率
1.Anroid框架定义的变化速率
AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator:动画以均匀的速率改变
2.set定义
1.定义
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"/>
2.如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator
android:shareInterpolator="true"
3如果是在代码上设置共享一个interpolator,则可以在AnimationSet设置interpolator
AnimationSet animationSet = newAnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
4.如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator
AnimationSet animationSet = newAnimationSet(false);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
rotateAnimation.setInterpolator(new DecelerateInterpolator());
11.Frame-By-Frame Animations - 帧动画
1.anim.xml <?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/a_01" android:duration="50"/>
<item android:drawable="@drawable/a_02" android:duration="50"/>
<item android:drawable="@drawable/a_03" android:duration="50"/>
<item android:drawable="@drawable/a_04" android:duration="50"/>
<item android:drawable="@drawable/a_05" android:duration="50"/>
<item android:drawable="@drawable/a_06" android:duration="50"/>
</animation-list
2.java调用
imageView.setBackgroundResource(R.anim.anim);
AnimationDrawable animationDrawable = (AnimationDrawable)
imageView.getBackground();
animationDrawable.start();
12.LayoutAnimationsController - 用于实现使多个控件按顺序一个一个的显示
1.介绍
布局动画控制器器用于布局动画或视图组的子视图。每个子视图执行相同的动画,但对于每个子视图而言,动画开始在不同的时间。布局动画控制器据
ViewGrop计算的动画延迟,每个子视图开始时必须将延迟抵消。该标准的执
行将计算由子视图在视图组的指数乘以毫秒的延迟时间。子视图都应该覆盖
getDelayForView(android.view.View)实施计算的延迟的不同的方式。例
如,GridLayoutAnimationController将计算基于子视图的父视图组的列和
行索引的延迟,计算出的子视图延迟信息存储在LayoutAnimationController.Animation
参数的一个实例,它本身存储在所述视图的ViewGroup.LayoutParams中。
2.直接子类
GridLayoutAnimationController3.相关XML属性和关联方法
XML属 性:android:animation关联方法:setAnimation(Animation)
注释说明: 引用动画效果文件
XML属 性:android:animationOrder
关联方法:setOrder(int)
注释说明:动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
XML属 性:android:delay
关联方法:setDelay(float delay)
注释说明:动画间隔时间;子类动画时间间隔(延迟)
70% 也可以是一个浮点数 如“1.2”等
XML属 性:android:interpolator
关联方法:setInterpolator(Context,int)
注释说明:动画加速率
4.静态常量
ORDER_NORMAL:动画顺序为序列的顺序ORDER_RANDOM:动画的顺序为随机顺序
ORDER_REVERSE:动画的顺序为反向顺序
5.构造方法
LayoutAnimationController(Context context, AttributeSet attrs)参数
context:当前上下文
attrs:xml中读取的属性设置
LayoutAnimationController(Animation animation)
参数
animation:动画效果文件
默认delay为50%,
LayoutAnimationController(Animation animation, float delay)
参数
animation:动画效果文件
delay:延迟,默认delay为50%,
6.效果实例
1.XML实现a)创建动画效果:list_anim_layout.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="1000"/>
</set>
b)创建LayoutAnimationController
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
c)布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试" />
<ListView
android:id="@+id/lv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/list_anim_layout"
android:scrollbars="vertical" />
</LinearLayout>
d)代码调用
public class LayoutAnimationsController extends AppCompatActivity {
private Button button;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aty_b);
listView = (ListView) findViewById(R.id.lv_test);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}
private ListAdapter createListAdapter() {
List<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
HashMap<String,String> m1 = new HashMap<String,String>();
m1.put("name", "bauble");
m1.put("sex", "male");
HashMap<String,String> m2 = new HashMap<String,String>();
m2.put("name", "Allorry");
m2.put("sex", "male");
HashMap<String,String> m3 = new HashMap<String,String>();
m3.put("name", "Allotory");
m3.put("sex", "male");
HashMap<String,String> m4 = new HashMap<String,String>();
m4.put("name", "boolbe");
m4.put("sex", "male");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,list,R.layout.item,new String[]{"name","sex"},
new int[]{R.id.name,R.id.sex});
return simpleAdapter;
}
private class ButtonListener implements View.OnClickListener {
public void onClick(View v) {
listView.setAdapter(createListAdapter());
}
}
}
2.代码实现
// LayoutAnimations 应设置在setAdapter之后
listView.setAdapter(createListAdapter());
// 1.创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;
Animation animation = (Animation) AnimationUtils.loadAnimation(
LayoutAnimationsController.this, R.anim.list_anim);
// 2.创建LayoutAnimationController对象:
LayoutAnimationController controller = new LayoutAnimationController(animation);
// 3.设置LayoutAnimationController属性
// 设置动画显示顺序
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
// 设置子视图动画延迟时间
controller.setDelay(0.5f);
// 4.为控件添加设置LayoutAnimationController属性
listView.setLayoutAnimation(controller);
12.AnimationListener
1.介绍
AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法2.主要方法
onAnimationEnd(Animation animation) - 当动画结束时调用onAnimationRepeat(Animation animation) - 当动画重复时调用
onAniamtionStart(Animation animation) - 当动画启动时调用