雪花飘飘动画效果

来源:互联网 发布:软件fmea可靠性分析 编辑:程序博客网 时间:2024/05/04 09:56

    之前有搜索过相关的信息,但是都不是最全的,下面我们来直接了解这个特效吧:






下面我们来看一看代码的实现吧:


  三个工具类:其中主函数中不需要写代码,只需布局有相应的控件即可,雪花的大小、颜色和数量等可以自己设置


1.***********************************SnowFlake.Java*************************************

[java] view plain copy
  1. import android.graphics.Canvas;  
  2. import android.graphics.Paint;  
  3. import android.graphics.Point;  
  4.   
  5. import me.chunyu.spike.wcl_snowfall_demo.RandomGenerator;  
  6.   
  7. /** 
  8.  * 雪花的类, 移动, 移出屏幕会重新设置位置. 
  9.  * <p/> 
  10.  * Created by wangchenlong on 16/1/24. 
  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.     // 雪花的移动速度  
  21.     private static final float INCREMENT_LOWER = 2f;  
  22.     private static final float INCREMENT_UPPER = 4f;  
  23.   
  24.     // 雪花的大小  
  25.     private static final float FLAKE_SIZE_LOWER = 7f;  
  26.     private static final float FLAKE_SIZE_UPPER = 20f;  
  27.   
  28.     private final RandomGenerator mRandom; // 随机控制器  
  29.     private final Point mPosition; // 雪花位置  
  30.     private float mAngle; // 角度  
  31.     private final float mIncrement; // 雪花的速度  
  32.     private final float mFlakeSize; // 雪花的大小  
  33.     private final Paint mPaint; // 画笔  
  34.   
  35.     private SnowFlake(RandomGenerator random, Point position, float angle, float increment, float flakeSize, Paint paint) {  
  36.         mRandom = random;  
  37.         mPosition = position;  
  38.         mIncrement = increment;  
  39.         mFlakeSize = flakeSize;  
  40.         mPaint = paint;  
  41.         mAngle = angle;  
  42.     }  
  43.   
  44.     public static SnowFlake create(int width, int height, Paint paint) {  
  45.         RandomGenerator random = new RandomGenerator();  
  46.         int x = random.getRandom(width);  
  47.         int y = random.getRandom(height);  
  48.         Point position = new Point(x, y);  
  49.         float angle = random.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;  
  50.         float increment = random.getRandom(INCREMENT_LOWER, INCREMENT_UPPER);  
  51.         float flakeSize = random.getRandom(FLAKE_SIZE_LOWER, FLAKE_SIZE_UPPER);  
  52.         return new SnowFlake(random, position, angle, increment, flakeSize, paint);  
  53.     }  
  54.   
  55.     // 绘制雪花  
  56.     public void draw(Canvas canvas) {  
  57.         int width = canvas.getWidth();  
  58.         int height = canvas.getHeight();  
  59.         move(width, height);  
  60.         canvas.drawCircle(mPosition.x, mPosition.y, mFlakeSize, mPaint);  
  61.     }  
  62.   
  63.     // 移动雪花  
  64.     private void move(int width, int height) {  
  65.         double x = mPosition.x + (mIncrement * Math.cos(mAngle));  
  66.         double y = mPosition.y + (mIncrement * Math.sin(mAngle));  
  67.   
  68.         mAngle += mRandom.getRandom(-ANGLE_SEED, ANGLE_SEED) / ANGLE_DIVISOR; // 随机晃动  
  69.   
  70.         mPosition.set((int) x, (int) y);  
  71.   
  72.         // 移除屏幕, 重新开始  
  73.         if (!isInside(width, height)) {  
  74.             reset(width);  
  75.         }  
  76.     }  
  77.   
  78.     // 判断是否在其中  
  79.     private boolean isInside(int width, int height) {  
  80.         int x = mPosition.x;  
  81.         int y = mPosition.y;  
  82.         return x >= -mFlakeSize - 1 && x + mFlakeSize <= width && y >= -mFlakeSize - 1 && y - mFlakeSize < height;  
  83.     }  
  84.   
  85.     // 重置雪花  
  86.     private void reset(int width) {  
  87.         mPosition.x = mRandom.getRandom(width);  
  88.         mPosition.y = (int) (-mFlakeSize - 1); // 最上面  
  89.         mAngle = mRandom.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE;  
  90.     }  
  91. }  


2.***********************************SnowView.java*************************************


[java] view plain copy
  1. import android.annotation.TargetApi;  
  2. import android.content.Context;  
  3. import android.graphics.Canvas;  
  4. import android.graphics.Color;  
  5. import android.graphics.Paint;  
  6. import android.util.AttributeSet;  
  7. import android.view.View;  
  8.   
  9. /** 
  10.  * 雪花视图, DELAY时间重绘, 绘制NUM_SNOWFLAKES个雪花 
  11.  * <p/> 
  12.  * Created by wangchenlong on 16/1/24. 
  13.  */  
  14. public class SnowView extends View {  
  15.   
  16.     private static final int NUM_SNOWFLAKES = 150// 雪花数量  
  17.     private static final int DELAY = 5// 延迟  
  18.     private SnowFlake[] mSnowFlakes; // 雪花  
  19.   
  20.     public SnowView(Context context) {  
  21.         super(context);  
  22.     }  
  23.   
  24.     public SnowView(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.     }  
  27.   
  28.     public SnowView(Context context, AttributeSet attrs, int defStyleAttr) {  
  29.         super(context, attrs, defStyleAttr);  
  30.     }  
  31.   
  32.      
  33.   
  34.     @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  35.         super.onSizeChanged(w, h, oldw, oldh);  
  36.         if (w != oldw || h != oldh) {  
  37.             initSnow(w, h);  
  38.         }  
  39.     }  
  40.   
  41.     private void initSnow(int width, int height) {  
  42.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // 抗锯齿  
  43.         paint.setColor(Color.WHITE); // 白色雪花  
  44.         paint.setStyle(Paint.Style.FILL); // 填充;  
  45.         mSnowFlakes = new SnowFlake[NUM_SNOWFLAKES];  
  46.         for (int i = 0; i < NUM_SNOWFLAKES; ++i) {  
  47.             mSnowFlakes[i] = SnowFlake.create(width, height, paint);  
  48.         }  
  49.     }  
  50.   
  51.     @Override protected void onDraw(Canvas canvas) {  
  52.         super.onDraw(canvas);  
  53.         for (SnowFlake s : mSnowFlakes) {  
  54.             s.draw(canvas);  
  55.         }  
  56.         // 隔一段时间重绘一次, 动画效果  
  57.         getHandler().postDelayed(runnable, DELAY);  
  58.     }  
  59.   
  60.     // 重绘线程  
  61.     private Runnable runnable = new Runnable() {  
  62.         @Override  
  63.         public void run() {  
  64.             invalidate();  
  65.         }  
  66.     };  
  67. }  


