英文原文地址: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

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.ex</span>ample.snowflake.view;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.view.View;  
  9.   
  10. /** 
  11.  * 雪花视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雪花 
  12.  */  
  13. public class SnowView extends View {  
  14.   
  15.     private static final int NUM_SNOWFLAKES = 150// 雪花数量  
  16.     private static final int DELAY = 5// 延迟  
  17.     private SnowFlake[] mSnowFlakes; // 雪花  
  18.   
  19.     public SnowView(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     public SnowView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.     }  
  26.   
  27.     public SnowView(Context context, AttributeSet attrs, int defStyleAttr) {  
  28.         super(context, attrs, defStyleAttr);  
  29.     }  
  30.   
  31.     @Override   
  32.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  33.         super.onSizeChanged(w, h, oldw, oldh);  
  34.         if (w != oldw || h != oldh) {  
  35.             initSnow(w, h);  
  36.         }  
  37.     }  
  38.   
  39.     private void initSnow(int width, int height) {  
  40.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿  
  41.         paint.setColor(Color.WHITE); // 白色雪花  
  42.         paint.setStyle(Paint.Style.FILL); // 填充;  
  43.         mSnowFlakes = new SnowFlake[NUM_SNOWFLAKES];  
  44.         //mSnowFlakes所有的雪花都生成放到这里面  
  45.         for (int i = 0; i < NUM_SNOWFLAKES; ++i) {  
  46.             mSnowFlakes[i] = SnowFlake.create(width, height, paint);  
  47.         }  
  48.     }  
  49.   
  50.     @Override   
  51.     protected void onDraw(Canvas canvas) {  
  52.         super.onDraw(canvas);  
  53.         //for返回SnowFlake  
  54.         for (SnowFlake s : mSnowFlakes) {  
  55.             //然后进行绘制  
  56.             s.draw(canvas);  
  57.         }  
  58.         // 隔一段时间重绘一次, 动画效果  
  59.         getHandler().postDelayed(runnable, DELAY);  
  60.     }  
  61.   
  62.     // 重绘线程  
  63.     private Runnable runnable = new Runnable() {  
  64.         @Override  
  65.         public void run() {  
  66.             //自动刷新  
  67.             invalidate();  
  68.         }  
  69.     };  
  70. }  

