android仿今日头条下拉刷新中的vector动画

一直有留意到今日头条下拉刷新的效果, 真的很赞,在学习了svg drawable相关资料后, 参考博客http://blog.csdn.net/u012950099/article/details/52040028

完成了今日头条下拉刷新中的动画, 首先看下效果图:


录制效果有点渣,少了上边和下边两条线, 请大家见谅.....ps: gif中还有仿ios菊花loading效果, 仿qq下拉刷新效果,侧滑删除等自定义view, 这都不是重点, 重点是标题!

这篇文章我只写效果的实现过程和代码, 具体什么叫svg 什么事vector, 请移步我之前写的文章, android vector pathData探究,几分钟绘制自己的vectordrawable.这篇文章有详细介绍,废话不多说, 上代码看看如何实现.

首先分析下动画效果, 图形是分为4个部分, 参与动画的为3个部分,分别为矩形框, 3条短线,3条长线.

首先创建vector图形文件:

[html] view plain copy
  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:width="200dp"  
  3.     android:height="200dp"  
  4.     android:viewportHeight="200"  
  5.     android:viewportWidth="200">  
  6.   
  7.     <path  
  8.         android:name="path1"  
  9.         android:fillColor="@color/black_overlay"  
  10.         android:pathData="  
  11.             M20,30  
  12.             L100,30  
  13.             M100,30  
  14.             L100,90  
  15.             M100,90  
  16.             L20,90  
  17.             M20,90  
  18.             L20,30"  
  19.         android:strokeColor="@color/black_overlay"  
  20.         android:strokeLineCap="round"  
  21.         android:strokeWidth="6" />  
  22.     <path  
  23.         android:name="path2"  
  24.         android:pathData="  
  25.             M120,30  
  26.             L180,30  
  27.             M120,60  
  28.             L180,60  
  29.             M120,90  
  30.             L180,90"  
  31.         android:strokeColor="@color/black_overlay"  
  32.         android:strokeLineCap="round"  
  33.         android:strokeWidth="6" />  
  34.     <path  
  35.         android:name="path3"  
  36.         android:pathData="  
  37.             M20,120  
  38.             L180,120  
  39.             M20,150  
  40.             L180,150  
  41.             M20,180  
  42.             L180,180"  
  43.         android:strokeColor="@color/black_overlay"  
  44.         android:strokeLineCap="round"  
  45.         android:strokeWidth="6" />  
  46.   
  47.     <path  
  48.         android:pathData="  
  49.             M0,0  
  50.             L200,0  
  51.             M200,0  
  52.             L200,200  
  53.             M200,200  
  54.             L0,200  
  55.             M0,200  
  56.             L0,0"  
  57.         android:strokeColor="@color/black_overlay"  
  58.         android:strokeLineCap="round"  
  59.         android:strokeWidth="6" />  
  60. </vector>  
具体里边的viewportheight, pathdata等等属性是什么意思还是请移步我之前写的: android vector pathData探究,几分钟绘制自己的vectordrawable.

效果图:

第二步, 既然是svg动画, 那么需要创建相应的list或者容器之类的来保存动画的执行顺序或步骤, 创建animated-vector相应xml文件:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:drawable="@drawable/jrtt">  
  4.   
  5.     <target  
  6.         android:animation="@animator/jrtt_path_one"  
  7.         android:name="path1"/>  
  8.   
  9.     <target  
  10.         android:animation="@animator/jrtt_path_two"  
  11.         android:name="path2"/>  
  12.   
  13.     <target  
  14.         android:animation="@animator/jrtt_path_three"  
  15.         android:name="path3"/>  
  16.   
  17. </animated-vector>  

第三步: 在res--animator--文件夹下分别创建对应的jrtt_path_one,jrtt_path_two,jrtt_path-three,这三个ObjectAnimator对应的svg xml文件,