3.*******************************RandomGenerator.java*****************************************


[java] view plain copy
  1. import java.util.Random;  
  2.   
  3. /** 
  4.  * 随机生成器 
  5.  * <p/> 
  6.  * Created by wangchenlong on 16/1/24. 
  7.  */  
  8. public class RandomGenerator {  
  9.     private static final Random RANDOM = new Random();  
  10.   
  11.     // 区间随机  
  12.     public float getRandom(float lower, float upper) {  
  13.         float min = Math.min(lower, upper);  
  14.         float max = Math.max(lower, upper);  
  15.         return getRandom(max - min) + min;  
  16.     }  
  17.   
  18.     // 上界随机  
  19.     public float getRandom(float upper) {  
  20.         return RANDOM.nextFloat() * upper;  
  21.     }  
  22.   
  23.     // 上界随机  
  24.     public int getRandom(int upper) {  
  25.         return RANDOM.nextInt(upper);  
  26.     }  
  27. }  

  一个布局:

4.*******************************activity_main.xml*****************************************


[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <FrameLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent">  
  10.   
  11.         <ImageView  
  12.             android:layout_width="match_parent"  
  13.             android:layout_height="match_parent"  
  14.             android:contentDescription="@null"  
  15.             android:scaleType="centerCrop"  
  16.             android:src="@drawable/christmas"/>  
  17.   
  18.         <me.chunyu.spike.wcl_snowfall_demo.views.SnowView  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="match_parent"/>  
  21.   
  22.     </FrameLayout>  
  23.   
  24. </RelativeLayout>  
原创粉丝点击