Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现

周末升级了墨迹天气,看着引导界面做的不错,模仿一下,可能与原作者的代码实现不一样,但是实现的效果还是差不多的。先分享一篇以前的文章,android动画的基础知识, Android UI开发第十二篇——动画效果Animation(一) 》, 写的不好,读者也可以自行搜索下android动画相关知识。模仿墨迹天气的引导界面动画使用的android动画的基础知识。


     实现墨迹天气向上滑动的viewpager使用的开源库ViewPager-Android。ViewPager-Android开源库设置app:orientation定义滑动方向。


    墨迹天气引导界面共有4个视图,先看一下:(这里引入的图片都是实现后的,截图都是静态图,运行程序看动画效果)。

                

图一                                                                                          图二

    

                

图三                                                                                        图四


图一动画效果:    

    图一中有四个动画效果,最上面的“极低耗电”标示,最下面的箭头标示,还有中间旋转的电池图标和电子表的闪动,最上面的使用的渐变尺寸(scale)动画效果:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <scale  
  4.         android:duration="2000"  
  5.         android:fillAfter="false"  
  6.         android:fromXScale="0.0"  
  7.         android:fromYScale="0.0"  
  8.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  9.         android:pivotX="50%"  
  10.         android:pivotY="50%"  
  11.         android:toXScale="1.1"  
  12.         android:toYScale="1.1" />  
  13. </set>  
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <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.1"
        android:toYScale="1.1" />
</set>

下面简单说明了scale的各个属性:

  1. <!-- 尺寸伸缩动画效果 scale  
  2.        属性:interpolator 指定一个动画的插入器  
  3.        常见动画插入器:  
  4.             accelerate_decelerate_interpolator  加速-减速 动画插入器  
  5.             accelerate_interpolator        加速-动画插入器  
  6.             decelerate_interpolator        减速- 动画插入器  
  7.             anticipate_interpolator 先回退一小步然后加速前进  
  8.             anticipate_overshoot_interpolator   在上一个基础上超出终点一小步再回到终点  
  9.             bounce_interpolator 最后阶段弹球效果  
  10.             cycle_interpolator  周期运动  
  11.             linear_interpolator 匀速  
  12.             overshoot_interpolator  快速到达终点并超出一小步最后回到终点  
  13.       浮点型值:  
  14.             
  15.             fromXScale 属性为动画起始时 X坐标上的伸缩尺寸      
  16.             toXScale   属性为动画结束时 X坐标上的伸缩尺寸       
  17.            
  18.             fromYScale 属性为动画起始时Y坐标上的伸缩尺寸      
  19.             toYScale   属性为动画结束时Y坐标上的伸缩尺寸      
  20.            
  21.             说明:  
  22.                  以上四种属性值      
  23.        
  24.                     0.0表示收缩到没有   
  25.                     1.0表示正常无伸缩       
  26.                     值小于1.0表示收缩    
  27.                     值大于1.0表示放大  
  28.            
  29.             pivotX     属性为动画相对于物件的X坐标的开始位置  
  30.             pivotY     属性为动画相对于物件的Y坐标的开始位置  
  31.            
  32.             说明:  
  33.                     以上两个属性值 从0%-100%中取值  
  34.                     50%为物件的X或Y方向坐标上的中点位置  
  35.            
  36.         长整型值:  
  37.             duration  属性为动画持续时间  
  38.             说明:   时间以毫秒为单位  
  39.    
  40.         布尔型值:  
  41.             fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用  
  42. -->  
