Browse Source

增加雪花

tags/old_version_1
fyf 1 year ago
parent
commit
4f93c51807
8 changed files with 731 additions and 5 deletions
  1. +223
    -0
      app/src/main/java/com/bonait/bnframework/common/bg/SnowView.java
  2. +268
    -0
      app/src/main/java/com/bonait/bnframework/common/bg/StarView.java
  3. +72
    -0
      app/src/main/java/com/bonait/bnframework/common/bg/mode/ColorUtils.java
  4. +42
    -0
      app/src/main/java/com/bonait/bnframework/common/bg/mode/SnowBean.java
  5. +95
    -0
      app/src/main/java/com/bonait/bnframework/common/bg/mode/StarBean.java
  6. +26
    -1
      app/src/main/java/com/bonait/bnframework/common/helper/Tools.java
  7. +1
    -0
      app/src/main/java/com/bonait/bnframework/modules/welcome/activity/LoginActivity.java
  8. +4
    -4
      app/src/main/res/layout/activity_login.xml

+ 223
- 0
app/src/main/java/com/bonait/bnframework/common/bg/SnowView.java View File

@@ -0,0 +1,223 @@
package com.bonait.bnframework.common.bg;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RelativeLayout;


import com.bonait.bnframework.common.bg.mode.SnowBean;
import com.bonait.bnframework.common.helper.Tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class SnowView extends RelativeLayout {
// private final String SNOW = "❄";
// private final String SNOW = "☀❆★❉❈❀✿❃❁";
private final String SNOW = "❄";
private float vX = 0.5f;//风向 >0 右边飘 <0 左边飘
private float vY = 4f;//下落速度 <0你的雪花要往上飘呀
private int snowCount = 10;//雪花个数
private List<SnowBean> snowBeanList = new ArrayList<>();

private int XB = Tools.getWindowsWidth();
private int YB = Tools.getWindowsHeight();

private Paint paint = new Paint();
private Timer timer;

private boolean isStart = false;

public SnowView(Context context) {
this(context, null);
}

public SnowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public SnowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}

private void initView() {
paint.setAntiAlias(true);
initSnowData();
}

public void start() {
if (timer == null) {
timer = new Timer();
}
isStart = true;
timer.schedule(new TimerTask() {
@Override
public void run() {

if (!isStart) return;
for (int i = 0; i < snowBeanList.size(); i++) {

snowBeanList.get(i).setX(snowBeanList.get(i).getX() + vX);
snowBeanList.get(i).setY(snowBeanList.get(i).getY() + vY);

if (snowBeanList.get(i).getX() < 0 || snowBeanList.get(i).getX() > XB) {
snowBeanList.get(i).setX(getRandomX());
}
if (snowBeanList.get(i).getY() < 0 || snowBeanList.get(i).getY() > YB) {
snowBeanList.get(i).setY(0f);
}
}

postInvalidate();
}
}, 0, 15);
}

public void resume() {
if (timer == null) {
start();
}
isStart = true;
}

public void pause(){
isStart = false;
}

public void destroy() {
isStart = false;
if (snowBeanList != null) {
snowBeanList.clear();
}
invalidate();
if (timer != null) {
timer.cancel();
timer = null;
}
}

private void initSnowData() {
for (int i = 0; i < snowCount; i++) {
SnowBean bean = new SnowBean();
bean.setX(getRandomX());
bean.setY(getRandomY());
bean.setSize((float) (Math.random() * 10) + 5);
snowBeanList.add(bean);
}
start();
}

private float getRandomX() {
return (float) (Math.random() * Tools.getWindowsWidth());
}

private float getRandomY() {
return (float) (Math.random() * Tools.getWindowsHeight());
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < snowBeanList.size(); i++) {
SnowBean bean = snowBeanList.get(i);
paint.setTextSize(bean.getSize());
paint.setColor(bean.getColor());
canvas.drawText(SNOW, bean.getX(), bean.getY(), paint);
}
}

// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// setMeasuredDimension(800,1280);
// Log.d("雪花宽高", "onMeasure"+widthMeasureSpec+" "+heightMeasureSpec);
// }

