android 实现漫天飞舞雪花以及下雨天的效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16064871/article/details/50603105

前言:

这个效果实现的原作者是国外一位大神。我在其基础上测试,以及在代码上加了不少注释,以及局部修改。后面我有根据漫天飞舞雪花,实现下雨天场景的效果。原作者项目还是android studio版本的。我改成eclipse android 版本。

英文原文地址:https://blog.stylingandroid.com/snowfall/

中文翻译地址:http://www.open-open.com/lib/view/open1452263908573.html

国外大神实现效果youtube视频地址:https://www.youtube.com/watch?v=pk66ZziTfOw 

中文翻译是open开发者经验库一位作者翻译,翻译的很好。在那篇文章也能看到实现的最原始的效果。

实现漫天飞舞的雪花下载地址:http://download.csdn.net/detail/qq_16064871/9420804

实现下雨天效果的下载地址:http://download.csdn.net/detail/qq_16064871/9420808

1、漫天飞舞的雪花主要代码

SnowView

<span style="font-size:14px;">package com.ex</span>ample.snowflake.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * 雪花视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雪花
 */
public class SnowView extends View {

    private static final int NUM_SNOWFLAKES = 150; // 雪花数量
    private static final int DELAY = 5; // 延迟
    private SnowFlake[] mSnowFlakes; // 雪花

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

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != oldw || h != oldh) {
            initSnow(w, h);
        }
    }

    private void initSnow(int width, int height) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿
        paint.setColor(Color.WHITE); // 白色雪花
        paint.setStyle(Paint.Style.FILL); // 填充;
        mSnowFlakes = new SnowFlake[NUM_SNOWFLAKES];
        //mSnowFlakes所有的雪花都生成放到这里面
        for (int i = 0; i < NUM_SNOWFLAKES; ++i) {
            mSnowFlakes[i] = SnowFlake.create(width, height, paint);
        }
    }

    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //for返回SnowFlake
        for (SnowFlake s : mSnowFlakes) {
        	//然后进行绘制
            s.draw(canvas);
        }
        // 隔一段时间重绘一次, 动画效果
        getHandler().postDelayed(runnable, DELAY);
    }

    // 重绘线程
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
        	//自动刷新
            invalidate();
        }
    };
}
SnowFlake

package com.example.snowflake.view;

import com.example.snowflake.RandomGenerator;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;

/**
 * 雪花的类, 移动, 移出屏幕会重新设置位置.
 */
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);
        float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);
        return new SnowFlake(random, position, angle, increment, flakeSize, paint);
    }

    // 绘制雪花
    public void draw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        move(width, height);
        canvas.drawCircle(mPosition.x, mPosition.y, mFlakeSize, mPaint);
    }

    // 移动雪花
    private void move(int width, int height) {
    	//x水平方向,那么需要晃动,主要设置这个值就可以,现在取消晃动了
    	//如果 mPosition.x不加上后面那个值,就不会晃动了
    	double x = mPosition.x + (mIncrement * Math.cos(mAngle));
        //y是竖直方向,就是下落
        double y = mPosition.y + (mIncrement * Math.sin(mAngle));
 
        mAngle += mRandom.getRandom(-ANGLE_SEED, ANGLE_SEED) / ANGLE_DIVISOR;
        //这个是设置雪花位置,如果在很短时间内刷新一次,就是连起来的动画效果
        mPosition.set((int) x, (int) y);

        // 移除屏幕, 重新开始
        if (!isInside(width, height)) {
            // 重置雪花
            reset(width);
        }
    }
    
    // 判断是否在其中
    private boolean isInside(int width, int height) {
        int x = mPosition.x;
        int y = mPosition.y;
        return x > mFlakeSize -5 && x + mFlakeSize <= width && y >= -mFlakeSize - 1 && y - mFlakeSize < height;
    }

    // 重置雪花
    private void reset(int width) {
        mPosition.x = mRandom.getRandom(width);
        mPosition.y = (int) (-mFlakeSize - 1); // 最上面
        mAngle = mRandom.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;
    }
}

2、实现下雨天效果代码

RainView

package com.example.raindrop.view;

import com.example.raindrop.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * 雨滴视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雨滴
 */
public class RainView extends View {

    private static final int NUM_SNOWFLAKES = 150; // 雨滴数量
    private static final int DELAY = 5; // 延迟
    private RainFlake[] mSnowFlakes; // 雨滴

    public RainView(Context context) {
        super(context);
    }

