冬天来了, 大雪纷飞, 好冷啊. 在应用里, 也可以实现漫天飞雪的动画, 让我来介绍一下吧.
我们的应用也可以有一些冬天的效果, 教大家做一个下雪的动画效果, 参考.
主要
(1) 隐藏status bar
, 全屏显示图片.
(2) 绘制多个点, 重绘页面, 实现移动效果.
(3) 回收点, 避免重复创建.
我喜欢用注释说话, 请大家多关注代码中的注释.
本文源码的GitHub下载地址
欢迎Follow我的GitHub: https://github.com/SpikeKing
雪花类
雪花的属性包含: 随机量, 位置, 增量, 大小, 角度, 画笔.
绘画的过程中, 使用角度会移动点的位置, 每次速率都不同.
当雪花移出屏幕时, 会重新使用, 在屏幕的顶端重新落下.
算法参考.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/** * 雪花的类, 移动, 移出屏幕会重新设置位置. * <p/> * Created by wangchenlong on 16/1/24. */ public class SnowFlake { // 雪花的角度 private static final float ANGE_RANGE = 0.1f; // 角度范围 private static final float HALF_ANGLE_RANGE = ANGE_RANGE / 2f; // 一般的角度 private static final float HALF_PI = (float) Math.PI / 2f; // 半PI private static final float ANGLE_SEED = 25f; // 角度随机种子 private static final float ANGLE_DIVISOR = 10000f; // 角度的分母 // 雪花的移动速度 private static final float INCREMENT_LOWER = 2f; private static final float INCREMENT_UPPER = 4f; // 雪花的大小 private static final float FLAKE_SIZE_LOWER = 7f; private static final float FLAKE_SIZE_UPPER = 20f; private final RandomGenerator mRandom; // 随机控制器 private final Point mPosition; // 雪花位置 private float mAngle; // 角度 private final float mIncrement; // 雪花的速度 private final float mFlakeSize; // 雪花的大小 private final Paint mPaint; // 画笔 private SnowFlake(RandomGenerator random, Point position, float angle, float increment, float flakeSize, Paint paint) { mRandom = random; mPosition = position; mIncrement = increment; mFlakeSize = flakeSize; mPaint = paint; mAngle = angle; } public static SnowFlake create(int width, int height, Paint paint) { RandomGenerator random = new RandomGenerator(); int x = random.getRandom(width); int y = random.getRandom(height); Point position = new Point(x, y); float angle = random.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE; float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER); floatlass="crayon-e">getRandom(INCREMENT_LOWER, INCREMENT_UPPER); float rg/
我们的应用也可以有一些冬天的效果, 教大家做一个下雪的动画效果, 参考. 主要 我喜欢用注释说话, 请大家多关注代码中的注释. 本文源码的GitHub下载地址
雪花类雪花的属性包含: 随机量, 位置, 增量, 大小, 角度, 画笔.
|