动画总结【Android】

来源:互联网 发布:潍坊行知学校复读 编辑:程序博客网 时间:2024/04/28 08:56
Android中提供了两种实现动画的方式
纯编码的方式
Xml配置的方式
单一动画(Animation)
复合动画(AnimationSet)
坐标类型:
Animation.ABSOLUTE绝对坐标
Animation.RELATIVE_TO_SELF相对自己
Animation.RELATIVE_TO_PARENT相对父容器
setDuration(long 微秒):设置持续时间
setStartOffset(long 微秒):设置开始的延迟的时间
setFillBefore(boolean fillBefore):设置最终是否固定在起始状态
setFillAfter(boolean fillAfter):设置最终是否固定在最后的状态
setAnimationListener(AnimationListener listener):设置动画监听
启动动画:view.startAnimation(animation)
结束动画:view.clearAnimation()

动画监听器:AnimationListener
onAnimationStart(Animation animation):动画开始的回调
onAnimationEnd(Animation animation):动画结束的回调
onAnimationRepeat(Animation animation):动画重复的回调


<span style="font-size:14px;color:#000000;">tv_animation_msg.setText("动画监听");RotateAnimation animation = new RotateAnimation(-90, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);animation.setDuration(5000);animation.setRepeatCount(3);animation.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {Log.e("tag", "动画开始");}@Overridepublic void onAnimationRepeat(Animation animation) {Log.e("tag", "动画重复");}@Overridepublic void onAnimationEnd(Animation animation) {Log.e("tag", "动画结束");}});iv_animation.startAnimation(animation);</span>

一、缩放动画
1.Code ScaleAnimation
ScaleAnimation(float fromx,float toX,float fromY,float toY,int X轴坐标类型,float 横坐标值,int pivotYType,float 纵坐标值);
注意:里面需要填入数值的数必须后面加上f,因为是float型
2.Xml ScaleAnimation
(1).定义动画文件
eclipse中新家一个xml文件,把Resource Type改为Tween Animation
duration="ms"持续时间
fromXScale=""
fromYScale=""
toXScale=""
toYScale=""
pivotY="1"//横坐标位置
pivotX="1"//纵坐标位置  数字表示相对坐标,百分之几100%表示相对自己,百分之几后加f表示相对父容器
fillAfter="true"最终固定
(2).加载动画文件得到动画对象
AnimationUrils.loadAnimation(context,id)这个id指的是动画文件的id(文件名)
(3).启动动画
view.startAnimation(animation);

二、旋转动画
1.Code RotateAnimation
RotateAnimation(float开始角度,float结束角度,int x轴坐标的类型,float x轴坐标的值,int y,float y);
-90度逆时针是负的度数,顺时针是正的度数
-90, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
2.xml RoteAnimation
定义一个rotate的xml文件,剩下的和缩放的类似

三、透明度动画
1.Code AlphaAnimation
AlphaAnimation(float 开始透明度,float 结束透明度)
2.xml AlphaAnimation
新建一个alpha的xml文件,剩下的就不用说了

四、移动动画
1.Code TranslateAnimation
TranslateAnimation(x类型,x起始值,x类型,x终点值,y类型,y起始值,y类型,y重点值)
2.xml TranslateAnimation
新建一个translate的xml文件
android:fromXDelta="100%p"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"
五、复合动画
1.Code AnimationSet
  tv_animation_msg.setText("Code复合动画:透明度从透明到不透明,持续2秒,接着旋转360度,持续一秒");
  AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
  alphaAnimation.setDuration(2000);
  RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f);
  rotateAnimation.setDuration(1000);
  rotateAnimation.setStartOffset(2000);
  AnimationSet animationSet = new AnimationSet(true);
  animationSet.addAnimation(alphaAnimation);
  animationSet.addAnimation(rotateAnimation);
  iv_animation.startAnimation(animationSet);
2.xml AnimationSet
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000">      
    </alpha>
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="1000"
        android:startOffset="2000"
        android:pivotX="50%"
        android:pivotY="50%"> 
    </rotate>
</set>
Iterpolator属性的使用
Iterpolator被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果
accelerated(加速),decelerated(减速),repeated(重复)等
@android:anim/linear_interpolator:线性变化
@android:anim/accelerate_interpolator:加速变化
@android:anim/decelerate_interpolator:减速变化
@android:anim/cycle_interpolator:周期循环变化
animation.setInterpolator(new AccelerateInterpolator());


案列:一个小人一直走
大致需要四个图片
1.在drawable中新建一个animation-list的xml文件

<span style="font-size:14px;color:#000000;"><animation-list xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">    <item         android:drawable="@drawable/nv1"        android:duration="500"></item>        <item         android:drawable="@drawable/nv2"        android:duration="500"></item>        <item         android:drawable="@drawable/nv3"        android:duration="500"></item>        <item         android:drawable="@drawable/nv4"        android:duration="500"></item></animation-list></span>

2.在布局文件中的imageView的background中需要引用步骤1的文件名
3.在代码中

<span style="font-size:14px;color:#000000;">AnimationDrawable animationDrawable = null;@Overridepublic void onClick(View v) {switch (v.getId()) {case 开始的id:if (animationDrawable == null) {animationDrawable = (AnimationDrawable) iv_da_mm.getBackground();animationDrawable.start();}break;case 结束的id:if (animationDrawable!=null) {animationDrawable.stop();animationDrawable = null;}break;default:break;}}</span>

注意:在启动动画前需要该动画为null,不能在已启动动画的前提下再进行启动动画
 在关闭动画前需要进行判断该动画是否开启,在已开启的情况下进行关闭动画,在关闭动画后,需要将动画置为空




界面跳转时,界面是从右边平移进来的,而不是默认的跳出来的
需要在startActivity后面调用一个方法overridePendingTransition(要进入的动画,要退出的动画)
进入和退出的动画都需要创建一个动画的xml文件
进入的xml文件

<span style="font-size:14px;color:#000000;"><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXDelta="100%"    android:toXDelta="0"    android:duration="500"></translate></span>

退出时的xml文件

<span style="font-size:14px;color:#000000;"><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXDelta="0"    android:toXDelta="-100%"    android:duration="500" > </translate></span>

0 0