SnowFlake

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.snowflake.view;  
  2.   
  3. import com.example.snowflake.RandomGenerator;  
  4.   
  5. import android.graphics.Canvas;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Point;  
  8.   
  9. /** 
  10.  * 雪花的类, 移动, 移出屏幕会重新设置位置. 
  11.  */  
  12. public class SnowFlake {  
  13.     // 雪花的角度  
  14.     private static final float ANGE_RANGE = 0.1f; // 角度范围  
  15.     private static final float HALF_ANGLE_RANGE = ANGE_RANGE / 2f; // 一般的角度  
  16.     private static final float HALF_PI = (float) Math.PI / 2f; // 半PI  
  17.     private static final float ANGLE_SEED = 25f; // 角度随机种子  
  18.     private static final float ANGLE_DIVISOR = 10000f;  
  19.     // 雪花的移动速度  
  20.     private static final float INCREMENT_LOWER = 2f;  
  21.     private static final float INCREMENT_UPPER = 4f;  
  22.   
  23.     // 雪花的大小  
  24.     private static final float FLAKE_SIZE_LOWER = 7f;  
  25.     private static final float FLAKE_SIZE_UPPER = 20f;  
  26.   
  27.     private final RandomGenerator mRandom; // 随机控制器  
  28.     private final Point mPosition; // 雪花位置  
  29.     private float mAngle; // 角度  
  30.     private final float mIncrement; // 雪花的速度  
  31.     private final float mFlakeSize; // 雪花的大小  
  32.     private final Paint mPaint; // 画笔  
  33.   
  34.     private SnowFlake(RandomGenerator random, Point position, float angle, float increment, float flakeSize, Paint paint) {  
  35.         mRandom = random;  
  36.         mPosition = position;  
  37.         mIncrement = increment;  
  38.         mFlakeSize = flakeSize;  
  39.         mPaint = paint;  
  40.         mAngle = angle;  
  41.     }  
  42.   
  43.     public static SnowFlake create(int width, int height, Paint paint) {  
  44.         RandomGenerator random = new RandomGenerator();  
  45.         int x = random.getRandom(width);  
  46.         int y = random.getRandom(height);  
  47.         Point position = new Point(x, y);  
  48.         float angle = random.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;  
  49.         float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER);  
  50.         float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);  
  51.         return new SnowFlake(random, position, angle, increment, flakeSize, paint);  
  52.     }  
  53.   
  54.     // 绘制雪花  
  55.     public void draw(Canvas canvas) {  
  56.         int width = canvas.getWidth();  
  57.         int height = canvas.getHeight();  
  58.         move(width, height);  
  59.         canvas.drawCircle(mPosition.x, mPosition.y, mFlakeSize, mPaint);  
  60.     }  
  61.   
  62.     // 移动雪花  
  63.     private void move(int width, int height) {  
  64.         //x水平方向,那么需要晃动,主要设置这个值就可以,现在取消晃动了  
  65.         //如果 mPosition.x不加上后面那个值,就不会晃动了  
  66.         double x = mPosition.x + (mIncrement * Math.cos(mAngle));  
  67.         //y是竖直方向,就是下落  
  68.         double y = mPosition.y + (mIncrement * Math.sin(mAngle));  
  69.    
  70.         mAngle += mRandom.getRandom(-ANGLE_SEED, ANGLE_SEED) / ANGLE_DIVISOR;  
  71.         //这个是设置雪花位置,如果在很短时间内刷新一次,就是连起来的动画效果  
  72.         mPosition.set((int) x, (int) y);  
  73.   
  74.         // 移除屏幕, 重新开始  
  75.         if (!isInside(width, height)) {  
  76.             // 重置雪花  
  77.             reset(width);  
  78.         }  
  79.     }  
  80.       
  81.     // 判断是否在其中  
  82.     private boolean isInside(int width, int height) {  
  83.         int x = mPosition.x;  
  84.         int y = mPosition.y;  
  85.         return x > mFlakeSize -5 && x + mFlakeSize <= width && y >= -mFlakeSize - 1 && y - mFlakeSize < height;  
  86.     }  
  87.   
  88.     // 重置雪花  
  89.     private void reset(int width) {  
  90.         mPosition.x = mRandom.getRandom(width);  
  91.         mPosition.y = (int) (-mFlakeSize - 1); // 最上面  
  92.         mAngle = mRandom.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;  
  93.     }  
  94. }  

 

 

2、实现下雨天效果代码

 

RainView

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.raindrop.view;  
  2.   
  3. import com.example.raindrop.R;  
  4.   
  5. import android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.util.AttributeSet;  
  9. import android.view.View;  
  10.   
  11. /** 
  12.  * 雨滴视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雨滴 
  13.  */  
  14. public class RainView extends View {  
  15.   
  16.     private static final int NUM_SNOWFLAKES = 150// 雨滴数量  
  17.     private static final int DELAY = 5// 延迟  
  18.     private RainFlake[] mSnowFlakes; // 雨滴  
  19.   
  20.     public RainView(Context context) {  
  21.         super(context);  
  22.     }  
  23.   
  24.     public RainView(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.     }  
  27.   
  28.     public RainView(Context context, AttributeSet attrs, int defStyleAttr) {  
  29.         super(context, attrs, defStyleAttr);  
  30.     }  
  31.   
  32.     @Override   
  33.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  34.         super.onSizeChanged(w, h, oldw, oldh);  
  35.         if (w != oldw || h != oldh) {  
  36.             initSnow(w, h);  
  37.         }  
  38.     }  
  39.   
  40.     private void initSnow(int width, int height) {  
  41.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿  
  42.         paint.setColor(getResources().getColor(R.color.colorWater)); // 雨滴的颜色  
  43.         paint.setStyle(Paint.Style.FILL); // 填充;  
  44.         mSnowFlakes = new RainFlake[NUM_SNOWFLAKES];  
  45.         //mSnowFlakes所有的雨滴都生成放到这里面  
  46.         for (int i = 0; i < NUM_SNOWFLAKES; ++i) {  
  47.             mSnowFlakes[i] = RainFlake.create(width, height, paint);  
  48.         }  
  49.     }  
  50.   
  51.     @Override   
  52.     protected void onDraw(Canvas canvas) {  
  53.         super.onDraw(canvas);  
  54.         //for返回SnowFlake  
  55.         for (RainFlake s : mSnowFlakes) {  
  56.             //然后进行绘制  
  57.             s.draw(canvas);  
  58.         }  
  59.         // 隔一段时间重绘一次, 动画效果  
  60.         getHandler().postDelayed(runnable, DELAY);  
  61.     }  
  62.   
  63.     // 重绘线程  
  64.     private Runnable runnable = new Runnable() {  
  65.         @Override  
  66.         public void run() {  
  67.             //自动刷新  
  68.             invalidate();  
  69.         }  
  70.     };  
  71. }  

 

 

