国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > 互联网 > Android UI开发第四十三篇――使用Property Animation实现墨迹天气3.0引导界面及动画实现

Android UI开发第四十三篇――使用Property Animation实现墨迹天气3.0引导界面及动画实现

来源:程序员人生   发布时间:2014-09-17 19:48:31 阅读次数:4370次
前面写过墨迹天气3.0引导界面及动画实现,里面完美实现了动画效果,那一篇文章使用的View Animation,这一篇文章使用的Property Animation实现。Property Animation是Android3.0以后新增的动画库。

这篇文章的源码以及效果在github。

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


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

               

图一                                                                                          图二

   

               

图三                                                                                        图四



    墨迹天气的引导界面使用的无非是移动、渐变、缩放、转动或者其中几个的组合。我们介绍其中的部分实现。


1、缩放动画


    首先是图一的“极低耗电”使用了一个缩放效果,使用Property Animation实现如下:

    xml动画文件:

   

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:ordering="together" > 
  4.  
  5.     <objectAnimator 
  6.         android:duration="1000" 
  7.         android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
  8.         android:propertyName="scaleX" 
  9.         android:valueFrom="1.0" 
  10.         android:valueTo="1.1" 
  11.         android:valueType="floatType" > 
  12.     </objectAnimator> 
  13.     <objectAnimator 
  14.         android:duration="1000" 
  15.         android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
  16.         android:propertyName="scaleY" 
  17.         android:valueFrom="1.0" 
  18.         android:valueTo="1.1" 
  19.         android:valueType="floatType" > 
  20.     </objectAnimator> 
  21.  
  22. </set> 
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together" > <objectAnimator android:duration="1000" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:propertyName="scaleX" android:valueFrom="1.0" android:valueTo="1.1" android:valueType="floatType" > </objectAnimator> <objectAnimator android:duration="1000" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:propertyName="scaleY" android:valueFrom="1.0" android:valueTo="1.1" android:valueType="floatType" > </objectAnimator> </set>

java使用:

[java] view plaincopyprint?
  1. animation1=(AnimatorSet)AnimatorInflater.loadAnimator(PropertyAnimActivity.this,   
  2.                 R.animator.tutorail_rotate);  
  3.         LinearInterpolator lin = new LinearInterpolator(); 
  4.         animation1.setInterpolator(lin); 
  5.         t1_icon2.setVisibility(View.VISIBLE); 
  6.          
  7.         animation1.setTarget(t1_icon2);   
  8.         animation1.start();  
animation1=(AnimatorSet)AnimatorInflater.loadAnimator(PropertyAnimActivity.this, R.animator.tutorail_rotate); LinearInterpolator lin = new LinearInterpolator(); animation1.setInterpolator(lin); t1_icon2.setVisibility(View.VISIBLE); animation1.setTarget(t1_icon2); animation1.start();


