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

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的属性动画;


微信扫码订阅
UP更新不错过~
关注
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原小明

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值