jrtt_path_one:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:ordering="sequentially">  
  4.   
  5.     <objectAnimator  
  6.         android:duration="400"  
  7.         android:interpolator="@android:interpolator/decelerate_cubic"  
  8.         android:propertyName="pathData"  
  9.         android:valueFrom="  
  10.             M20,30  
  11.             L100,30  
  12.             M100,30  
  13.             L100,90  
  14.             M100,90  
  15.             L20,90  
  16.             M20,90  
  17.             L20,30"  
  18.         android:valueTo="  
  19.             M100,30  
  20.             L180,30  
  21.             M180,30  
  22.             L180,90  
  23.             M180,90  
  24.             L100,90  
  25.             M100,90  
  26.             L100,30"  
  27.         android:valueType="pathType" />  
  28.     <objectAnimator  
  29.         android:duration="400"  
  30.         android:interpolator="@android:interpolator/decelerate_cubic"  
  31.         android:propertyName="pathData"  
  32.         android:valueFrom="  
  33.             M100,30  
  34.             L180,30  
  35.             M180,30  
  36.             L180,90  
  37.             M180,90  
  38.             L100,90  
  39.             M100,90  
  40.             L100,30"  
  41.         android:valueTo="  
  42.             M100,120  
  43.             L180,120  
  44.             M180,120  
  45.             L180,180  
  46.             M180,180  
  47.             L100,180  
  48.             M100,180  
  49.             L100,120"  
  50.         android:valueType="pathType" />  
  51.     <objectAnimator  
  52.         android:duration="400"  
  53.         android:interpolator="@android:interpolator/decelerate_cubic"  
  54.         android:propertyName="pathData"  
  55.         android:valueFrom="  
  56.             M100,120  
  57.             L180,120  
  58.             M180,120  
  59.             L180,180  
  60.             M180,180  
  61.             L100,180  
  62.             M100,180  
  63.             L100,120"  
  64.         android:valueTo="  
  65.             M20,120  
  66.             L100,120  
  67.             M100,120  
  68.             L100,180  
  69.             M100,180  
  70.             L20,180  
  71.             M20,180  
  72.             L20,120"  
  73.         android:valueType="pathType" />  
  74.     <objectAnimator  
  75.         android:duration="400"  
  76.         android:interpolator="@android:interpolator/decelerate_cubic"  
  77.         android:propertyName="pathData"  
  78.         android:valueFrom="  
  79.             M20,120  
  80.             L100,120  
  81.             M100,120  
  82.             L100,180  
  83.             M100,180  
  84.             L20,180  
  85.             M20,180  
  86.             L20,120"  
  87.         android:valueTo="  
  88.             M20,30  
  89.             L100,30  
  90.             M100,30  
  91.             L100,90  
  92.             M100,90  
  93.             L20,90  
  94.             M20,90  
  95.             L20,30"  
  96.         android:valueType="pathType" />  
  97. </set>  

jrtt_path_tow:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:ordering="sequentially">  
  4.   
  5.     <objectAnimator  
  6.         android:duration="400"  
  7.         android:interpolator="@android:interpolator/decelerate_cubic"  
  8.         android:propertyName="pathData"  
  9.         android:valueFrom="  
  10.             M120,30  
  11.             L180,30  
  12.             M120,60  
  13.             L180,60  
  14.             M120,90  
  15.             L180,90"  
  16.         android:valueTo="  
  17.             M20,120  
  18.             L180,120  
  19.             M20,150  
  20.             L180,150  
  21.             M20,180  
  22.             L180,180"  
  23.         android:valueType="pathType"/>  
  24.     <objectAnimator  
  25.         android:duration="400"  
  26.         android:interpolator="@android:interpolator/decelerate_cubic"  
  27.         android:propertyName="pathData"  
  28.         android:valueFrom="  
  29.             M20,120  
  30.             L180,120  
  31.             M20,150  
  32.             L180,150  
  33.             M20,180  
  34.             L180,180"  
  35.         android:valueTo="  
  36.             M20,120  
  37.             L80,120  
  38.             M20,150  
  39.             L80,150  
  40.             M20,180  
  41.             L80,180"  
  42.         android:valueType="pathType"/>  
  43.     <objectAnimator  
  44.         android:duration="400"  
  45.         android:interpolator="@android:interpolator/decelerate_cubic"  
  46.         android:propertyName="pathData"  
  47.         android:valueFrom="  
  48.             M20,120  
  49.             L80,120  
  50.             M20,150  
  51.             L80,150  
  52.             M20,180  
  53.             L80,180"  
  54.         android:valueTo="  
  55.              M20,30  
  56.             L180,30  
  57.             M20,60  
  58.             L180,60  
  59.             M20,90  
  60.             L180,90"  
  61.         android:valueType="pathType"/>  
  62.     <objectAnimator  
  63.         android:duration="400"  
  64.         android:interpolator="@android:interpolator/decelerate_cubic"  
  65.         android:propertyName="pathData"  
  66.         android:valueFrom="  
  67.             M20,30  
  68.             L180,30  
  69.             M20,60  
  70.             L180,60  
  71.             M20,90  
  72.             L180,90"  
  73.         android:valueTo="  
  74.             M120,30  
  75.             L180,30  
  76.             M120,60  
  77.             L180,60  
  78.             M120,90  
  79.             L180,90"  
  80.         android:valueType="pathType"/>  
  81. </set>  

