自定义View——天气下雨效果
2015-12-29 08:44
267 查看
项目要实现下雨效果,在StylingAndroid中发现一篇有关下雪的效果,稍作修改,便可以实现下雨效果:
原地址:https://blog.stylingandroid.com/snowfall/
原地址:https://blog.stylingandroid.com/snowfall/
public class RainView extends View { private static final int NUM_RAIN_FLAKES = 100; private static final int DELAY = 5; private RainFlake[] rainflakes; 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); } protected void resize(int width, int height) { Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); rainflakes = new RainFlake[NUM_RAIN_FLAKES]; for (int i = 0; i < NUM_RAIN_FLAKES; i++) { rainflakes[i] = RainFlake.create(width, height, paint); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (w != oldw || h != oldh) { resize(w, h); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (RainFlake rainFlake : rainflakes) { rainFlake.draw(canvas); } getHandler().postDelayed(runnable, DELAY); } private Runnable runnable = new Runnable() { @Override public void run() { invalidate(); } }; }
class RainFlake { 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 = 4f; private static final float INCREMENT_UPPER = 6f; private static final float FLAKE_SIZE_LOWER = 7f; private static final float FLAKE_SIZE_UPPER = 25f; private final Random random; private final Point position; private float angle; private final float increment; private final float flakeSize; private final Paint paint; public static RainFlake create(int width, int height, Paint paint) { Random random = new Random(); 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 RainFlake(random, position, angle, increment, flakeSize, paint); } RainFlake(Random random, Point position, float angle, float increment, float flakeSize, Paint paint) { this.random = random; this.position = position; this.angle = angle; this.increment = increment; this.flakeSize = flakeSize; this.paint = paint; } private void move(int width, int height) { // double x = position.x + (increment * Math.cos(angle)); double x = position.x; double y = position.y + (increment * Math.sin(angle)); angle += random.getRandom(-ANGLE_SEED, ANGLE_SEED) / ANGLE_DIVISOR; position.set((int) x, (int) y); if (!isInside(width, height)) { reset(width); } } private boolean isInside(int width, int height) { int x = position.x; int y = position.y; return x >= -flakeSize - 1 && x + flakeSize <= width && y >= -flakeSize - 1 && y - flakeSize < height; } private void reset(int width) { position.x = random.getRandom(width); position.y = (int) (-flakeSize - 1); angle = random.getRandom(ANGLE_SEED) / ANGLE_SEED * ANGE_RANGE + HALF_PI - HALF_ANGLE_RANGE; } public void draw(Canvas canvas) { int width = canvas.getWidth(); int height = canvas.getHeight(); move(width, height); canvas.drawLine(position.x, position.y, position.x,position.y+flakeSize, paint); } }
class Random { private static final java.util.Random RANDOM = new java.util.Random(); public float getRandom(float lower, float upper) { float min = Math.min(lower, upper); float max = Math.max(lower, upper); return getRandom(max - min) + min; } public float getRandom(float upper) { return RANDOM.nextFloat() * upper; } public int getRandom(int upper) { return RANDOM.nextInt(upper); } }
相关文章推荐
- 原生js配合cookie制作保存路径的拖拽
- linux挂载共享文件
- 运算符 && 和 || 的返回值
- 03《软件需求分析教程》
- 图解.NET Stack和Heap的本质区别
- bzoj3140: [Hnoi2013]消毒
- http://imageresizing.net/
- 安装操作系统前硬盘如何分区与格式化
- unity3d 参考坐标系
- setFeature的妙用,解析XML时,外部注入预防即XXE攻击
- Xcode7 使用NSURLSession发送HTTP请求的问题
- HDU5600:N bulbs
- Python标准库06 子进程 (subprocess包)
- css固定元素位置(fixed)
- JavaScript总结
- createJs游戏引擎
- Leetcode: Smallest Rectangle Enclosing Black Pixels
- 阿里云服务器配置 SVN 服务器与生产站点同步
- Java深入(面试常见题)
- VS 2015相当不错的功能:C#交互窗口