RainFlake

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.raindrop.view;  
  2.   
  3. import com.example.raindrop.RandomGenerator;  
  4.   
  5. import android.graphics.Canvas;  
  6. import android.graphics.Paint;  
  7.   
  8. /** 
  9.  * 雨滴的类, 移动, 移出屏幕会重新设置位置. 
  10.  */  
  11. public class RainFlake {  
  12.   
  13.     // 雨滴的移动速度  
  14.     private static final float INCREMENT_LOWER = 6f;  
  15.     private static final float INCREMENT_UPPER = 8f;  
  16.   
  17.     // 雨滴的大小  
  18.     private static final float FLAKE_SIZE_LOWER = 2f;  
  19.     private static final float FLAKE_SIZE_UPPER = 5f;  
  20.   
  21.     private final float mIncrement; // 雨滴的速度  
  22.     private final float mFlakeSize; // 雨滴的大小  
  23.     private final Paint mPaint; // 画笔  
  24.       
  25.     private Line mLine; // 雨滴  
  26.       
  27.     private RandomGenerator mRandom;  
  28.   
  29.     private RainFlake(RandomGenerator random,Line line, float increment, float flakeSize, Paint paint) {  
  30.         mRandom = random;  
  31.         mLine = line;  
  32.         mIncrement = increment;  
  33.         mFlakeSize = flakeSize;  
  34.         mPaint = paint;  
  35.     }  
  36.   
  37.     //生成雨滴  
  38.     public static RainFlake create(int width, int height, Paint paint) {  
  39.         RandomGenerator random = new RandomGenerator();  
  40.         int [] nline;  
  41.         nline = random.getLine(width, height);  
  42.           
  43.         Line line = new Line(nline[0], nline[1], nline[2], nline[3]);  
  44.         float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER);  
  45.         float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);  
  46.         return new RainFlake(random,line, increment, flakeSize, paint);  
  47.     }  
  48.   
  49.     // 绘制雨滴  
  50.     public void draw(Canvas canvas) {  
  51.         int width = canvas.getWidth();  
  52.         int height = canvas.getHeight();  
  53.         drawLine(canvas, width, height);  
  54.     }  
  55.   
  56.     /** 
  57.      * 改成线条,类似于雨滴效果 
  58.      * @param canvas 
  59.      * @param width 
  60.      * @param height 
  61.      */  
  62.     private void drawLine(Canvas canvas, int width, int height) {  
  63.         //设置线宽  
  64.       mPaint.setStrokeWidth(mFlakeSize);  
  65.         //y是竖直方向,就是下落  
  66.       double y1 = mLine.y1 + (mIncrement * Math.sin(1.5));  
  67.       double y2 = mLine.y2 + (mIncrement * Math.sin(1.5));  
  68.   
  69.       //这个是设置雨滴位置,如果在很短时间内刷新一次,就是连起来的动画效果  
  70.        mLine.set(mLine.x1,(int) y1,mLine.x2 ,(int) y2);  
  71.           
  72.         if (!isInsideLine(height)) {  
  73.             resetLine(width,height);  
  74.         }  
  75.           
  76.         canvas.drawLine(mLine.x1, mLine.y1, mLine.x2, mLine.y2, mPaint);  
  77.     }  
  78.       
  79.     // 判断是否在其中  
  80.     private boolean isInsideLine(int height) {  
  81.         return mLine.y1 < height && mLine.y2 < height;  
  82.     }  
  83.   
  84.     // 重置雨滴  
  85.     private void resetLine(int width, int height) {  
  86.         int [] nline;  
  87.         nline = mRandom.getLine(width, height);  
  88.         mLine.x1 = nline[0];  
  89.         mLine.y1 = nline[1];  
  90.         mLine.x2 = nline[2];  
  91.         mLine.y2 = nline[3];  
  92.     }  
  93.   
  94. }  

 

 

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

