Skip to content

Commit 2fbcae5

Browse files
committed
feat:Snow has be finished.
1 parent b7fce26 commit 2fbcae5

File tree

5 files changed

+87
-65
lines changed

5 files changed

+87
-65
lines changed

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/Rain.kt

+1-51
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
2626
length = value
2727
}
2828

29-
var translateX = 0f
3029
var timeSpace = 16
3130
// var originSpeed = speed
32-
var rainAlpha = 100
3331
var rainColor = Color.parseColor("#efefef")
34-
// override var time: Long = 5000 // 总共下落时间
3532

3633

3734
// 计算加速度
3835
override fun getAcceleration(): Float {
39-
// val acc = context.getScreenHeight() / (time * time).toFloat()
40-
// LogUtil.i(TAG, "acc = " + acc)
41-
// 恩,决定了,放弃加速,匀速走完
4236
return 0f
4337
}
4438

@@ -65,58 +59,14 @@ class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
6559
end.y = 0f
6660
}
6761

68-
// override fun initStyle(wtc: () -> Unit) {
69-
// super.initStyle(wtc)
70-
// }
71-
72-
override fun wtc() {
73-
val random = Random()
62+
override fun wtc(random: Random) {
7463
length = random.nextInt(5).toFloat() + originLength
7564
paint.apply {
7665
color = rainColor
7766
}
7867
}
7968

8069

81-
// 通过该方法来获取一个随机的初始化样式
82-
// override fun initStyle() {
83-
// val random = Random()
84-
// // 获取随机透明值
85-
// rainAlpha = random.nextInt(155) + 50
86-
// // 获得起点x偏移
87-
// translateX = random.nextInt(10).toFloat() + 5
88-
// // 获得长度
89-
// length = random.nextInt(5).toFloat() + originLength
90-
// // 获得宽度 5 ~ 8
91-
// width = random.nextInt(3) + 5f
92-
// if (!isRandom) {
93-
// start.x = translateX + originX
94-
// end.x = translateX + originX
95-
// } else {
96-
// // 如果是随机雨点,将x坐标随机范围扩大
97-
// val randomWidth = random.nextInt(context.getScreenWidth())
98-
// start.x = randomWidth + originX
99-
// end.x = randomWidth + originX
100-
// }
101-
// start.y = -length
102-
// end.y = 0f
103-
// paint.apply {
104-
// alpha = rainAlpha
105-
// strokeWidth = width
106-
// color = rainColor
107-
// isAntiAlias = true
108-
// }
109-
// super.initStyle {
110-
//
111-
// }
112-
// val random = Random()
113-
// length = random.nextInt(5).toFloat() + originLength
114-
// paint.apply {
115-
// color = rainColor
116-
// }
117-
118-
// }
119-
12070
override fun randomSpeed(random: Random): Float {
12171
// 获取随机速度 0.02 ~ 0.06
12272
var randomSpeed = random.nextFloat() / 10

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/Snow.kt

+45-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.xiasuhuei321.gank_kotlin.customview.weather
22

3-
import android.graphics.Canvas
4-
import android.graphics.PointF
3+
import android.graphics.*
4+
import com.xiasuhuei321.gank_kotlin.context
5+
import com.xiasuhuei321.gank_kotlin.extension.getScreenHeight
56
import java.util.*
67

78
/**
@@ -27,7 +28,13 @@ class Snow(start: PointF, end: PointF) : WeatherShape(start, end) {
2728

2829
override fun drawWhenInUse(canvas: Canvas) {
2930
// 通过圆心与半径确定圆的位置及大小
31+
val distance = speed * lastTime
32+
center.y += distance
33+
start.y += distance
34+
end.y += distance
35+
lastTime += 16
3036
canvas.drawCircle(center.x, center.y, radius, paint)
37+
if (end.y > context.getScreenHeight()) clear()
3138
}
3239

3340
fun calcCenter(): PointF {
@@ -38,6 +45,41 @@ class Snow(start: PointF, end: PointF) : WeatherShape(start, end) {
3845
}
3946

4047
override fun randomSpeed(random: Random): Float {
41-
return 0f
48+
// 获取随机速度
49+
var randomSpeed = random.nextFloat() / 10
50+
if (randomSpeed - 0.04f > 0.01f) {
51+
randomSpeed = 0.03f
52+
} else if (randomSpeed < 0.01f) {
53+
randomSpeed = 0.01f
54+
}
55+
56+
return randomSpeed
57+
}
58+
59+
override fun wtc(random: Random) {
60+
// 设置颜色渐变
61+
val shader = RadialGradient(center.x, center.y, radius,
62+
Color.parseColor("#FFFFFF"), Color.parseColor("#D1D1D1"),
63+
Shader.TileMode.CLAMP)
64+
// 外部设置的起始点其实并不对,先计算出半径
65+
radius = random.nextInt(10) + 15f
66+
// 根据半径计算start end
67+
end.x = start.x + radius
68+
end.y = start.y + radius
69+
// 计算圆心
70+
calcCenter()
71+
72+
paint.apply {
73+
setShader(shader)
74+
}
75+
}
76+
77+
fun clear() {
78+
isInUse = false
79+
lastTime = 0
80+
start.y = -radius * 2
81+
end.y = 0f
82+
83+
center = calcCenter()
4284
}
4385
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/WeatherShape.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,16 @@ abstract class WeatherShape(val start: PointF, val end: PointF) {
111111
isAntiAlias = true
112112
}
113113
// 如果有什么想要做的,刚好可以在追加上完成,就使用这个函数
114-
wtc()
114+
wtc(random)
115115
}
116116

117117
/**
118-
* 空实现的函数,将会在initStyle中调用。如果现有的
119-
* initStyle函数能满足你的需求,但是你还需要追加一些
120-
* 东西,你可以通过复写此函数实现
121-
* empty body, this will be invoke in initStyle
122-
* method.If current initStyle method can // 满足不会。。。
118+
* Empty body, this will be invoke in initStyle
119+
* method.If current initStyle method can satisfy your need
123120
* but you still add something, by override this method
124-
* can be a good idea.
121+
* will be a good idea to solve the problem.
125122
*/
126-
open fun wtc(): Unit {
123+
open fun wtc(random:Random): Unit {
127124

128125
}
129126

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/WeatherShapePool.kt

+34-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.graphics.Canvas
44
import android.graphics.PointF
55
import com.xiasuhuei321.gank_kotlin.context
66
import com.xiasuhuei321.gank_kotlin.extension.getScreenWidth
7-
import java.util.*
87

98
/**
109
* Created by xiasuhuei321 on 2017/9/7.
@@ -15,9 +14,13 @@ class WeatherShapePool {
1514
val constantRain = ArrayList<Rain>()
1615
val randomRain = ArrayList<Rain>()
1716

17+
val constantSnow = ArrayList<Snow>()
18+
val randomSnow = ArrayList<Snow>()
19+
1820
init {
1921
// 初始化
2022
initData()
23+
initSnow()
2124
}
2225

2326
private fun initData() {
@@ -48,4 +51,34 @@ class WeatherShapePool {
4851
r.draw(canvas)
4952
}
5053
}
54+
55+
private fun initSnow(){
56+
val space = context.getScreenWidth() / 20
57+
var currentSpace = 0f
58+
// 将其均匀的分布在屏幕x方向上
59+
for (i in 0..19) {
60+
val snow = Snow(PointF(currentSpace, 0f), PointF(currentSpace, 0f))
61+
snow.originX = currentSpace
62+
snow.radius = 20f
63+
constantSnow.add(snow)
64+
currentSpace += space
65+
}
66+
67+
for (j in 0..19) {
68+
val snow = Snow(PointF(0f, 0f), PointF(0f, 0f))
69+
snow.isRandom = true
70+
snow.radius = 20f
71+
randomSnow.add(snow)
72+
}
73+
}
74+
75+
fun drawSnow(canvas: Canvas){
76+
for(r in constantSnow){
77+
r.draw(canvas)
78+
}
79+
80+
for (r in randomSnow){
81+
r.draw(canvas)
82+
}
83+
}
5184
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/WeatherView.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
5858
Thread.sleep(16 - drawTime)
5959
}
6060
} catch (e: Exception) {
61-
e.printStackTrace()
61+
// e.printStackTrace()
6262
}
6363
}
6464
}.apply { name = "WeatherThread" }
@@ -100,7 +100,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
100100

101101
private fun draw(canvas: Canvas, type: Weather, startTime: Long) {
102102
// type什么的先放一边,先实现一个
103-
weatherShapePool.drawRain(canvas)
103+
weatherShapePool.drawSnow(canvas)
104104
}
105105

106106
enum class Weather {

0 commit comments

Comments
 (0)