<!-- 尺寸伸缩动画效果 scale
       属性:interpolator 指定一个动画的插入器
       常见动画插入器:
            accelerate_decelerate_interpolator  加速-减速 动画插入器
            accelerate_interpolator        加速-动画插入器
            decelerate_interpolator        减速- 动画插入器
            anticipate_interpolator	先回退一小步然后加速前进
            anticipate_overshoot_interpolator	在上一个基础上超出终点一小步再回到终点
            bounce_interpolator	最后阶段弹球效果
            cycle_interpolator	周期运动
            linear_interpolator	匀速
            overshoot_interpolator	快速到达终点并超出一小步最后回到终点
      浮点型值:
          
            fromXScale 属性为动画起始时 X坐标上的伸缩尺寸    
            toXScale   属性为动画结束时 X坐标上的伸缩尺寸     
         
            fromYScale 属性为动画起始时Y坐标上的伸缩尺寸    
            toYScale   属性为动画结束时Y坐标上的伸缩尺寸    
         
            说明:
                 以上四种属性值    
     
                    0.0表示收缩到没有 
                    1.0表示正常无伸缩     
                    值小于1.0表示收缩  
                    值大于1.0表示放大
         
            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置
         
            说明:
                    以上两个属性值 从0%-100%中取值
                    50%为物件的X或Y方向坐标上的中点位置
         
        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位
 
        布尔型值:
            fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
-->

最下面的箭头标示使用了混合动画:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:repeatMode="reverse" >  
  4.   
  5.     <translate  
  6.         android:duration="1000"  
  7.         android:fromXDelta="0"  
  8.         android:fromYDelta="-15"  
  9.         android:repeatCount="infinite"  
  10.         android:toXDelta="0"  
  11.         android:toYDelta="20" />  
  12.   
  13.     <alpha  
  14.         android:duration="1000"  
  15.         android:fromAlpha="1.0"  
  16.         android:repeatCount="infinite"  
  17.         android:toAlpha="0.3" />  
  18.   
  19. </set>  
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:repeatMode="reverse" >

    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:fromYDelta="-15"
        android:repeatCount="infinite"
        android:toXDelta="0"
        android:toYDelta="20" />

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:repeatCount="infinite"
        android:toAlpha="0.3" />

</set>

混合动画是set集合,包含了平移动画(translate)和渐变动画(alpha),对这两动画简单说明:

  1. <alpha  
  2.       android:duration="1000"  
  3.       android:fromAlpha="1.0"  
  4.       android:repeatCount="infinite"  
  5.       android:toAlpha="0.3" />  
  6.   <!--  
  7.        透明度控制动画效果 alpha  
  8.       浮点型值:  
  9.           fromAlpha 属性为动画起始时透明度  
  10.           toAlpha   属性为动画结束时透明度  
  11.           说明:   
  12.               0.0表示完全透明  
  13.               1.0表示完全不透明  
  14.           以上值取0.0-1.0之间的float数据类型的数字  
  15.          
  16.       长整型值:  
  17.           duration  属性为动画持续时间  
  18.           说明:       
  19.               时间以毫秒为单位  
  20.       整型值:          
  21.           repeatCount:重复次数  
  22.           说明:  
  23.           infinite:循环执行,  
  24.           具体正整数表示循环次数  
  25.             
  26.            repeatMode:重复模式,  
  27.            说明:  
  28.               restart:重新从头开始执行  
  29.               reverse:反方向执行  
  30.              
  31.   -->  
  <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:repeatCount="infinite"
        android:toAlpha="0.3" />
    <!--
         透明度控制动画效果 alpha
        浮点型值:
            fromAlpha 属性为动画起始时透明度
            toAlpha   属性为动画结束时透明度
            说明: 
                0.0表示完全透明
                1.0表示完全不透明
            以上值取0.0-1.0之间的float数据类型的数字
         
        长整型值:
            duration  属性为动画持续时间
            说明:     
                时间以毫秒为单位
        整型值:        
            repeatCount:重复次数
            说明:
            infinite:循环执行,
            具体正整数表示循环次数
            
             repeatMode:重复模式,
             说明:
                restart:重新从头开始执行
                reverse:反方向执行
             
    -->

  1. <translate  
  2.      android:duration="1000"  
  3.      android:fromXDelta="0"  
  4.      android:fromYDelta="-15"  
  5.      android:repeatCount="infinite"  
  6.      android:toXDelta="0"  
  7.      android:toYDelta="20" />  
  8.  <!-- translate 平移动画效果  
  9.      整型值:  
  10.          fromXDelta 属性为动画起始时 X坐标上的位置      
  11.          toXDelta   属性为动画结束时 X坐标上的位置  
  12.          fromYDelta 属性为动画起始时 Y坐标上的位置  
  13.          toYDelta   属性为动画结束时 Y坐标上的位置  
  14.          注意:  
  15.                   没有指定fromXType toXType fromYType toYType 时候,  
  16.                   默认是以自己为相对参照物  ,默认参考物最重要           
  17.      长整型值:  
  18.          duration  属性为动画持续时间  
  19.          说明:   时间以毫秒为单位  
  20. gt;  
   <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:fromYDelta="-15"
        android:repeatCount="infinite"
        android:toXDelta="0"
        android:toYDelta="20" />
    <!-- translate 平移动画效果
        整型值:
            fromXDelta 属性为动画起始时 X坐标上的位置    
            toXDelta   属性为动画结束时 X坐标上的位置
            fromYDelta 属性为动画起始时 Y坐标上的位置
            toYDelta   属性为动画结束时 Y坐标上的位置
            注意:
                     没有指定fromXType toXType fromYType toYType 时候,
                     默认是以自己为相对参照物  ,默认参考物最重要         
        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位
