Android营造雪花和雨滴浪漫效果
内容摘要
本文在实现雪花效果的基础上,根据漫天飞舞雪花,实现下雨天场景的效果,使用eclipse android 版本,具体内容如下
雪花效果图:
具体代码:
1、漫天飞舞的雪花主要代码
SnowView
雪花效果图:
具体代码:
1、漫天飞舞的雪花主要代码
SnowView
文章正文
本文在实现雪花效果的基础上,根据漫天飞舞雪花,实现下雨天场景的效果,使用eclipse android 版本,具体内容如下
雪花效果图:
具体代码:
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]; } }
雨滴效果图:
以上就是本文的全部内容,帮助大家轻松实现浪漫的雪花雨滴效果,大家可以把效果运用到自己的项目中,希望大家喜欢。
代码注释