您的位置:首页 > 移动开发 > Android开发

Android-Animation动画(变换动画,帧动画,布局动画)

2015-09-12 01:02 435 查看

1.回顾

   上篇学习了 封装 一般常用的Adpater 为jar ,方便使用;

2. 重点
   (1)变换动画(TweenAnimation)
   (2)帧动画   (FrameAnimation)
   (3)布局动画 (LayoutAnimation)

3.变换动画
   3.1 四个基本的变换
         Alpha :渐变;   Scale : 渐变尺寸;   Translate :位置移动动画;  Rotate:旋转动画;

   3.2 常用属性:

     (1)  Duration :动画时间;

     (2)fillAfter :为true 动画转换为 动画结束后应用;

     (3)fillBefore :true 动画转换 直接被 应用;

     (4)interpolator :动画插入器(加速,减速插入器);

     (5)repeatCount  : 动画重复次数;

     (6)repateMode   :顺序重复 、倒叙重复;

     (7)startOffset  : 动画之间的时间间隔;

   3.3 实现方式:

     配置文件:alpha , scale ,translate ,rotate ;

     代码实现:AlphaAnimation ,ScaleAnimation ,TranslateAnimation,RotateAnimation;

   3.4 Alpha 实现
     (1)代码实现
Animation animation = new AlphaAnimation(0.1f, 1.0f);
animation.setDuration(5000);
imageview1.startAnimation(animation);


    (2)布局实现
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1.0"
/>
</set>


          加载:
Animation animation2 = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.alpha);
imageview1.startAnimation(animation2);


   3.5 Scale 缩放
     (1)代码实现
/**
* 参数:
* 0.0~1.0
* float fromX :x开始比例
* float toX   :x结束比例
* float fromY :y开始比例
* float toY,  :y结束比例
* int pivotXType    :Animation.RELATIVE_TO_SELF 相对与自己缩放
* float pivotXValue :x缩放的位置 0.0~1.0 ;0.5为 中心
* int pivotYType    :Animation.RELATIVE_TO_SELF 相对与自己缩放
* float pivotYValue :y缩放的位置 0.0~1.0 ;0.5为 中心
*
*/
Animation animation = new ScaleAnimation(1.0f, 0.3f, 1.0f, 0.3f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
//缩放结束后,保持为改比例
animation.setFillAfter(true);
imageview1.startAnimation(animation);


   (2)布局实现
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<!--
**duration 时间
**fillAfter 结束后是否保持状态
**fromXScale x缩放初始值
**fromYScale y缩放初始值
**interpolator 插入器
**pivotx x缩放位置
**pivotY y缩放位置
**toXScale x缩放最终值
**toYScale y缩放最终值

-->

<scale
android:duration="2000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />

</set>


   调用:
Animation anim= AnimationUtils.loadAnimation(this, R.anim.scale);
image.startAnimation(anim);


   3.6 Translate 移动
     (1)代码实现
/**
* 参数:
* int fromXType ,x初始相对位置
* float fromXValue, x 初始位置
* int toXType,  x 目标相对位置
* float toXValue,  x目标位置
* int fromYType, y 初始相对位置
* float fromYValue, y 初始位置
* int toYType,   y目标相对位置
* float toYValue y目标位置
*
* 下面实现 平移效果
*
*/

Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(1000);
imageview1.startAnimation(animation);


     (2)布局实现
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="1000"
android:fromXDelta="10"
android:fromYDelta="10"
android:toXDelta="100"
android:toYDelta="100" />

</set>


   3.7 Rotate 旋转效果
      (1)代码实现
/**
* 参数:
* float fromDegrees, 开始角度
* float toDegrees,  旋转的角度
* int pivotXType,   x相对属性 Animation.RELATIVE_TO_SELF
* float pivotXValue, 旋转中心 x
* int pivotYType,   y相对属性 Animation.RELATIVE_TO_SELF
* float pivotYValue  旋转中心 y
*
* 旋转180度
*/

Animation animation = new RotateAnimation(0.0f, 180.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setFillAfter(true);
imageview1.startAnimation(animation);


    (2)布局实现
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360" />

</set>


   调用:
Animation anim= AnimationUtils.loadAnimation(this, R.anim.rotate);
image.startAnimation(anim);


4.帧动画 FrameAnimation
  (1)实现布局 animation-list 
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:drawable="@drawable/one"
android:duration="500"/>
<item
android:drawable="@drawable/two"
android:duration="500"/>
<item
android:drawable="@drawable/three"
android:duration="500"/>
<item
android:drawable="@drawable/four"
android:duration="500"/>
<item
android:drawable="@drawable/five"
android:duration="500"/>
<item
android:drawable="@drawable/six"
android:duration="500"/>

</animation-list>


   (2)加载
      ImageView加载 实现动画;
image.setImageResource(R.drawable.anim_list);


5. 布局动画
  (1) 实现LayoutAnimationController 实现加载动画;

LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);


  (2)动画实现 渐进进入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator" >

<scale
android:duration="1000"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1.0" />
</set>


   (3)动画实现 渐进渐出
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top" >

<scale
android:duration="@android:integer/config_mediumAnimTime"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:toXScale="0.1"
android:toYScale="0.1" />

<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0" />

</set>


6. Activity 切换动画
    使用 overridePendingTransition实现加载动画;
    实例:
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);


 7.总结
   这里仅仅是最基本的动画,更复杂的动画效果,需要组合完成;下篇学习Android的属性动画;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息