英文原文地址: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

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:14px;">package com.ex</span>ample.snowflake.view;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.util.AttributeSet;  
  8. import android.view.View;  
  9.   
  10. /** 
  11.  * 雪花视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雪花 
  12.  */  
  13. public class SnowView extends View {  
  14.   
  15.     private static final int NUM_SNOWFLAKES = 150// 雪花数量  
  16.     private static final int DELAY = 5// 延迟  
  17.     private SnowFlake[] mSnowFlakes; // 雪花  
  18.   
  19.     public SnowView(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     public SnowView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.     }  
  26.   
  27.     public SnowView(Context context, AttributeSet attrs, int defStyleAttr) {  
  28.         super(context, attrs, defStyleAttr);  
  29.     }  
  30.   
  31.     @Override   
  32.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  33.         super.onSizeChanged(w, h, oldw, oldh);  
  34.         if (w != oldw || h != oldh) {  
  35.             initSnow(w, h);  
  36.         }  
  37.     }  
  38.   
  39.     private void initSnow(int width, int height) {  
  40.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿  
  41.         paint.setColor(Color.WHITE); // 白色雪花  
  42.         paint.setStyle(Paint.Style.FILL); // 填充;  
  43.         mSnowFlakes = new SnowFlake[NUM_SNOWFLAKES];  
  44.         //mSnowFlakes所有的雪花都生成放到这里面  
  45.         for (int i = 0; i < NUM_SNOWFLAKES; ++i) {  
  46.             mSnowFlakes[i] = SnowFlake.create(width, height, paint);  
  47.         }  
  48.     }  
  49.   
  50.     @Override   
  51.     protected void onDraw(Canvas canvas) {  
  52.         super.onDraw(canvas);  
  53.         //for返回SnowFlake  
  54.         for (SnowFlake s : mSnowFlakes) {  
  55.             //然后进行绘制  
  56.             s.draw(canvas);  
  57.         }  
  58.         // 隔一段时间重绘一次, 动画效果  
  59.         getHandler().postDelayed(runnable, DELAY);  
  60.     }  
  61.   
  62.     // 重绘线程  
  63.     private Runnable runnable = new Runnable() {  
  64.         @Override  
  65.         public void run() {  
  66.             //自动刷新  
  67.             invalidate();  
  68.         }  
  69.     };  
  70. }  