2、移动渐变组合动画


    图一中下面的箭头使用了移动渐变组合动画,实现如下:

    xml文件:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:ordering="together" > 
  4.  
  5.     <!-- 
  6.     可以包含<objectAnimator>, <valueAnimator>,<set>项 
  7.     属性:android:ordering=["together" | "sequentially"],子集执行顺序 
  8.     sequentially    Play animations in this set sequentially 
  9.     together (default)  Play animations in this set at the same time. 
  10.     --> 
  11.  
  12.     <set 
  13.         xmlns:android="http://schemas.android.com/apk/res/android" 
  14.         android:ordering="together" > 
  15.         <objectAnimator 
  16.             android:duration="1000" 
  17.             android:propertyName="translationX" 
  18.             android:repeatCount="infinite" 
  19.             android:repeatMode="reverse" 
  20.             android:valueFrom="0" 
  21.             android:valueTo="0" 
  22.             android:valueType="floatType" > 
  23.         </objectAnimator> 
  24.         <objectAnimator 
  25.             android:duration="1000" 
  26.             android:propertyName="translationY" 
  27.             android:repeatCount="infinite" 
  28.             android:repeatMode="reverse" 
  29.             android:valueFrom="-15" 
  30.             android:valueTo="20" 
  31.             android:valueType="floatType" > 
  32.         </objectAnimator> 
  33.     </set> 
  34.  
  35.     <objectAnimator 
  36.         android:duration="1000" 
  37.         android:propertyName="alpha" 
  38.         android:repeatCount="infinite" 
  39.         android:valueFrom="1.0" 
  40.         android:valueTo="0.3" 
  41.         android:valueType="floatType" > 
  42.     </objectAnimator> 
  43.     <!-- 
  44.    objectAnimator: 
  45.      
  46.         android:propertyName 
  47.             对view可以设置一下值: 
  48.                translationX and translationY:  
  49.                    These properties control where the View is located  
  50.                    as a delta from its left and top coordinates which  
  51.                    are set by its layout container. 
  52.                rotation, rotationX, and rotationY:  
  53.                    These properties control the rotation  
  54.                    in 2D (rotation property) and 3D around the pivot point. 
  55.                scaleX and scaleY:  
  56.                    These properties control the 2D scaling of a View around  
  57.                    its pivot point. 
  58.                pivotX and pivotY:  
  59.                    These properties control the location of the pivot point,  
  60.                    around which the rotation and scaling transforms occur.  
  61.                    By default, the pivot point is located at the center of  
  62.                    the object. 
  63.                x and y:  
  64.                    These are simple utility properties to describe  
  65.                    the final location of the View in its container,  
  66.                    as a sum of the left and top values and translationX  
  67.                    and translationY values. 
  68.                alpha:  
  69.                    Represents the alpha transparency on the View.  
  70.                    This value is 1 (opaque) by default, with a value of 0  
  71.                    representing full transparency (not visible). 
  72.                     
  73.                还可以设置"backgroundColor"等值 
  74.                     
  75.         android:valueTo 
  76.             float, int, or color. Required. The value where the animated property ends.  
  77.             Colors are represented as six digit hexadecimal numbers (for example, #333333). 
  78.          
  79.         android:valueFrom 
  80.             float, int, or color. The value where the animated property starts. If not specified,  
  81.             the animation starts at the value obtained by the property's get method.  
  82.             Colors are represented as six digit hexadecimal numbers (for example, #333333). 
  83.          
  84.         android:duration 
  85.             int. The time in milliseconds of the animation. 300 milliseconds is the default. 
  86.          
  87.         android:startOffset 
  88.             int. The amount of milliseconds the animation delays after start() is called.   
  89.          
  90.         android:repeatCount:重复次数 
  91.             说明: 
  92.             infinite:循环执行, 
  93.             具体正整数表示循环次数 
  94.              
  95.         android:repeatMode:重复模式, 
  96.             说明: 
  97.                 restart:重新从头开始执行 
  98.                 reverse:反方向执行 
  99.                  
  100.         android:valueType 
  101.            Keyword. Do not specify this attribute if the value is a color.  
  102.            The animation framework automatically handles color values 
  103.             
  104.            intType: Specifies that the animated values are integers 
  105.            floatType (default): Specifies that the animated values are floats 
  106.     --> 
  107.  
  108. </set> 
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together" > <!-- 可以包含<objectAnimator>, <valueAnimator>,<set>项 属性:android:ordering=["together" | "sequentially"],子集执行顺序 sequentially Play animations in this set sequentially together (default) Play animations in this set at the same time. --> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together" > <objectAnimator android:duration="1000" android:propertyName="translationX" android:repeatCount="infinite" android:repeatMode="reverse" android:valueFrom="0" android:valueTo="0" android:valueType="floatType" > </objectAnimator> <objectAnimator android:duration="1000" android:propertyName="translationY" android:repeatCount="infinite" android:repeatMode="reverse" android:valueFrom="-15" android:valueTo="20" android:valueType="floatType" > </objectAnimator> </set> <objectAnimator android:duration="1000" android:propertyName="alpha" android:repeatCount="infinite" android:valueFrom="1.0" android:valueTo="0.3" android:valueType="floatType" > </objectAnimator> <!-- objectAnimator: android:propertyName 对view可以设置一下值: translationX and translationY: These properties control where the View is located as a delta from its left and top coordinates which are set by its layout container. rotation, rotationX, and rotationY: These properties control the rotation in 2D (rotation property) and 3D around the pivot point.
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生