-->


中间的电池动画使用了旋转(rotate)动画和渐变尺寸动画的组合:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="800"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="50%"  
  11.         android:pivotY="50%"  
  12.         android:toXScale="1.2"  
  13.         android:toYScale="1.2" />  
  14.   
  15.     <rotate  
  16.         android:duration="3000"  
  17.         android:fromDegrees="0"  
  18.         android:pivotX="50%"  
  19.         android:pivotY="50%"  
  20.         android:repeatCount="-1"  
  21.         android:toDegrees="359.0" />  
  22.   
  23. </set>  
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="800"
        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.2"
        android:toYScale="1.2" />

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:toDegrees="359.0" />

</set>

前面介绍了渐变尺寸动画,下面只介绍旋转动画:

  1.   <rotate  
  2.         android:duration="3000"  
  3.         android:fromDegrees="0"  
  4.         android:pivotX="50%"  
  5.         android:pivotY="50%"  
  6.         android:repeatCount="-1"  
  7.         android:toDegrees="359.0" />  
  8.     <!-- rotate 旋转动画效果  
  9.        属性:interpolator 指定一个动画的插入器  
  10.              
  11.        浮点数型值:  
  12.             fromDegrees 属性为动画起始时物件的角度      
  13.             toDegrees   属性为动画结束时物件旋转的角度 可以大于360度     
  14.    
  15.            
  16.             说明:  
  17.                      当角度为负数——表示逆时针旋转  
  18.                      当角度为正数——表示顺时针旋转                
  19.                      (负数from——to正数:顺时针旋转)     
  20.                      (负数from——to负数:逆时针旋转)   
  21.                      (正数from——to正数:顺时针旋转)   
  22.                      (正数from——to负数:逆时针旋转)         
  23.    
  24.             pivotX     属性为动画相对于物件的X坐标的开始位置  
  25.             pivotY     属性为动画相对于物件的Y坐标的开始位置  
  26.                    
  27.             说明:        以上两个属性值 从0%-100%中取值  
  28.                          50%为物件的X或Y方向坐标上的中点位置  
  29.    
  30.         长整型值:  
  31.             duration  属性为动画持续时间  
  32.             说明:       时间以毫秒为单位  
  33. -->  
  <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:toDegrees="359.0" />
    <!-- rotate 旋转动画效果
       属性:interpolator 指定一个动画的插入器
           
       浮点数型值:
            fromDegrees 属性为动画起始时物件的角度    
            toDegrees   属性为动画结束时物件旋转的角度 可以大于360度   
 
         
            说明:
                     当角度为负数——表示逆时针旋转
                     当角度为正数——表示顺时针旋转              
                     (负数from——to正数:顺时针旋转)   
                     (负数from——to负数:逆时针旋转) 
                     (正数from——to正数:顺时针旋转) 
                     (正数from——to负数:逆时针旋转)       
 
            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置
                 
            说明:        以上两个属性值 从0%-100%中取值
                         50%为物件的X或Y方向坐标上的中点位置
 
        长整型值:
            duration  属性为动画持续时间
            说明:       时间以毫秒为单位