SnowFlake

 

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.snowflake.view;  
  2.   
  3. import com.example.snowflake.RandomGenerator;  
  4.   
  5. import android.graphics.Canvas;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Point;  
  8.   
  9. /** 
  10.  * 雪花的类, 移动, 移出屏幕会重新设置位置. 
  11.  */  
  12. public class SnowFlake {  
  13.     // 雪花的角度  
  14.     private static final float ANGE_RANGE = 0.1f; // 角度范围  
  15.     private static final float HALF_ANGLE_RANGE = ANGE_RANGE / 2f; // 一般的角度  
  16.     private static final float HALF_PI = (float) Math.PI / 2f; // 半PI  
  17.     private static final float ANGLE_SEED = 25f; // 角度随机种子  
  18.     private static final float ANGLE_DIVISOR = 10000f;  
  19.     // 雪花的移动速度  
  20.     private static final float INCREMENT_LOWER = 2f;  
  21.     private static final float INCREMENT_UPPER = 4f;  
  22.   
  23.     // 雪花的大小  
  24.     private static final float FLAKE_SIZE_LOWER = 7f;  
  25.     private static final float FLAKE_SIZE_UPPER = 20f;  
  26.   
  27.     private final RandomGenerator mRandom; // 随机控制器  
  28.     private final Point mPosition; // 雪花位置  
  29.     private float mAngle; // 角度  
  30.     private final float mIncrement; // 雪花的速度  
  31.     private final float mFlakeSize; // 雪花的大小  
  32.     private final Paint mPaint; // 画笔  
  33.   
  34.     private SnowFlake(RandomGenerator random, Point position, float angle, float increment, float flakeSize, Paint paint) {  
  35.         mRandom = random;  
  36.         mPosition = position;  
  37.         mIncrement = increment;  
  38.         mFlakeSize = flakeSize;  
  39.         mPaint = paint;  
  40.         mAngle = angle;  
  41.     }  
  42.   
  43.     public static SnowFlake create(int width, int height, Paint paint) {  
  44.         RandomGenerator random = new RandomGenerator();  
  45.         int x = random.getRandom(width);  
  46.         int y = random.getRandom(height);  
  47.         Point position = new Point(x, y);  
  48.         float angle = random.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;  
  49.         float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER);  
  50.         float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);  
  51.         return new SnowFlake(random, position, angle, increment, flakeSize, paint);  
  52.     }  
  53.   
  54.     // 绘制雪花  
  55.     public void draw(Canvas canvas) {  
  56.         int width = canvas.getWidth();  
  57.         int height = canvas.getHeight();  
  58.         move(width, height);  
  59.         canvas.drawCircle(mPosition.x, mPosition.y, mFlakeSize, mPaint);  
  60.     }  
  61.   
  62.     // 移动雪花  
  63.     private void move(int width, int height) {  
  64.         //x水平方向,那么需要晃动,主要设置这个值就可以,现在取消晃动了  
  65.         //如果 mPosition.x不加上后面那个值,就不会晃动了  
  66.         double x = mPosition.x + (mIncrement * Math.cos(mAngle));  
  67.         //y是竖直方向,就是下落  
  68.         double y = mPosition.y + (mIncrement * Math.sin(mAngle));  
  69.    
  70.         mAngle += mRandom.getRandom(-ANGLE_SEED, ANGLE_SEED) / ANGLE_DIVISOR;  
  71.         //这个是设置雪花位置,如果在很短时间内刷新一次,就是连起来的动画效果  
  72.         mPosition.set((int) x, (int) y);  
  73.   
  74.         // 移除屏幕, 重新开始  
  75.         if (!isInside(width, height)) {  
  76.             // 重置雪花  
  77.             reset(width);  
  78.         }  
  79.     }  
  80.       
  81.     // 判断是否在其中  
  82.     private boolean isInside(int width, int height) {  
  83.         int x = mPosition.x;  
  84.         int y = mPosition.y;  
  85.         return x > mFlakeSize -5 && x + mFlakeSize <= width && y >= -mFlakeSize - 1 && y - mFlakeSize < height;  
  86.     }  
  87.   
  88.     // 重置雪花  
  89.     private void reset(int width) {  
  90.         mPosition.x = mRandom.getRandom(width);  
  91.         mPosition.y = (int) (-mFlakeSize - 1); // 最上面  
  92.         mAngle = mRandom.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;  
  93.     }  
  94. }  

 

 

2、实现下雨天效果代码

 

RainView

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.raindrop.view;  
  2.   
  3. import com.example.raindrop.R;  
  4.   
  5. import android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.util.AttributeSet;  
  9. import android.view.View;  
  10.   
  11. /** 
  12.  * 雨滴视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雨滴 
  13.  */  
  14. public class RainView extends View {  
  15.   
  16.     private static final int NUM_SNOWFLAKES = 150// 雨滴数量  
  17.     private static final int DELAY = 5// 延迟  
  18.     private RainFlake[] mSnowFlakes; // 雨滴  
  19.   
  20.     public RainView(Context context) {  
  21.         super(context);  
  22.     }  
  23.   
  24.     public RainView(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.     }  
  27.   
  28.     public RainView(Context context, AttributeSet attrs, int defStyleAttr) {  
  29.         super(context, attrs, defStyleAttr);  
  30.     }  
  31.   
  32.     @Override   
  33.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  34.         super.onSizeChanged(w, h, oldw, oldh);  
  35.         if (w != oldw || h != oldh) {  
  36.             initSnow(w, h);  
  37.         }  
  38.     }  
  39.   
  40.     private void initSnow(int width, int height) {  
  41.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿  
  42.         paint.setColor(getResources().getColor(R.color.colorWater)); // 雨滴的颜色  
  43.         paint.setStyle(Paint.Style.FILL); // 填充;  
  44.         mSnowFlakes = new RainFlake[NUM_SNOWFLAKES];  
  45.         //mSnowFlakes所有的雨滴都生成放到这里面  
  46.         for (int i = 0; i < NUM_SNOWFLAKES; ++i) {  
  47.             mSnowFlakes[i] = RainFlake.create(width, height, paint);  
  48.         }  
  49.     }  
  50.   
  51.     @Override   
  52.     protected void onDraw(Canvas canvas) {  
  53.         super.onDraw(canvas);  
  54.         //for返回SnowFlake  
  55.         for (RainFlake s : mSnowFlakes) {  
  56.             //然后进行绘制  
  57.             s.draw(canvas);  
  58.         }  
  59.         // 隔一段时间重绘一次, 动画效果  
  60.         getHandler().postDelayed(runnable, DELAY);  
  61.     }  
  62.   
  63.     // 重绘线程  
  64.     private Runnable runnable = new Runnable() {  
  65.         @Override  
  66.         public void run() {  
  67.             //自动刷新  
  68.             invalidate();  
  69.         }  
  70.     };  
  71. }  

 

 