private GestureDetector detector = new GestureDetector(getContext(),new MyGestureDetector());
private boolean isPoint = false;
private long pointTime = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
// switch (event.getAction()) {
// case MotionEvent.ACTION_DOWN:
// pointTime = 0;
// int pCount = event.getPointerCount();
// if (pCount >= 2) {
// isPoint = true;
// pointTime = System.currentTimeMillis();
// }
// break;
// case MotionEvent.ACTION_MOVE:
// break;
// case MotionEvent.ACTION_UP:
// isPoint = false;
// pointTime = 0;
// break;
// }

// return super.onTouchEvent(event);
return detector.onTouchEvent(event);
}

private class MyGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

@Override
public boolean onDown(MotionEvent e) {
return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
}
}

+ 268
- 0
app/src/main/java/com/bonait/bnframework/common/bg/StarView.java View File

@@ -0,0 +1,268 @@
package com.bonait.bnframework.common.bg;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.bonait.bnframework.common.bg.mode.ColorUtils;
import com.bonait.bnframework.common.bg.mode.StarBean;
import com.bonait.bnframework.common.helper.Tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class StarView extends RelativeLayout {
private static final String TAG = "StarView";

private Paint paint;
private Paint paintHead;
private Paint paintHeadLight;
private Paint paintHeadLight2;

private int bw = Tools.getWindowsWidth();
private int bh = Tools.getWindowsHeight();

private double sweep = 340;//角度
private int count = 100;
private double pointCount = 100;
private List<StarBean> list = new ArrayList<>();
private Timer timer;
private boolean isStart = false;

private Timer playTimer;
private int s = 0;
// private int currentColor = ColorUtils.getRandomColor();

public StarView(Context context) {
this(context, null);
}

public StarView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public StarView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
LinearGradient linearGradient = new LinearGradient(
0f, 0f, 0f, 1000,
new int[]{Color.TRANSPARENT, Color.RED},
new float[]{0f, 1f},
Shader.TileMode.CLAMP
);
paint.setShader(linearGradient);

paintHead = new Paint();
paintHead.setStyle(Paint.Style.FILL);
paintHead.setAntiAlias(true);
paintHead.setColor(Color.WHITE);

paintHeadLight = new Paint();
paintHeadLight.setStyle(Paint.Style.FILL);
paintHeadLight.setAntiAlias(true);
paintHeadLight.setColor(Color.argb(100, 255, 255, 255));

paintHeadLight2 = new Paint();
paintHeadLight2.setStyle(Paint.Style.FILL);
paintHeadLight2.setAntiAlias(true);
paintHeadLight2.setColor(Color.argb(30, 255, 255, 255));


initStar();
initPoint();

start();

//播放组合
initPlay();
}

private void initPlay() {
playTimer = new Timer();

s = 0;
playTimer.schedule(new TimerTask() {
@Override
public void run() {
s++;
if (s != 0 && s % 2 == 0) {
int color = ColorUtils.getRandomColor();
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).isPoint()){
list.get(i).setColor(color);
}
}
}
if (s >= 1000) {//防止数据过大
s = 0;
}
}
}, 0, 1000);
}

private void initStar() {
int color = ColorUtils.getColor("#1976D2");//ColorUtils.getRandomColor();
for (int i = 0; i < count; i++) {
StarBean bean = getStarBean();
bean.setColor(color);
list.add(bean);
}
}

public StarBean getStarBean() {
StarBean bean = new StarBean();
// setBeanXY(bean);
bean.setX((float) (Math.random() * bw));
bean.setY((float) (Math.random() * bh));
bean.setR((float) (Math.random() * 3 + 0.3f));
return bean;
}

public void initPoint(){
for (int i = 0; i < pointCount; i++) {
StarBean bean = getStarBean();
bean.setPoint(true);
bean.setColor(Color.WHITE);
bean.setR((float) (Math.random() * 1.5 + 0.1f));
list.add(bean);
}
}

private void setBeanXY(StarBean bean){
if (Math.random() >= 0.5) {
bean.setX(0);
bean.setY((float) (Math.random() * bh));
} else {
bean.setX((float) (Math.random() * bw));
bean.setY(0);
}
}

public void addStar(int count) {
for (int i = 0; i < count; i++) {
StarBean bean = getStarBean();
list.add(bean);
}
}

public void addStar(int count, int color) {
for (int i = 0; i < count; i++) {
StarBean bean = getStarBean();
bean.setColor(color);
list.add(bean);
}
}