-->

电子表闪动动画使用animation-list实现的逐帧动画:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:oneshot="false" >  
  4. <!--     
  5.     根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画    
  6.     根标签下,通过item标签对动画中的每一个图片进行声明    
  7.     android:duration 表示展示所用的该图片的时间长度    
  8.  -->    
  9.     <item  
  10.         android:drawable="@drawable/tutorial1_icon1"  
  11.         android:duration="200"/>  
  12.     <item  
  13.         android:drawable="@drawable/tutorial1_icon2"  
  14.         android:duration="200"/>  
  15.   
  16. </animation-list>  
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
<!--   
    根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画  
    根标签下,通过item标签对动画中的每一个图片进行声明  
    android:duration 表示展示所用的该图片的时间长度  
 -->  
    <item
        android:drawable="@drawable/tutorial1_icon1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/tutorial1_icon2"
        android:duration="200"/>

</animation-list>


Anima启动:
  1. t1_icon1.setImageResource(R.drawable.t1_frame_animation);  
  2.             t1_icon1_animationDrawable = (AnimationDrawable) t1_icon1  
  3.                     .getDrawable();  
  4.             t1_icon1_animationDrawable.start();  
t1_icon1.setImageResource(R.drawable.t1_frame_animation);
			t1_icon1_animationDrawable = (AnimationDrawable) t1_icon1
					.getDrawable();
			t1_icon1_animationDrawable.start();


图二动画效果: 

    图二中最上面的“极小安装”动画和最下面的箭头动画和图一中一样,不做过多介绍,中间动画使用的尺寸渐变动画,和图一中的尺寸渐变动画一样,也不多介绍。


