Grav:各种各样的粒子动画

来源:互联网 发布:矩阵分解推荐算法 编辑:程序博客网 时间:2024/06/05 10:16

编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识、前端、后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过!

Grav是一个可以实现各种各样粒子动画的库,地址:https://github.com/glomadrian/Grav 。

下面是其文档的简单翻译。

这个库由几个generator组成,每个generator负责一件事情。不同的generator组合可以做出不同的效果。

有以下几个generator:

  • Point generator

  • Grav generator

  • Color generator

  • Animation generator

Point generator

point generator制造出粒子的分布位置,通过如下属性设置point generator:

  1. app:pointGenerator="classname"

有三种point generator可用:

Regular point generator

基于单位和方差生成粒子。

  1. app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
  2. app:regular_cell_size="200"
  3. app:regular_variance="20"

(和 GravBallGeneartor一起使用)

regular_generator.png

Circular point generator

基于一个圆内的单位和方差生成粒子。

  1. app:pointGenerator="com.github.glomadrian.grav.generator.point.CircularPointGenerator"
  2. app:regular_cell_size="200"
  3. app:regular_variance="200"

(和 GravBallGeneartor一起使用)

circular_generator.png

Percent point generator

根据百分比数组产生粒子。

  1. app:pointGenerator="com.github.glomadrian.grav.generator.point.PercentPointGenerator"
  2. app:percent_points_array="@array/walla_points_percent_points"

arrays.xml中每组item代表一个点,其中一个item定义宽度上的百分比,另一个定义高度上的百分比:

  1. <integer-array name="sample_points_percent_points">
  2.    <item>10</item>
  3.    <item>60</item>
  4.  
  5.    <item>8</item>
  6.    <item>30</item>
  7.  
  8.    <item>25</item>
  9.    <item>10</item>
  10.  </integer-array>

(和GravBallGeneartor一起使用)

percent_generator.png

Grav Generator

Grav generator负责绘制每个点。通过设置如下属性来使用它:

  1. app:gravGenerator="classname"

有两种Grav Generator可用

Ball generator

绘制一个球。

可以使用下面的属性让球的大小在一个范围内随机生成:

  1. app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
  2. app:ball_from_size="3dp"
  3. app:ball_to_size="16dp"

ball_size_range.png

Rectangle generator

绘制矩形。

可以使用下面的属性来改变矩形的大小:

  1. app:gravGenerator="com.github.glomadrian.grav.generator.grav.RectangleGenerator"
  2. app:rectangle_width="15dp"
  3. app:rectangle_height="10dp"

rectangle_grav.png

Color generator

定义Grav是如何涂色的。

你需要设置如下属性来使用它:

  1. appcolorGenerator="classname"

有两种 Color Generator可用。

Single color generator

绘制单一的颜色。

  1. app:colorGenerator="com.github.glomadrian.grav.generator.paint.SingleColorGenerator"
  2. app:single_color="@color/colorPrimary"

Array color generator

使用颜色数组涂色

  1. app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
  2. app:array_colors="@array/Spectral"

ball_color.png

Animation generator

 animation generator负责位置,大小以及属性的动画。 animation generator 可以是一个animation generator或者animation generator数组。

Single animation generator

通过下面的属性设置:

  1. app:animationGenerator="classname"

Array animation generator

通过下面的属性设置:

  1. app:animationGenerators="@array/array_reference"
  2.  
  3. <string-array name="array_reference">
  4.   <item>com.github.glomadrian.grav.generator.animation.PathAnimator</item>
  5.   <item>com.github.glomadrian.grav.generator.animation.BallSizeAnimator</item>
  6. </string-array>

Shake animator

在一个区间内移动

  1. app:animationGenerator=" com.github.glomadrian.grav.generator.animation.ShakeAnimator"
  2. //Min animation duration
  3. app:shake_min_duration="1000"
  4. //Max animation duration
  5. app:shake_max_duration="3000"
  6. //Direction horizontal or vertical
  7. app:shake_direction="horizontal"
  8. //The size of the movement
  9. app:shake_variance="15dp"

shake_anim.gif

Side to side animator

单位的移动从一边到另一边

  1. app:animationGenerator="com.github.glomadrian.grav.generator.animation.SideToSideAnimator"
  2. //Min animation duration
  3. app:side_to_side_min_duration="1000"
  4. //Max animation duration
  5. app:side_to_side_max_duration="3000"
  6. //Direction leftToRight | rightToLeft | upToDown | downToUp
  7. app:side_to_side_direction="leftToRight"

你也可以设置一个插值器

  1. side_to_side_interpolator="interpolator class"

side_to_side.gif

Alpha animator

对单位使用渐变动画

  1. app:animationGenerator="com.github.glomadrian.grav.generator.animation.AlphaAnimator"
  2. //Min animation duration
  3. app:alpha_min_duration="1000"
  4. //Max animation duration
  5. app:alpha_max_duration="3000"
  6. //From and to in a range (0-255)
  7. app:alpha_from="0"
  8. app:alpha_to="255"

Ball size animator

对球的大小进行动画。

  1. app:animationGenerator="com.github.glomadrian.grav.generator.animation.BallSizeAnimator"
  2. //Min animation duration
  3. app:ball_size_min_duration="1000"
  4. //Max animation duration
  5. app:ball_size_max_duration="3000"
  6. app:ball_size_from_size="3dp"
  7. app:ball_size_to_size="8dp"