jrtt_path_three:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:ordering="sequentially">  
  4.   
  5.     <objectAnimator  
  6.         android:duration="400"  
  7.         android:interpolator="@android:interpolator/decelerate_cubic"  
  8.         android:propertyName="pathData"  
  9.         android:valueFrom="  
  10.             M20,120  
  11.             L180,120  
  12.             M20,150  
  13.             L180,150  
  14.             M20,180  
  15.             L180,180"  
  16.         android:valueTo="  
  17.             M20,30  
  18.             L80,30  
  19.             M20,60  
  20.             L80,60  
  21.             M20,90  
  22.             L80,90"  
  23.         android:valueType="pathType" />  
  24.     <objectAnimator  
  25.         android:duration="400"  
  26.         android:interpolator="@android:interpolator/decelerate_cubic"  
  27.         android:propertyName="pathData"  
  28.         android:valueFrom="  
  29.             M20,30  
  30.             L80,30  
  31.             M20,60  
  32.             L80,60  
  33.             M20,90  
  34.             L80,90"  
  35.         android:valueTo="  
  36.             M20,30  
  37.             L180,30  
  38.             M20,60  
  39.             L180,60  
  40.             M20,90  
  41.             L180,90"  
  42.         android:valueType="pathType" />  
  43.     <objectAnimator  
  44.         android:duration="400"  
  45.         android:interpolator="@android:interpolator/decelerate_cubic"  
  46.         android:propertyName="pathData"  
  47.         android:valueFrom="  
  48.             M20,30  
  49.             L180,30  
  50.             M20,60  
  51.             L180,60  
  52.             M20,90  
  53.             L180,90"  
  54.         android:valueTo="  
  55.             M120,120  
  56.             L180,120  
  57.             M120,150  
  58.             L180,150  
  59.             M120,180  
  60.             L180,180"  
  61.         android:valueType="pathType" />  
  62.     <objectAnimator  
  63.         android:duration="400"  
  64.         android:interpolator="@android:interpolator/decelerate_cubic"  
  65.         android:propertyName="pathData"  
  66.         android:valueFrom="  
  67.             M120,120  
  68.             L180,120  
  69.             M120,150  
  70.             L180,150  
  71.             M120,180  
  72.             L180,180"  
  73.         android:valueTo="  
  74.            M20,120  
  75.             L180,120  
  76.             M20,150  
  77.             L180,150  
  78.             M20,180  
  79.             L180,180"  
  80.         android:valueType="pathType" />  
  81. </set>  

然后再相应布局文件中:

[html] view plain copy
  1. <ImageView  
  2.         android:id="@+id/iv_test_view_vector_jrtt"  
  3.         android:layout_width="40dp"  
  4.         android:layout_height="40dp"  
  5.         android:layout_margin="20dp"  
  6.         android:layout_gravity="center_horizontal"  
  7.         android:src="@drawable/jrtt_vector"  
  8.         />  
在activity中:

[html] view plain copy
  1. ImageView iv_vector = (ImageView) findViewById(R.id.iv_test_view_vector_jrtt);  
  2.        AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) iv_vector.getDrawable();  
  3.        drawable.start();  
好了, 运行起来就能看到文章开头图中效果了.

具体实现,请参考我的GitHub项目, https://github.com/MrDeclanCoder/KotlinMVPRxJava2Dagger2GreenDaoRetrofitDemo, 欢迎start!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章