    public RainView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w != oldw || h != oldh) {
            initSnow(w, h);
        }
    }

    private void initSnow(int width, int height) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿
        paint.setColor(getResources().getColor(R.color.colorWater)); // 雨滴的颜色
        paint.setStyle(Paint.Style.FILL); // 填充;
        mSnowFlakes = new RainFlake[NUM_SNOWFLAKES];
        //mSnowFlakes所有的雨滴都生成放到这里面
        for (int i = 0; i < NUM_SNOWFLAKES; ++i) {
            mSnowFlakes[i] = RainFlake.create(width, height, paint);
        }
    }

    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //for返回SnowFlake
        for (RainFlake s : mSnowFlakes) {
        	//然后进行绘制
            s.draw(canvas);
        }
        // 隔一段时间重绘一次, 动画效果
        getHandler().postDelayed(runnable, DELAY);
    }

    // 重绘线程
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
        	//自动刷新
            invalidate();
        }
    };
}

RainFlake

package com.example.raindrop.view;

import com.example.raindrop.RandomGenerator;

import android.graphics.Canvas;
import android.graphics.Paint;

/**
 * 雨滴的类, 移动, 移出屏幕会重新设置位置.
 */
public class RainFlake {

    // 雨滴的移动速度
    private static final float INCREMENT_LOWER = 6f;
    private static final float INCREMENT_UPPER = 8f;

    // 雨滴的大小
    private static final float FLAKE_SIZE_LOWER = 2f;
    private static final float FLAKE_SIZE_UPPER = 5f;

    private final float mIncrement; // 雨滴的速度
    private final float mFlakeSize; // 雨滴的大小
    private final Paint mPaint; // 画笔
    
    private Line mLine; // 雨滴
    
    private RandomGenerator mRandom;

    private RainFlake(RandomGenerator random,Line line, float increment, float flakeSize, Paint paint) {
    	mRandom = random;
    	mLine = line;
        mIncrement = increment;
        mFlakeSize = flakeSize;
        mPaint = paint;
    }

    //生成雨滴
    public static RainFlake create(int width, int height, Paint paint) {
    	RandomGenerator random = new RandomGenerator();
		int [] nline;
		nline = random.getLine(width, height);
        
        Line line = new Line(nline[0], nline[1], nline[2], nline[3]);
        float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER);
        float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);
        return new RainFlake(random,line, increment, flakeSize, paint);
    }

    // 绘制雨滴
    public void draw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        drawLine(canvas, width, height);
    }

    /**
     * 改成线条,类似于雨滴效果
     * @param canvas
     * @param width
     * @param height
     */
	private void drawLine(Canvas canvas, int width, int height) {
		//设置线宽
	  mPaint.setStrokeWidth(mFlakeSize);
		//y是竖直方向,就是下落
      double y1 = mLine.y1 + (mIncrement * Math.sin(1.5));
      double y2 = mLine.y2 + (mIncrement * Math.sin(1.5));

      //这个是设置雨滴位置,如果在很短时间内刷新一次,就是连起来的动画效果
       mLine.set(mLine.x1,(int) y1,mLine.x2 ,(int) y2);
		
		if (!isInsideLine(height)) {
			resetLine(width,height);
		}
		
		canvas.drawLine(mLine.x1, mLine.y1, mLine.x2, mLine.y2, mPaint);
	}
	
    // 判断是否在其中
    private boolean isInsideLine(int height) {
        return mLine.y1 < height && mLine.y2 < height;
    }

    // 重置雨滴
    private void resetLine(int width, int height) {
		int [] nline;
		nline = mRandom.getLine(width, height);
		mLine.x1 = nline[0];
		mLine.y1 = nline[1];
		mLine.x2 = nline[2];
		mLine.y2 = nline[3];
    }

}

3、效果图




在这里我说一下为什么是没有gif效果图,android手机录制屏幕太太麻烦了,还要转为gif。以前我是用腾讯应用宝截图做成gif效果图。但是这次效果不好我就直接截图了。

需要看动画效果,下载我demo。或者去中文翻译那片文章也有效果图。

还有各位有什么推荐工具,关于android 机器录制gif工具或者软件。

再次说明以下:

英文原文地址:https://blog.stylingandroid.com/snowfall/

中文翻译地址:http://www.open-open.com/lib/view/open1452263908573.html

国外大神实现效果youtube视频地址:https://www.youtube.com/watch?v=pk66ZziTfOw 

中文翻译是open开发者经验库一位作者翻译,翻译的很好。在那篇文章也能看到实现的最原始的效果。

实现漫天飞舞的雪花下载地址:http://download.csdn.net/detail/qq_16064871/9420804

实现下雨天效果的下载地址:http://download.csdn.net/detail/qq_16064871/9420808


没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试