Skip to content

Commit b7fce26

Browse files
committed
refactor: make code more clear
Do more thing in parent than before.
1 parent 72a3841 commit b7fce26

File tree

5 files changed

+177
-66
lines changed

5 files changed

+177
-66
lines changed

app/src/main/cpp/image-lib.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ int *blur_ARGB_8888(int *pix, int w, int h, int radius) {
160160
yi = x;
161161
stackpointer = radius;
162162
for (y = 0; y < h; y++) {
163-
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
163+
// Preserve shapeAlpha channel: ( 0xff000000 & pix[yi] )
164164
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
165165

166166
rsum -= routsum;

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

+52-52
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package com.xiasuhuei321.gank_kotlin.customview.weather
22

33
import android.graphics.Canvas
44
import android.graphics.Color
5-
import android.graphics.Paint
65
import android.graphics.PointF
76
import com.xiasuhuei321.gank_kotlin.context
87
import com.xiasuhuei321.gank_kotlin.extension.getScreenHeight
9-
import com.xiasuhuei321.gank_kotlin.extension.getScreenWidth
108
import java.util.*
119

1210
/**
@@ -20,7 +18,6 @@ class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
2018

2119
override var TAG = "Rain"
2220

23-
var width = 5f
2421
// 用户可以设置
2522
var length = 20f
2623
var originLength = 20f
@@ -29,21 +26,13 @@ class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
2926
length = value
3027
}
3128

32-
var originX = 0f
3329
var translateX = 0f
3430
var timeSpace = 16
3531
// var originSpeed = speed
3632
var rainAlpha = 100
37-
private var lastTime = 0L // 从开始到现在下落所经过的时间
3833
var rainColor = Color.parseColor("#efefef")
3934
// override var time: Long = 5000 // 总共下落时间
4035

41-
var paint = Paint().apply {
42-
color = rainColor
43-
strokeWidth = width
44-
isAntiAlias = true
45-
alpha = rainAlpha
46-
}
4736

4837
// 计算加速度
4938
override fun getAcceleration(): Float {
@@ -56,12 +45,7 @@ class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
5645
/**
5746
* 绘制过程在此完成
5847
*/
59-
override fun draw(canvas: Canvas) {
60-
if (!isInUse) {
61-
lastTime += randomPre()
62-
initStyle()
63-
isInUse = true
64-
}
48+
override fun drawWhenInUse(canvas: Canvas) {
6549
val distance = speed * lastTime
6650
start.y += distance
6751
end.y += distance
@@ -81,52 +65,68 @@ class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
8165
end.y = 0f
8266
}
8367

84-
/**
85-
* @see WeatherShape
86-
*/
87-
override fun randomPre(): Long {
88-
val random = Random()
89-
val pre = random.nextInt(1000).toLong()
90-
// LogUtil.i("asdf", "random = $delay ")
91-
return pre
92-
}
68+
// override fun initStyle(wtc: () -> Unit) {
69+
// super.initStyle(wtc)
70+
// }
9371

94-
// 通过该方法来获取一个随机的初始化样式
95-
override fun initStyle() {
72+
override fun wtc() {
9673
val random = Random()
97-
// 获取随机透明值
98-
rainAlpha = random.nextInt(155) + 50
99-
// 获得起点x偏移
100-
translateX = random.nextInt(10).toFloat() + 5
101-
// 获得长度
10274
length = random.nextInt(5).toFloat() + originLength
103-
// 获得宽度 5 ~ 8
104-
width = random.nextInt(3) + 5f
105-
if (!isRandom) {
106-
start.x = translateX + originX
107-
end.x = translateX + originX
108-
} else {
109-
// 如果是随机雨点,将x坐标随机范围扩大
110-
val randomWidth = random.nextInt(context.getScreenWidth())
111-
start.x = randomWidth + originX
112-
end.x = randomWidth + originX
75+
paint.apply {
76+
color = rainColor
11377
}
78+
}
79+
80+
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+
120+
override fun randomSpeed(random: Random): Float {
114121
// 获取随机速度 0.02 ~ 0.06
115122
var randomSpeed = random.nextFloat() / 10
116123
if (randomSpeed - 0.05f > 0.01f) {
117124
randomSpeed -= 0.05f
118125
} else if (randomSpeed < 0.02f) {
119126
randomSpeed = 0.02f
120127
}
121-
speed = randomSpeed
122-
start.y = -length
123-
end.y = 0f
124-
paint.apply {
125-
alpha = rainAlpha
126-
strokeWidth = width
127-
color = rainColor
128-
isAntiAlias = true
129-
}
128+
129+
return randomSpeed
130130
}
131131

132132

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

+22-4
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@ package com.xiasuhuei321.gank_kotlin.customview.weather
22

33
import android.graphics.Canvas
44
import android.graphics.PointF
5+
import java.util.*
56

67
/**
78
* Created by xiasuhuei321 on 2017/9/5.
89
* author:luo
910
1011
*/
1112
class Snow(start: PointF, end: PointF) : WeatherShape(start, end) {
13+
14+
/**
15+
* 圆心,用户可以改变这个值
16+
*/
17+
var center = calcCenter()
18+
19+
/**
20+
* 半径
21+
*/
22+
var radius = 10f
23+
1224
override fun getAcceleration(): Float {
1325
return 0f
1426
}
1527

16-
override fun draw(canvas: Canvas) {
28+
override fun drawWhenInUse(canvas: Canvas) {
29+
// 通过圆心与半径确定圆的位置及大小
30+
canvas.drawCircle(center.x, center.y, radius, paint)
1731
}
1832

19-
override fun randomPre(): Long {
20-
return 1
33+
fun calcCenter(): PointF {
34+
val center = PointF(0f, 0f)
35+
center.x = (start.x + end.x) / 2f
36+
center.y = (start.y + end.y) / 2f
37+
return center
2138
}
2239

23-
override fun initStyle() {
40+
override fun randomSpeed(random: Random): Float {
41+
return 0f
2442
}
2543
}

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

+96-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
package com.xiasuhuei321.gank_kotlin.customview.weather
22

33
import android.graphics.Canvas
4+
import android.graphics.Paint
45
import android.graphics.PointF
6+
import com.xiasuhuei321.gank_kotlin.context
7+
import com.xiasuhuei321.gank_kotlin.extension.getScreenWidth
8+
import java.util.*
59

610
/**
711
* Created by xiasuhuei321 on 2017/9/5.
812
* author:luo
913
14+
*
15+
* desc: All shape's parent class.It describes a shape will have
16+
* what feature.It's draw flows are:
17+
* 1.Outside the class init some value such as the start and the
18+
* end point.
19+
* 2.Invoke draw(Canvas) method, in this method, there are still
20+
* two flows:
21+
* 1) Get random value to init paint, this will affect the shape
22+
* draw style.
23+
* 2) When the shape is not used, invoke init method, and when it
24+
* is not used invoke drawWhenInUse(Canvas) method. It should be
25+
* override by user and to implement draw itself.
26+
*
1027
*/
1128
abstract class WeatherShape(val start: PointF, val end: PointF) {
1229
open var TAG = "WeatherShape"
@@ -27,27 +44,98 @@ abstract class WeatherShape(val start: PointF, val end: PointF) {
2744
var speed = 0.05f
2845

2946
/**
30-
* 总时间
47+
* shape的宽度
3148
*/
32-
// abstract var time: Long
49+
var width = 5f
50+
51+
var shapeAlpha = 100
52+
53+
var paint = Paint().apply {
54+
strokeWidth = width
55+
isAntiAlias = true
56+
alpha = alpha
57+
}
58+
59+
// 总共下落的时间
60+
var lastTime = 0L
61+
// 原始x坐标位置
62+
var originX = 0f
3363

3464
/**
3565
* 根据自己的规则计算加速度,如果是匀速直接 return 0
3666
*/
3767
abstract fun getAcceleration(): Float
3868

3969
/**
40-
* 绘制自身,具体实现交给子类去实现(可以drawLine,drawBitmap等)
70+
* 绘制自身,这里在Shape是非使用的时候进行一些初始化操作
4171
*/
42-
abstract fun draw(canvas: Canvas)
72+
open fun draw(canvas: Canvas) {
73+
if (!isInUse) {
74+
lastTime += randomPre()
75+
initStyle()
76+
isInUse = true
77+
} else {
78+
drawWhenInUse(canvas)
79+
}
80+
}
4381

4482
/**
45-
* 随机的提前量,让Shape错开
83+
* Shape在使用的时候调用此方法
4684
*/
47-
abstract fun randomPre(): Long
85+
abstract fun drawWhenInUse(canvas: Canvas)
4886

4987
/**
5088
* 初始化Shape风格
5189
*/
52-
abstract fun initStyle()
53-
}
90+
open fun initStyle() {
91+
val random = Random()
92+
// 获取随机透明度
93+
shapeAlpha = random.nextInt(155) + 50
94+
// 获得起点x偏移
95+
val translateX = random.nextInt(10).toFloat() + 5
96+
if (!isRandom) {
97+
start.x = translateX + originX
98+
end.x = translateX + originX
99+
} else {
100+
// 如果是随机Shape,将x坐标随机范围扩大到整个屏幕的宽度
101+
val randomWidth = random.nextInt(context.getScreenWidth())
102+
start.x = randomWidth.toFloat()
103+
end.x = randomWidth.toFloat()
104+
}
105+
speed = randomSpeed(random)
106+
// 初始化length的工作留给之后对应的子类去实现
107+
// 初始化color也留给子类去实现
108+
paint.apply {
109+
alpha = shapeAlpha
110+
strokeWidth = width
111+
isAntiAlias = true
112+
}
113+
// 如果有什么想要做的,刚好可以在追加上完成,就使用这个函数
114+
wtc()
115+
}
116+
117+
/**
118+
* 空实现的函数,将会在initStyle中调用。如果现有的
119+
* initStyle函数能满足你的需求,但是你还需要追加一些
120+
* 东西,你可以通过复写此函数实现
121+
* empty body, this will be invoke in initStyle
122+
* method.If current initStyle method can // 满足不会。。。
123+
* but you still add something, by override this method
124+
* can be a good idea.
125+
*/
126+
open fun wtc(): Unit {
127+
128+
}
129+
130+
abstract fun randomSpeed(random: Random): Float
131+
132+
/**
133+
* 获取一个随机的提前量,让shape在竖屏上有一个初始的偏移
134+
*/
135+
open fun randomPre(): Long {
136+
val random = Random()
137+
val pre = random.nextInt(1000).toLong()
138+
return pre
139+
}
140+
}
141+
// TODO 待验证重构之后的代码的正确性

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ import java.lang.Exception
1616
* author:luo
1717
1818
*/
19-
class WeatherView(context: Context, attributeSet: AttributeSet? = null, defaultStyle: Int = 0) :
19+
class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: Int) :
2020
SurfaceView(context, attributeSet, defaultStyle), SurfaceHolder.Callback {
2121
private val TAG = "WeatherView"
2222

23+
constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
24+
25+
constructor(context: Context) : this(context, null, 0)
26+
2327
// 低级并发,Kotlin中支持的不是很好,所以用一下黑科技
2428
val lock = Object()
2529
var type = Weather.RAIN
@@ -85,6 +89,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet? = null, defaultS
8589
}
8690

8791
init {
92+
LogUtil.i(TAG, "init开始")
8893
holder.addCallback(this)
8994
holder.setFormat(PixelFormat.RGBA_8888)
9095
// initData()

0 commit comments

Comments
 (0)