Android核心技术之(10)---动画(Aimation)

1.什么是动画

•动画有下面两种情况:
1.同一个图形通过视图在界面上进行透明度,缩放,旋转,平移的变化(View动画)
2.在界面的同一个位置上不断切换显示不同的图片(Drawable动画)

•动画的分类:
1.View Animation
1.1单一动画(Animation)
•缩放动画(ScaleAnimation)
•透明度动画(AlphaAnimation)
•旋转动画(RotateAnimation)
•平移动画(TranslateAnimation)

1.2复合动画(AnimationSet)
•由多个单一动画组合在一起的动画

2.Drawable Animation

•Android中提供了两种实现动画的方式:
1.纯编码的方式
2.Xml配置的方式

•动画在应用中是非常常见的界面效果, 也是提高用户体验的一种好手段

2.Aimation的公用功能

setDuration(long durationMillis): 设置持续时间(单位ms)
setStartOffset(long startOffset) : 设置开始的延迟的时间(单位ms)
•setFillBefore(boolean fillBefore) : 设置最终是否固定在起始状态
setFillAfter(boolean fillAfter) : 设置最终是否固定在最后的状态
•setAnimationListener(AnimationListener listener) : 设置动画监听

•坐标类型:
•Animation.ABSOLUTE  
•Animation.RELATIVE_TO_SELF   
•Animation.RELATIVE_TO_PARENT

•启动动画 : view.startAnimation(animation);
•结束动画: view.clearAnimation()

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

3.缩放动画(Code ScaleAnimation)


•fromX : 开始时X轴上的缩放比例  
•toX : 结束时X轴上的缩放比例  
•fromY :开始时Y轴上的缩放比例
•toY :结束时Y轴上的缩放比例
•pivotXType : X轴坐标的类型(计算x轴上的偏移量的方式)
•pivotXVlaue : 中心点在X轴相对视图左顶点在x轴上的偏移量
•pivotYType : Y轴坐标的类型(计算x轴上的偏移量的方式)
•pivotYValue : 中心点相对视图左顶点在y轴上的偏移量

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

   android:duration="2000"

   android:fromXScale="0.0"

   android:fromYScale="0.0"

   android:pivotX=“1"

   android:pivotY=“0.5“          

   android:toXScale="1.0"

   android:toYScale="1.0"

   android:fillAfter="true"/>


•Animation.ABSOLUTE:

  数值(默认以px为单位)   100


•Animation.RELATIVE_TO_SELF:

  百分数,如:50%(以当前视图的宽度或高度其为基数来计算)  

•Animation.RELATIVE_TO_PARENT:

  百分数+p,如:50%p(以父视图的宽度或高度其为基数来计算)


4.旋转动画(RotateAnimation)



•fromDegrees : 开始时的角度
•toDegrees : 结束时的角度

•pivotXType : X轴坐标的类型
•pivotXVlaue : X轴坐标的值
•pivotYType : Y轴坐标的类型
•pivotYValue : Y轴坐标的值

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

   android:duration="5000"

   android:fromDegrees="+90“

    android:toDegrees="-90"

   android:pivotX="0%"

   android:pivotY="0%"/>



5.透明度动画(AlphaAnimation)

•fromAlpha: 开始时的透明比例
•toAlpha: 结束时的透明比例

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

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

android:fromAlpha="1.0"

android:toAlpha="0.0"

android:startOffset="500"

android:duration="1000" />



6.平移动画(TranslateAnimation)


•fromXType: 坐标类型
•fromXValue: 开始时X轴的坐标
•toXType:坐标类型
•toXValue: 结束时X轴的坐标
•fromYType:坐标类型
•fromYValue: 开始时Y轴的坐标
•toYType:坐标类型
•toYValue: 结束时Y轴的坐标

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

   android:duration="2000"

   android:fromXDelta="-100%p"

   android:fromYDelta="0%"

   android:toXDelta="0%"

   android:toYDelta="0%" />


7.复合动画(Code AnimationSet)

// 复合动画对象

AnimationSet animationSet = new AnimationSet(false);

// 添加一个单一动画

animationSet.addAnimation(alpha);

animationSet.addAnimation(rotate);

//开启动画

iv_animation.startAnimation(animationSet);


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

   <alpha

        android:duration="2000"

        android:fromAlpha="0.0"

        android:toAlpha="1.0"/>

    <rotate

        android:duration="1000"

        android:fromDegrees="0"

        android:pivotX="50%"

        android:pivotY="50%“

       android:toDegrees="360"/>

</set>


8.Interpolator属性的使用

Interpolator被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repeated(重复)等。

•@android:anim/linear_interpolator  : 线性变化

•@android:anim/accelerate_interpolator  : 加速变化

•@android:anim/decelerate_interpolator  : 减速变化

•@android:anim/cycle_interpolator: 周期循环变化


9. 动画监听

•在程序中可以对动画的特定时刻进行监听
•Animation.setAnimationListener(AnimationListenerlistener) : 设置动画监听
•动画监听器: AnimationListener
•onAnimationStart(Animationanimation) : 动画开始的回调
•onAnimationEnd(Animationanimation) : 动画结束的回调
•onAnimationRepeat(Animationanimation) : 动画重复执行

10.动画配置

<animation-list

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

android:oneshot="false">

<itemandroid:drawable="@drawable/nv1"android:duration="500" />

<itemandroid:drawable="@drawable/nv2"android:duration="500"/>

<itemandroid:drawable="@drawable/nv3"android:duration="500" />

<itemandroid:drawable="@drawable/nv4"android:duration="500" />

</animation-list>


<ImageView

   android:id="@+id/iv_dv"

   android:layout_width=“80dp"

   android:layout_height=“80dp"

   android:layout_marginTop="160dp"

    android:background="@drawable/anim_da"/>


11.启动Drawable动画


//得到背景动画图上

AnimationDrawable ad =null;

ad = (AnimationDrawable) imageView.getBackground();

//启动动画

ad.start();

//停止动画

ad.stop();












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值