本帖最后由 Android_Robot 于 2016-5-10 14:38 编辑
英文原文:https://blog.stylingandroid.com/snowfall/ 这篇文章的发布日期是2015年的圣诞节,貌似唯一能与之匹配的就是就是在Styling Android上来点喜庆的东西~ 虽然我觉得这张照片足以结束这篇文章了,不过我就再慷慨的来点下雪的效果吧。 我们可以在包含了这张图片的布局里添加一个自定义View来实现:
- <font color="#555555"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.stylingandroid.snowfall.MainActivity">
-
- <ImageView
- android:id="@+id/image"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_centerInParent="true"
- android:contentDescription="@null"
- android:scaleType="fitCenter"
- android:src="@drawable/tree" />
-
- <com.stylingandroid.snowfall.SnowView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignBottom="@id/image"
- android:layout_alignEnd="@id/image"
- android:layout_alignLeft="@id/image"
- android:layout_alignRight="@id/image"
- android:layout_alignStart="@id/image"
- android:layout_alignTop="@id/image" />
- </RelativeLayout>
- </font>
复制代码
本来想在一个自定义的ImageView里面做这件事情,但是还是选择了把它们分开,这样我就不需要每次刷新动画的时候都重新渲染一遍图像。那么就让我们看看我们的自定义View:
- <font color="#555555">public class SnowView extends View {
- private static final int NUM_SNOWFLAKES = 150;
- private static final int DELAY = 5;
-
- private SnowFlake[] snowflakes;
-
- public SnowView(Context context) {
- super(context);
- }
-
- public SnowView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- public SnowView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
-
- protected void resize(int width, int height) {
- Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setColor(Color.WHITE);
- paint.setStyle(Paint.Style.FILL);
- snowflakes = new SnowFlake[NUM_SNOWFLAKES];
- for (int i = 0; i < NUM_SNOWFLAKES; i++) {
- snowflakes[i] = SnowFlake.create(width, height, paint);
- }
- }
-
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- super.onSizeChanged(w, h, oldw, oldh);
- if (w != oldw || h != oldh) {
- resize(w, h);
- }
- }
-
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- for (SnowFlake snowFlake : snowflakes) {
- snowFlake.draw(canvas);
- }
- getHandler().postDelayed(runnable, DELAY);
- }
-
- private Runnable runnable = new Runnable() {
- @Override
- public void run() {
- invalidate();
- }
- };
- }
- </font>
复制代码
这非常简单。当View被resized的时候,我们初始化150个随即放置的SnowFlake对象。onDraw() 方法绘制所有的SnowFlake对象,然后周期性的执行invalidate()。为了不完全占用UI线程,我们在调用这个的时候稍微延迟了一点点时间。 SnowFlake的代码大致是基于 Samuel Arbesman的snowfall算法:
- <font color="#555555">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;
- 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 Random random;
- private final Point position;
- private float angle;
- private final float increment;
- private final float flakeSize;
- private final Paint paint;
-
- public static SnowFlake create(int width, int height, Paint paint) {
- Random random = new Random();
- 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);
- float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);
- return new SnowFlake(random, position, angle, increment, flakeSize, paint);
- }
-
- SnowFlake(Random random, Point position, float angle, float increment, float flakeSize, Paint paint) {
- this.random = random;
- this.position = position;
- this.angle = angle;
- this.increment = increment;
- this.flakeSize = flakeSize;
- this.paint = paint;
- }
-
- private void move(int width, int height) {
- double x = position.x + (increment * Math.cos(angle));
- double y = position.y + (increment * Math.sin(angle));
-
- angle += random.getRandom(-ANGLE_SEED, ANGLE_SEED) / ANGLE_DIVISOR;
-
- position.set((int) x, (int) y);
-
- if (!isInside(width, height)) {
- reset(width);
- }
- }
-
- private boolean isInside(int width, int height) {
- int x = position.x;
- int y = position.y;
- return x >= -flakeSize - 1 && x + flakeSize <= width && y >= -flakeSize - 1 && y - flakeSize < height;
- }
-
- private void reset(int width) {
- position.x = random.getRandom(width);
- position.y = (int) (-flakeSize - 1);
- angle = random.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;
- }
-
- public void draw(Canvas canvas) {
- int width = canvas.getWidth();
- int height = canvas.getHeight();
- move(width, height);
- canvas.drawCircle(position.x, position.y, flakeSize, paint);
- }
- }
- </font>
复制代码
当每朵雪花初始化之后它被放在Canvas上的一个随机位置。这是为了确保在首次绘制的时候,雪花看起来像是正在进行中,而如果一开始所有的雪花都是从顶部落下的话,就会觉得雪是刚开始下的。当一片雪花离开了画布的时候,它会被重新放置在顶部横轴的一个随机位置 - 这样我们就能回收雪花,避免不必要的对象创建。 绘制每一帧的时候,我们首先为雪花的移动添加一些随机因素来模拟风吹的效果,让每片雪花稍微改变下自己的方向。然后在实际绘制雪花之前,我们执行边界检查(如果必要,把它移到上方)。
所有的常量都经过调整,知道雪花模拟达到我满意的效果为止。 运行效果如下: youtube视频地址:https://www.youtube.com/watch?v=pk66ZziTfOw 当然,在Canvas绘制并不是渲染这类东西最高效的方法(比如使用OpenGL渲染),但是我还有礼物要打开,还有火鸡要吃所以这个只能改天再说了。 【github源码下载地址】回帖可见:
其他资源: [官方秀] 为什么你要报传智·黑马学Android
【github项目分享】Android实现雪花飞舞幻特效
|