size_to_size.gif

Path animator

按照一个路径移动单位。

The path (sample)

  1. <string name="circle">
  2. M527.023,71.8233 C780.213,71.8233,985.464,277.075,985.464,530.265
  3. C985.464,783.455,780.213,988.707,527.023,988.707
  4. C273.832,988.707,68.5809,783.455,68.5809,530.265
  5. C68.5809,277.075,273.832,71.8233,527.023,71.8233 Z
  6. </string>
  7. <integer name="circle_original_width">1062</integer>
  8. <integer name="circle_original_height">1062</integer>

使用 path animator

  1. app:animationGenerator="com.github.glomadrian.grav.generator.animation.PathAnimator"
  2. //Variance is the random margin given to the grav
  3. app:path_variance_from="1dp"
  4. app:path_variance_to="20dp"
  5. //Min animation duration
  6. app:path_min_duration="2000"
  7. //Max animation duration
  8. app:path_max_duration="2300"
  9. //String that define the path
  10. app:path="@string/circle"
  11. app:path_original_width="@integer/circle_original_width"
  12. app:path_original_height="@integer/circle_original_height"

path_animator.gif

XML 示例

完整的例子可以在源码的demo app中找到。

  1. <com.github.glomadrian.grav.GravView
  2.      android:id="@+id/grav"
  3.      android:layout_centerInParent="true"
  4.      android:layout_width="400dp"
  5.      android:layout_height="400dp"
  6.      app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
  7.      app:array_colors="@array/red"
  8.      app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
  9.      app:regular_cell_size="150"
  10.      app:regular_variance="100"
  11.      app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
  12.      app:ball_size_from_size="3dp"
  13.      app:ball_size_to_size="6dp"
  14.      app:animationGenerators="@array/path"
  15.      app:path_variance_from="-10dp"
  16.      app:path_variance_to="12dp"
  17.      app:path="@string/circle"
  18.      app:path_original_width="@integer/circle_original_width"
  19.      app:path_original_height="@integer/circle_original_height"
  20.      app:path_min_duration="5000"
  21.      app:path_max_duration="6000"
  22.      />

  1. <com.github.glomadrian.grav.GravView
  2.      android:id="@+id/grav"
  3.      android:layout_width="match_parent"
  4.      android:layout_height="match_parent"
  5.      android:layout_centerInParent="true"
  6.      app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
  7.      app:array_colors="@array/bubble"
  8.      app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
  9.      app:regular_cell_size="300"
  10.      app:regular_variance="200"
  11.      app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
  12.      app:ball_from_size="10dp"
  13.      app:ball_to_size="20dp"
  14.      app:animationGenerators="@array/BubbleAnimations"
  15.      app:side_to_side_min_duration="10000"
  16.      app:side_to_side_max_duration="10000"
  17.      app:side_to_side_direction="leftToRight"
  18.      app:shake_direction="vertical"
  19.      app:shake_min_duration="10000"
  20.      app:shake_max_duration="20000"
  21.      app:shake_variance="500dp"
  22.     />

  1. <com.github.glomadrian.grav.GravView
  2.       android:id="@+id/grav"
  3.       android:layout_width="match_parent"
  4.       android:layout_height="match_parent"
  5.       app:colorGenerator="com.github.glomadrian.grav.generator.paint.ArrayColorGenerator"
  6.       app:array_colors="@array/falcon"
  7.       app:pointGenerator="com.github.glomadrian.grav.generator.point.RegularPointGenerator"
  8.       app:regular_cell_size="100"
  9.       app:regular_variance="200"
  10.       app:gravGenerator="com.github.glomadrian.grav.generator.grav.RectangleGenerator"
  11.       app:rectangle_width="15dp"
  12.       app:rectangle_height="2dp"
  13.       app:animationGenerators="@array/FalconAnimations"
  14.       app:side_to_side_min_duration="400"
  15.       app:side_to_side_max_duration="800"
  16.       app:side_to_side_direction="rightToLeft"
  17.       app:shake_variance="5dp"
  18.       app:shake_direction="vertical"
  19.       />

robot.gif

  1. <com.github.glomadrian.grav.GravView
  2.     android:id="@+id/grav"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     app:colorGenerator="com.github.glomadrian.grav.generator.paint.OneColorGenerator"
  6.     app:single_color="#FFF"
  7.     app:pointGenerator="com.github.glomadrian.grav.generator.point.PercentPointGenerator"
  8.     app:ball_from_size="2dp"
  9.     app:ball_to_size="5dp"
  10.     app:animationGenerators="@array/WallaIcon"
  11.     app:alpha_from="100"
  12.     app:alpha_to="200"
  13.     app:alpha_min_duration="1600"
  14.     app:alpha_max_duration="3000"
  15.     app:shake_direction="vertical"
  16.     app:shake_variance="3dp"
  17.     app:shake_min_duration="1100"
  18.     app:shake_max_duration="3500"
  19.     app:percent_points_array="@array/walla_points_percent_points"
  20.     app:gravGenerator="com.github.glomadrian.grav.generator.grav.BallGenerator"
  21.     />