public void start() {
if (timer == null) {
timer = new Timer();
}
timer.schedule(new TimerTask() {
@Override
public void run() {

for (int i = 0; i < list.size(); i++) {

list.get(i).setX(list.get(i).getX() + list.get(i).getvX());
list.get(i).setY(list.get(i).getY() + list.get(i).getvY());

if (
list.get(i).getX() < 0
|| list.get(i).getX() > (bw + Math.sin(sweep) * list.get(i).getL())
|| list.get(i).getY() > (bh + Math.cos(sweep) * list.get(i).getL())
|| list.get(i).getY() < 0
) {
setBeanXY(list.get(i));
// list.get(i).setX((float) (Math.random() * bw));
// list.get(i).setY((float) (Math.random() * bh));
}

}
postInvalidate();
}
}, 0, 15);
}

public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}

if (playTimer != null) {
playTimer.cancel();
playTimer = null;
}
}


private Path getPath(float x, float y, double sweep, float r, StarBean bean) {
Path path = new Path();

float l = bean.getL();
float startX = (float) (x + r * Math.cos(sweep));
float startY = (float) (y - r * Math.sin(sweep));
float twoX = (float) (x - r * Math.cos(sweep));
float twoY = (float) (y + r * Math.sin(sweep));
float threeX = (float) (x - l * Math.sin(sweep));
float threeY = (float) (y - l * Math.cos(sweep));

path.moveTo(startX, startY);
path.lineTo(twoX, twoY);
path.lineTo(threeX, threeY);
path.lineTo(startX, startY);
path.close();

LinearGradient linearGradient = new LinearGradient(
threeX, threeY, x, y,
new int[]{Color.TRANSPARENT, bean.getColor()},
new float[]{0f, 1f},
Shader.TileMode.CLAMP
);
paint.setShader(linearGradient);
return path;

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawColor(Color.BLACK);

// path.moveTo(startX, startY);
// path.lineTo((mStartSize - mEndSize) / 2, Height);
// path.lineTo(((mStartSize - mEndSize) / 2 + mEndSize), Height);
// path.lineTo(mStartSize, 0f);
// path.lineTo(startX, startY);

for (int i = 0; i < list.size(); i++) {
StarBean bean = list.get(i);
if (!bean.isPoint()) {
Path path = getPath(bean.getX(), bean.getY(), sweep, bean.getR(), bean);
canvas.drawPath(path, paint);
canvas.drawCircle(bean.getX(), bean.getY(), bean.getHeadRLight2(), paintHeadLight2);
canvas.drawCircle(bean.getX(), bean.getY(), bean.getHeadRLight(), paintHeadLight);
canvas.drawCircle(bean.getX(), bean.getY(), bean.getHeadR(), paintHead);
} else {
canvas.drawCircle(bean.getX(), bean.getY(), bean.getR(), paintHead);
}
}

}
}

+ 72
- 0
app/src/main/java/com/bonait/bnframework/common/bg/mode/ColorUtils.java View File

@@ -0,0 +1,72 @@
package com.bonait.bnframework.common.bg.mode;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;

import com.bonait.bnframework.MainApplication;
import com.bonait.bnframework.common.helper.Tools;

public class ColorUtils {

public static int getColor(String color)
{
return Color.parseColor(color);
}
public static int getRandomColor() {
StringBuffer buffer = new StringBuffer();
buffer.append("#");
for (int i = 0; i < 6; i++) {
buffer.append(arr[(int) (Math.random() * arr.length)]);
}
return Color.parseColor(buffer.toString());
}

private static String[] arr = new String[]{
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"a",
"b",
"c",
"d",
"e",
"f"
};


public static Drawable getRandomDrawable() {
GradientDrawable drawable = new GradientDrawable();
drawable.setOrientation(GradientDrawable.Orientation.TR_BL);
drawable.setCornerRadius(Tools.dip2px(MainApplication.getContext(), 10));
drawable.setColors(new int[]{
getRandomColor(),
getRandomColor(),
getRandomColor(),
getRandomColor(),
});
return drawable;
}

public static Drawable getRandomDrawable(float radiusDp) {
GradientDrawable drawable = new GradientDrawable();
drawable.setOrientation(GradientDrawable.Orientation.TR_BL);
drawable.setCornerRadius(Tools.dip2px(MainApplication.getContext(), radiusDp));
drawable.setColors(new int[]{
getRandomColor(),
getRandomColor(),
getRandomColor(),
getRandomColor(),
});
return drawable;
}

}

