雪花飘飘动画效果
来源:互联网 发布:软件fmea可靠性分析 编辑:程序博客网 时间:2024/05/04 09:56
之前有搜索过相关的信息,但是都不是最全的,下面我们来直接了解这个特效吧:
下面我们来看一看代码的实现吧:
三个工具类:其中主函数中不需要写代码,只需布局有相应的控件即可,雪花的大小、颜色和数量等可以自己设置
1.***********************************SnowFlake.Java*************************************
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Point;
-
- import me.chunyu.spike.wcl_snowfall_demo.RandomGenerator;
-
-
-
-
-
-
- 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 - 1 && 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.***********************************SnowView.java*************************************
- import android.annotation.TargetApi;
- 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();
- }
- };
- }
3.*******************************RandomGenerator.java*****************************************
- import java.util.Random;
-
-
-
-
-
-
- public class RandomGenerator {
- private static final Random RANDOM = new 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);
- }
- }
一个布局:4.*******************************activity_main.xml*****************************************
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:contentDescription="@null"
- android:scaleType="centerCrop"
- android:src="@drawable/christmas"/>
-
- <me.chunyu.spike.wcl_snowfall_demo.views.SnowView
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
-
- </FrameLayout>
-
- </RelativeLayout>