1
1
package com.xiasuhuei321.gank_kotlin.customview.weather
2
2
3
3
import android.content.Context
4
- import android.graphics.*
4
+ import android.graphics.Canvas
5
+ import android.graphics.Color
6
+ import android.graphics.PixelFormat
7
+ import android.graphics.PorterDuff
5
8
import android.util.AttributeSet
6
9
import android.view.SurfaceHolder
7
10
import android.view.SurfaceView
8
11
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
9
- import com.xiasuhuei321.gank_kotlin.extension.getScreenWidth
10
12
import java.lang.Exception
11
13
12
14
/* *
13
15
* Created by xiasuhuei321 on 2017/9/5.
14
16
* author:luo
15
17
16
18
*/
17
- class WeatherView (context : Context , attributeSet : AttributeSet ? , defaultStyle : Int ) :
19
+ class WeatherView (context : Context , attributeSet : AttributeSet ? = null , defaultStyle : Int = 0 ) :
18
20
SurfaceView (context, attributeSet, defaultStyle), SurfaceHolder .Callback {
19
21
private val TAG = " WeatherView"
20
22
21
- var time = 0L
22
- val rainWidth = 5
23
23
// 低级并发,Kotlin中支持的不是很好,所以用一下黑科技
24
24
val lock = Object ()
25
25
var type = Weather .RAIN
@@ -28,16 +28,6 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
28
28
@Volatile var canRun = false
29
29
@Volatile var threadQuit = false
30
30
31
- val rains = ArrayList <Rain >()
32
-
33
- constructor (context: Context , attributeSet: AttributeSet ) : this (context, attributeSet, 0 ) {
34
- LogUtil .e(TAG , " 构建方法2" )
35
- }
36
-
37
- constructor (context: Context ) : this (context, null , 0 ) {
38
- LogUtil .e(TAG , " 构建方法1" )
39
- }
40
-
41
31
var thread = Thread {
42
32
while (! threadQuit) {
43
33
if (! canRun) {
@@ -71,6 +61,8 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
71
61
72
62
override fun surfaceChanged (holder : SurfaceHolder ? , format : Int , width : Int , height : Int ) {
73
63
// surface发生了变化
64
+ // canRun = true
65
+
74
66
}
75
67
76
68
override fun surfaceDestroyed (holder : SurfaceHolder ? ) {
@@ -101,21 +93,6 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
101
93
thread.start()
102
94
}
103
95
104
- private fun initData () {
105
- val space = context.getScreenWidth() / 20
106
- var i = 0
107
- var currentSpace = 0f
108
- // 将其均匀的分布在屏幕x方向上
109
- while (i < 20 ) {
110
- val rain = Rain (PointF (currentSpace, 0f ), PointF (currentSpace, 0f ))
111
- rain.originLength = 20f
112
- rain.originX = currentSpace
113
- rains.add(rain)
114
- currentSpace + = space
115
- i++
116
- }
117
- }
118
-
119
96
private fun draw (canvas : Canvas , type : Weather , startTime : Long ) {
120
97
// type什么的先放一边,先实现一个
121
98
weatherShapePool.drawRain(canvas)
@@ -128,5 +105,12 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
128
105
129
106
fun onDestroy () {
130
107
threadQuit = true
108
+ canRun = true
109
+ try {
110
+ synchronized(lock) {
111
+ lock.notify()
112
+ }
113
+ } catch (e: Exception ) {
114
+ }
131
115
}
132
116
}
0 commit comments