标签:
英文原文地址: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;
-
-
-
-
- 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];
-
- 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 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;
- 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) {
-
-
- double x = mPosition.x + (mIncrement * Math.cos(mAngle));
-
- 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;
-
-
-
-
- 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];
-
- 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 (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);
- }
-
-
-
-
-
-
-
- private void drawLine(Canvas canvas, int width, int height) {
-
- mPaint.setStrokeWidth(mFlakeSize);
-
- 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
英文原文地址: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;
-
-
-
-
- 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];
-
- 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 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;
- 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) {
-
-
- double x = mPosition.x + (mIncrement * Math.cos(mAngle));
-
- 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;
-
-
-
-
- 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];
-
- 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 (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);
- }
-
-
-
-
-
-
-
- private void drawLine(Canvas canvas, int width, int height) {
-
- mPaint.setStrokeWidth(mFlakeSize);
-
- 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
android 经典下雨效果
标签:
原文地址:http://www.cnblogs.com/xiguameng/p/5677729.html