美文网首页
Snowfall--降雪效果

Snowfall--降雪效果

作者: youmu178 | 来源:发表于2016-01-11 14:37 被阅读129次

翻译于Snowfall
效果图就不贴了,就是一个下雪效果。可以想到。。。
我们这样做,将这个自动一布局在Image上面。这样的话也可以依附其他布局。

<?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>
  • 尽管可以通过继承ImageView来实现自定义View,但我决定将 SnowView 和图片分开,这样每次刷新动画的时候不用重新渲染图片,只刷新 SnowView 就行了

  • 让我们来看这个自定义的View:

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();
        }
    };
}

代码很简单。当布局改变的时候,初始化150个雪花对象,随机放置。在onDraw()方法中绘制所有的雪花,在子线程中每隔一段时间刷新invalidate()重新绘制。

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);
    }
}

首先确定每一朵雪花在画布上的任意位置。这是确保当雪花是第一次绘制时,降雪已经开始在下,而不是全在开始位置。当雪花超出画布上的边界,它被重新定位到一个随机水平位置在顶部,这样我们的雪花就循环起来了,以避免不必要的对象的创建。
我们也可以随意改变位置出现不同的效果,不妨试试吧。
最终效果如下:
youtube
源文件地址

版权声明:
Part of this code is based upon “Snowfall” by Sam Arbesman, licensed under Creative Commons Attribution-Share Alike 3.0 and GNU GPL license.
Work: http://openprocessing.org/visuals/?visualID= 84771
License:
http://creativecommons.org/licenses/by-sa/3.0/
http://creativecommons.org/licenses/GPL/2.0/

© 2015, Mark Allison. All rights reserved. This article originally appeared on Styling Android.

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License

相关文章

  • Snowfall--降雪效果

    翻译于Snowfall效果图就不贴了,就是一个下雪效果。可以想到。。。我们这样做,将这个自动一布局在Image上面...

  • 降雪

    今夜必有狂欢 今夜你以雪为衣 今夜是高高亮堂的一夜 噢 下雪 今夜你头戴冻乳汁 酣然一夜圆舞 在我的王国 饮我的液...

  • 降雪

    近日来,突如其来的暴雪覆盖了整个小镇,顷刻间白雪皑皑银装素裹的世界分外妖娆,世间万物好似披上了一层厚厚的面纱,羞涩...

  • 降雪

    今天降温,最高温度-5℃,雪飘飞了一天。早上出门时雪还不算大,在飘雪中行走,多了几许浪漫。白色的雪花落在地上,映的...

  • 降雪

    “极寒天气”是人们形容今年冬天用的最多的词,好在我在家休养一个月,感受到的极寒的时间,便短了些,可是,这并不影响极...

  • 降雪

    雪并不大,我喜欢银装素裹的感觉。 早上去接爱人的时候,路上比较滑,下坡都要小心翼翼的。笑笑在家带悠悠,很操心,有当...

  • 降雪之城

    文/羊君小二 永盛镇,地处江南偏西,常年浸润在阴冷的湿气里,气候和地点都不是最佳,但是却因为镇中心的一口活泉,而...

  • 《岁末降雪》

    昨夜降雪悄无声, 推窗不见路行人, 旧雪未消添新景, 忙碌不觉又年根。 2018.1.31晨于临江仙斋

  • 有降雪

    2019年12月14日 星期六 阴有小雪 早上飘了一点雪,不大,刚能看到一点踪迹。到了下午,基本无踪可寻了。倒是风...

  • 邂逅降雪

    凛冽的寒风 撕裂着铅似的苍穹 淹没急促的雪声 倔强的柳树 舞动着柔弱的枝丫 发出抗议的嘶鸣 爱水的野鸭 包裹着厚厚...

网友评论

      本文标题:Snowfall--降雪效果

      本文链接:https://www.haomeiwen.com/subject/yeibhttx.html