图三动画效果: 

    图二中最上面的“极速流畅”动画和最下面的箭头动画和图一中一样,不做过多介绍。中间效果使用了云移动效果使用了平移动画,火箭喷气效果使用了animation-list的逐帧动画。逐帧动画就不多说了,这里的平移动画没有使用xml文件实现,使用的java代码,为了适配多种屏幕,需要计算平移的初始位置,代码定义了几个位置:

  1. view3.getViewTreeObserver().addOnGlobalLayoutListener(  
  2.             new OnGlobalLayoutListener() {  
  3.   
  4.                 @Override  
  5.                 public void onGlobalLayout() {  
  6.                     // TODO Auto-generated method stub  
  7.                     int h1 = centerLayout.getTop();  
  8.                     int h2 = centerLayout.getBottom();  
  9.                     DensityUtil densityUtil = new DensityUtil(  
  10.                             MainActivity.this);  
  11.                     int w = densityUtil.getScreenWidth();  
  12.   
  13.                     fx1 = t3_icon2.getTop() + t3_icon2.getHeight();  
  14.                     fy1 = -t3_icon2.getTop() - t3_icon2.getHeight();  
  15.                     tx1 = -t3_icon2.getWidth() - t3_icon2.getLeft();  
  16.                     ty1 = t3_icon2.getTop() + t3_icon2.getLeft()  
  17.                             + t3_icon2.getWidth();  
  18.   
  19.                     fx2 = t3_icon3.getTop() + t3_icon3.getHeight();  
  20.                     fy2 = -t3_icon3.getTop() - t3_icon3.getHeight();  
  21.                     tx2 = -t3_icon3.getWidth() - t3_icon3.getLeft();  
  22.                     ty2 = t3_icon3.getTop() + t3_icon3.getLeft()  
  23.                             + t3_icon3.getWidth();  
  24.   
  25.                     fx3 = w - t3_icon4.getLeft();  
  26.                     fy3 = -(w - t3_icon4.getLeft());  
  27.                     tx3 = -(h2 - h1 - t3_icon4.getTop());  
  28.                     ty3 = h2 - h1 - t3_icon4.getTop();  
  29.   
  30.                     fx4 = w - t3_icon5.getLeft();  
  31.                     fy4 = -(w - t3_icon5.getLeft());  
  32.                     tx4 = -(h2 - h1 - t3_icon5.getTop());  
  33.                     ty4 = h2 - h1 - t3_icon5.getTop();  
  34.                 }  
  35.             });  
	view3.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						// TODO Auto-generated method stub
						int h1 = centerLayout.getTop();
						int h2 = centerLayout.getBottom();
						DensityUtil densityUtil = new DensityUtil(
								MainActivity.this);
						int w = densityUtil.getScreenWidth();

						fx1 = t3_icon2.getTop() + t3_icon2.getHeight();
						fy1 = -t3_icon2.getTop() - t3_icon2.getHeight();
						tx1 = -t3_icon2.getWidth() - t3_icon2.getLeft();
						ty1 = t3_icon2.getTop() + t3_icon2.getLeft()
								+ t3_icon2.getWidth();

						fx2 = t3_icon3.getTop() + t3_icon3.getHeight();
						fy2 = -t3_icon3.getTop() - t3_icon3.getHeight();
						tx2 = -t3_icon3.getWidth() - t3_icon3.getLeft();
						ty2 = t3_icon3.getTop() + t3_icon3.getLeft()
								+ t3_icon3.getWidth();

						fx3 = w - t3_icon4.getLeft();
						fy3 = -(w - t3_icon4.getLeft());
						tx3 = -(h2 - h1 - t3_icon4.getTop());
						ty3 = h2 - h1 - t3_icon4.getTop();

						fx4 = w - t3_icon5.getLeft();
						fy4 = -(w - t3_icon5.getLeft());
						tx4 = -(h2 - h1 - t3_icon5.getTop());
						ty4 = h2 - h1 - t3_icon5.getTop();
					}
				});


图四动画效果:

    图四中“墨迹天气3.0”图片使用了RotateAnimation,动画插值器使用的CycleInterpolator,“全面革新 我是极致控”使用的渐变尺寸动画

  1. int pivot = Animation.RELATIVE_TO_SELF;  
  2.             CycleInterpolator interpolator = new CycleInterpolator(3.0f);  
  3.             RotateAnimation animation = new RotateAnimation(010, pivot,  
  4.                     0.47f, pivot, 0.05f);  
  5.             animation.setStartOffset(500);  
  6.             animation.setDuration(3000);  
  7.             animation.setRepeatCount(1);// Animation.INFINITE  
  8.             animation.setInterpolator(interpolator);  
  9.             t4_icon1.startAnimation(animation);  
int pivot = Animation.RELATIVE_TO_SELF;
			CycleInterpolator interpolator = new CycleInterpolator(3.0f);
			RotateAnimation animation = new RotateAnimation(0, 10, pivot,
					0.47f, pivot, 0.05f);
			animation.setStartOffset(500);
			animation.setDuration(3000);
			animation.setRepeatCount(1);// Animation.INFINITE
			animation.setInterpolator(interpolator);
			t4_icon1.startAnimation(animation);



上面基本实现了墨迹天气的动画效果。

资源库下载:http://download.csdn.net/detail/xyz_lmn/7094071

 


参考:

http://developer.android.com/guide/topics/resources/animation-resource.html

http://developer.android.com/reference/android/view/animation/Animation.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值