RainFlake

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.example.raindrop.view;  
  2.   
  3. import com.example.raindrop.RandomGenerator;  
  4.   
  5. import android.graphics.Canvas;  
  6. import android.graphics.Paint;  
  7.   
  8. /** 
  9.  * 雨滴的类, 移动, 移出屏幕会重新设置位置. 
  10.  */  
  11. public class RainFlake {  
  12.   
  13.     // 雨滴的移动速度  
  14.     private static final float INCREMENT_LOWER = 6f;  
  15.     private static final float INCREMENT_UPPER = 8f;  
  16.   
  17.     // 雨滴的大小  
  18.     private static final float FLAKE_SIZE_LOWER = 2f;  
  19.     private static final float FLAKE_SIZE_UPPER = 5f;  
  20.   
  21.     private final float mIncrement; // 雨滴的速度  
  22.     private final float mFlakeSize; // 雨滴的大小  
  23.     private final Paint mPaint; // 画笔  
  24.       
  25.     private Line mLine; // 雨滴  
  26.       
  27.     private RandomGenerator mRandom;  
  28.   
  29.     private RainFlake(RandomGenerator random,Line line, float increment, float flakeSize, Paint paint) {  
  30.         mRandom = random;  
  31.         mLine = line;  
  32.         mIncrement = increment;  
  33.         mFlakeSize = flakeSize;  
  34.         mPaint = paint;  
  35.     }  
  36.   
  37.     //生成雨滴  
  38.     public static RainFlake create(int width, int height, Paint paint) {  
  39.         RandomGenerator random = new RandomGenerator();  
  40.         int [] nline;  
  41.         nline = random.getLine(width, height);  
  42.           
  43.         Line line = new Line(nline[0], nline[1], nline[2], nline[3]);  
  44.         float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER);  
  45.         float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);  
  46.         return new RainFlake(random,line, increment, flakeSize, paint);  
  47.     }  
  48.   
  49.     // 绘制雨滴  
  50.     public void draw(Canvas canvas) {  
  51.         int width = canvas.getWidth();  
  52.         int height = canvas.getHeight();  
  53.         drawLine(canvas, width, height);  
  54.     }  
  55.   
  56.     /** 
  57.      * 改成线条,类似于雨滴效果 
  58.      * @param canvas 
  59.      * @param width 
  60.      * @param height 
  61.      */  
  62.     private void drawLine(Canvas canvas, int width, int height) {  
  63.         //设置线宽  
  64.       mPaint.setStrokeWidth(mFlakeSize);  
  65.         //y是竖直方向,就是下落  
  66.       double y1 = mLine.y1 + (mIncrement * Math.sin(1.5));  
  67.       double y2 = mLine.y2 + (mIncrement * Math.sin(1.5));  
  68.   
  69.       //这个是设置雨滴位置,如果在很短时间内刷新一次,就是连起来的动画效果  
  70.        mLine.set(mLine.x1,(int) y1,mLine.x2 ,(int) y2);  
  71.           
  72.         if (!isInsideLine(height)) {  
  73.             resetLine(width,height);  
  74.         }  
  75.           
  76.         canvas.drawLine(mLine.x1, mLine.y1, mLine.x2, mLine.y2, mPaint);  
  77.     }  
  78.       
  79.     // 判断是否在其中  
  80.     private boolean isInsideLine(int height) {  
  81.         return mLine.y1 < height && mLine.y2 < height;  
  82.     }  
  83.   
  84.     // 重置雨滴  
  85.     private void resetLine(int width, int height) {  
  86.         int [] nline;  
  87.         nline = mRandom.getLine(width, height);  
  88.         mLine.x1 = nline[0];  
  89.         mLine.y1 = nline[1];  
  90.         mLine.x2 = nline[2];  
  91.         mLine.y2 = nline[3];  
  92.     }  
  93.   
  94. }  

 

 

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