+ 42
- 0
app/src/main/java/com/bonait/bnframework/common/bg/mode/SnowBean.java View File

@@ -0,0 +1,42 @@
package com.bonait.bnframework.common.bg.mode;

import android.graphics.Color;

public class SnowBean {
float x;
float y;
float size;
int color = Color.YELLOW;

public float getX() {
return x;
}

public void setX(float x) {
this.x = x;
}

public float getY() {
return y;
}

public void setY(float y) {
this.y = y;
}

public float getSize() {
return size;
}

public void setSize(float size) {
this.size = size;
}

public int getColor() {
return color;
}

public void setColor(int color) {
this.color = color;
}
}

+ 95
- 0
app/src/main/java/com/bonait/bnframework/common/bg/mode/StarBean.java View File

@@ -0,0 +1,95 @@
package com.bonait.bnframework.common.bg.mode;


public class StarBean {

private float x;
private float y;

private float r;

private int color = ColorUtils.getColor("#1976D2");;;//ColorUtils.getRandomColor();

private float l;

private float headR;
private float headRLight;
private float headRLight2;

private float vX;
private float vY;

private boolean isPoint = false;

public float getL() {
l = r * 10 + getX() * 0.3f;
return l;
}

public int getColor() {
return color;
}

public void setColor(int color) {
this.color = color;
}

public float getHeadR() {
return headR;
}

public float getHeadRLight() {
return headRLight;
}

public float getHeadRLight2() {
return headRLight2;
}

public float getvX() {
return vX;
}

public float getvY() {
return vY;
}

public float getX() {
return x;
}

public void setX(float x) {
this.x = x;
}

public float getY() {
return y;
}

public void setY(float y) {
this.y = y;
}

public float getR() {

return r;
}

public void setR(float r) {
this.r = r;
vX = r * 1.5f;
vY = r * 2f;
headR = r + r * 0.3f;
headRLight = r + r * 2f;
headRLight2 = headRLight + headRLight * 0.6f;
}

public boolean isPoint() {
return isPoint;
}

public void setPoint(boolean point) {
isPoint = point;
}
}


+ 26
- 1
app/src/main/java/com/bonait/bnframework/common/helper/Tools.java View File

@@ -13,8 +13,11 @@ import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

import com.bonait.bnframework.MainApplication;
import com.google.gson.Gson;

import org.json.JSONException;
@@ -108,7 +111,7 @@ public class Tools {
* @param dipValue
* @return
*/
public int dip2px(Context context, float dipValue) {
public static int dip2px(Context context, float dipValue) {

final float scale = context.getResources().getDisplayMetrics().density;

@@ -267,4 +270,26 @@ public class Tools {
}
return isurl;
}

/**
* 获取屏幕的宽度
*/
public static int getWindowsWidth() {
WindowManager wm = (WindowManager) (MainApplication.getContext().getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int mScreenWidth = dm.widthPixels;
return mScreenWidth;
}

/**
* 获取屏幕的高度
*/
public static int getWindowsHeight() {
WindowManager wm = (WindowManager) (MainApplication.getContext().getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int mScreenHeigh = dm.heightPixels;
return mScreenHeigh;
}
}

+ 1
- 0
app/src/main/java/com/bonait/bnframework/modules/welcome/activity/LoginActivity.java View File

@@ -23,6 +23,7 @@ import android.widget.LinearLayout;
import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.bg.SnowView;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.Constants;
import com.bonait.bnframework.common.constant.SPConstants;


+ 4
- 4
app/src/main/res/layout/activity_login.xml View File

@@ -9,7 +9,10 @@
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".modules.welcome.activity.LoginActivity">

<com.bonait.bnframework.common.bg.SnowView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"/>
<ImageView
android:id="@+id/logo"
android:layout_width="100dp"
@@ -20,7 +23,6 @@
android:background="@null"
android:scaleType="centerCrop"
android:src="@mipmap/ico"/>

<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
@@ -32,7 +34,6 @@
android:fillViewport="true"
android:scrollbarThumbVertical="@android:color/transparent"
android:scrollbars="vertical">

<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
@@ -174,7 +175,6 @@
</LinearLayout>

</androidx.core.widget.NestedScrollView>

<!--<LinearLayout
android:id="@+id/service"
android:layout_width="match_parent"


Loading…
Cancel
Save