Android动画机制(一)

来源:互联网 发布:腾讯mac管家 编辑:程序博客网 时间:2024/06/04 17:59

Android动画相关概念

在Android 3.0之前,动画分为Tween Animation、Frame Animation。

Tween Animation 补间动画定义在xml文件中。可以对view实现一系列的转换,例如:移动、渐变、伸缩、旋转。
Frame Animation 逐帧动画是一系列的图片按顺序显示。

这两个都属于View Animation的范畴。在Android 3.0之后,增加了Property Animation(属性动画)

Tween Animation

Tween Animation属性

-set
|-android:interpolator -> 插值器,影响动画的速度
|-默认值 -> @android:anim/accelerate_decelerate_interpolator
|-android:shareInterpolator -> 集合所有动画是否使用同一插值器
|-android:fillAfter -> 动画结束后View是否停留在结束的位置
|-android:startOffset -> 动画多少秒之后执行
|-android:repeatMode -> 重复的模式,默认为restart,即重头开始重新运行,reverse即从结束开始向前重新运行

-TranslateAnimation -> 移动View
|-< translate >
|-android:fillAfter ->
|-android:duration -> 表示动画持续的时间
|-android:fromXDelta -> 表示 x 的起始值
|-android:toXDelta -> 表示 x 的结束值
|-android:fromYDelta -> 表示 y 的起始值
|-android:toYDelta -> 表示 y 的结束值

-scaleAnimation -> 放大或者缩小View
|-< scale >
|-android:duration -> 表示动画持续的时间
|-android:fromXScale -> 表示水平方向缩放的起始值
|-android:fromYScale -> 表示竖直方向缩放的起始值
|-android:pivotX -> 表示缩放中心点的 X 坐标
|-android:pivotY -> 表示缩放中心点的 Y 坐标
|-android:toXScale -> 表示水平方向缩放的结束值
|-android:toYScale -> 表示竖直方向缩放的结束值

-RotateAnimation -> 旋转View
< rotate >
|-android:duration -> 表示动画持续的时间
|-android:fromDegrees -> 旋转开始的角度
|-android:toDegrees -> 旋转结束的角度
|-android:pivotX -> 旋转中心点的 X 坐标
|-android:pivotY -> 旋转中心点的 Y 坐标

-AlphaAnimation -> 改变View的透明度
|- < alpha >
|-android:duration -> 表示动画持续的时间
|-android:fromAlpha -> 透明度的起始值
|-android:toAlpha -> 透明度的结束值

Tween Animation的使用

路径:/myactivity/app/src/main/res/anim/move_in.xml

实现功能:简单的平移然后颜色渐变。

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="400"     android:fillAfter="false">    <translate        android:duration="100"        android:fromXDelta="0"        android:toXDelta="200"/>    <alpha        android:duration="200"        android:fromAlpha="0"        android:startOffset="200"        android:toAlpha="1"/></set>

Tween Animation XML形式的使用

        Animation animation = AnimationUtils.loadAnimation(getActivity(),R.anim.move_in);                animationBt.startAnimation(animation);
 注意:在XML文件中定义动画,可读性好~

Tween Animation Java形式的使用

  Animation animation = AnimationUtils.loadAnimation(getActivity(),R.anim.move_in);                animationBt.startAnimation(animation);                /*TranslateAnimation translateAnimation = new TranslateAnimation(0,500,0,500);                translateAnimation.setDuration(1000);                translateAnimation.setFillAfter(true);                animationBt.startAnimation(translateAnimation);*/                animation.setAnimationListener(new Animation.AnimationListener() {                    @Override                    public void onAnimationStart(Animation animation) {                        Log.d("translateAnimation","onAnimationStart");                    }                    @Override                    public void onAnimationEnd(Animation animation) {                        Log.d("translateAnimation","onAnimationEnd");                    }                    @Override                    public void onAnimationRepeat(Animation animation) {                        Log.d("translateAnimation","onAnimationRepeat");                    }                });
注意:注释的代码块也可以替换执行,表示某一个动画的监听。

Frame Animation在实际项目中运用较少,这里不做实例演示了。

LayoutAnimation

LayoutAnimation是对一组控件的操作,如定义viewgroup里的子控件的动画,支持子控件动画执行的间隔和顺序。

xml中的LayoutAnimation的运用

首先定义一个子控件执行的动画,路径:myactivity/app/src/main/res/anim/move_in.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="2000"     android:fillAfter="false">    <translate        android:duration="600"        android:fromYDelta="0"        android:toYDelta="1000"/>    <alpha        android:duration="1400"        android:fromAlpha="1"        android:startOffset="400"        android:toAlpha="0"/></set>

然后在myactivity/app/src/main/res/anim/layout_animation.xml中引入此子控件动画

<?xml version="1.0" encoding="utf-8"?><layoutAnimation    xmlns:android="http://schemas.android.com/apk/res/android"    android:animation="@anim/move_in"    android:animationOrder="reverse"    android:delay="10%"></layoutAnimation>

最后在要定义LayoutAnimation的xml文件中引用此LayoutAnimation动画

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/animation_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layoutAnimation="@anim/layout_animation"    android:gravity="center_horizontal"    android:orientation="vertical">    <Button        android:id="@+id/button17"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button4"        android:layout_alignBottom="@+id/button4"        android:layout_toEndOf="@+id/button14"        android:layout_toRightOf="@+id/button14"        android:text="展示"/>    <Button        android:id="@+id/button8"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button14"        android:layout_alignBottom="@+id/button14"        android:layout_toEndOf="@+id/button14"        android:layout_toRightOf="@+id/button14"        android:text="展示"/></RelativeLayout>

这样,该动画就会自动运行。

在java代码中执行LayoutAnimation动画

区别就在于最后引用的部分,其他的都没变。

 LayoutAnimationController layoutAnimationController = AnimationUtils                        .loadLayoutAnimation(getActivity(), R.anim                                .layout_animation);                animationLayout.setLayoutAnimation(layoutAnimationController);                animationLayout.startLayoutAnimation();
注意:animationLayout就是要展示LayoutAnimation动画的viewgroup。

由于篇幅原因,我们在下一篇整篇学习下属性动画~

0 0
原创粉丝点击