Skip to content

Commit 72a3841

Browse files
committed
refactor:optimize weather code framework
1 parent 585aea7 commit 72a3841

File tree

7 files changed

+76
-51
lines changed

7 files changed

+76
-51
lines changed

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'android-apt'
33
apply plugin: 'kotlin-android'
4+
apply plugin: 'kotlin-android-extensions'
45

56
android {
67
compileSdkVersion 25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package com.xiasuhuei321.gank_kotlin
22

3-
import android.graphics.Bitmap
43
import android.os.Bundle
54
import android.support.v7.app.AppCompatActivity
65
import android.view.Window
76
import android.view.WindowManager
8-
import android.widget.RelativeLayout
9-
import com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
10-
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
11-
import com.xiasuhuei321.gank_kotlin.extension.find
7+
import com.xiasuhuei321.gank_kotlin.extension.shortToast
8+
import kotlinx.android.synthetic.main.activity_main.*
129

1310
class MainActivity : AppCompatActivity() {
14-
var bmp: Bitmap? = null
1511
override fun onCreate(savedInstanceState: Bundle?) {
1612
super.onCreate(savedInstanceState)
1713
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
1814
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
1915
WindowManager.LayoutParams.FLAG_FULLSCREEN)
2016
setContentView(R.layout.activity_main)
21-
val test = find<WeatherView>(R.id.wv_test)
22-
val rl_test = find<RelativeLayout>(R.id.rl_test)
23-
rl_test.setOnClickListener {
24-
LogUtil.i("test", "点击事件传到了")
17+
18+
weatherIv.setOnClickListener {
19+
shortToast("weather icon be clicked")
2520
}
2621
}
22+
23+
override fun onDestroy() {
24+
super.onDestroy()
25+
weatherWv.onDestroy()
26+
}
2727
}
2828

2929

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.xiasuhuei321.gank_kotlin.customview.weather
33
import android.graphics.Canvas
44
import android.graphics.Color
55
import android.graphics.Paint
6+
import android.graphics.PointF
67
import com.xiasuhuei321.gank_kotlin.context
78
import com.xiasuhuei321.gank_kotlin.extension.getScreenHeight
89
import com.xiasuhuei321.gank_kotlin.extension.getScreenWidth
@@ -12,8 +13,10 @@ import java.util.*
1213
* Created by xiasuhuei321 on 2017/9/5.
1314
* author:luo
1415
16+
*
17+
* desc:
1518
*/
16-
class Rain(start: android.graphics.PointF, end: android.graphics.PointF) : WeatherShape(start, end) {
19+
class Rain(start: PointF, end: PointF) : WeatherShape(start, end) {
1720

1821
override var TAG = "Rain"
1922

@@ -32,8 +35,8 @@ class Rain(start: android.graphics.PointF, end: android.graphics.PointF) : Weath
3235
// var originSpeed = speed
3336
var rainAlpha = 100
3437
private var lastTime = 0L // 从开始到现在下落所经过的时间
35-
var rainColor = Color.WHITE
36-
override var time: Long = 5000 // 总共下落时间
38+
var rainColor = Color.parseColor("#efefef")
39+
// override var time: Long = 5000 // 总共下落时间
3740

3841
var paint = Paint().apply {
3942
color = rainColor
@@ -44,17 +47,16 @@ class Rain(start: android.graphics.PointF, end: android.graphics.PointF) : Weath
4447

4548
// 计算加速度
4649
override fun getAcceleration(): Float {
47-
val acc = context.getScreenHeight() / (time * time).toFloat()
50+
// val acc = context.getScreenHeight() / (time * time).toFloat()
4851
// LogUtil.i(TAG, "acc = " + acc)
4952
// 恩,决定了,放弃加速,匀速走完
5053
return 0f
5154
}
5255

5356
/**
54-
* 此函数用来绘制相对"固定"的雨滴
57+
* 绘制过程在此完成
5558
*/
5659
override fun draw(canvas: Canvas) {
57-
// 计算公式: s = at^2
5860
if (!isInUse) {
5961
lastTime += randomPre()
6062
initStyle()
@@ -64,8 +66,9 @@ class Rain(start: android.graphics.PointF, end: android.graphics.PointF) : Weath
6466
start.y += distance
6567
end.y += distance
6668
canvas.drawLine(start.x, start.y, end.x, end.y, paint)
69+
// 很重要,持续时间增加
6770
lastTime += timeSpace
68-
// 可以复用了
71+
// 如果已经超出屏幕,表示可以服用了,清空原先状态
6972
if (end.y >= context.getScreenHeight()) {
7073
clear()
7174
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
package com.xiasuhuei321.gank_kotlin.customview.weather
22

3+
import android.graphics.Canvas
4+
import android.graphics.PointF
5+
36
/**
47
* Created by xiasuhuei321 on 2017/9/5.
58
* author:luo
69
7-
*/
10+
*/
11+
class Snow(start: PointF, end: PointF) : WeatherShape(start, end) {
12+
override fun getAcceleration(): Float {
13+
return 0f
14+
}
15+
16+
override fun draw(canvas: Canvas) {
17+
}
18+
19+
override fun randomPre(): Long {
20+
return 1
21+
}
22+
23+
override fun initStyle() {
24+
}
25+
}

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,28 @@ import android.graphics.PointF
1111
abstract class WeatherShape(val start: PointF, val end: PointF) {
1212
open var TAG = "WeatherShape"
1313

14+
/**
15+
* 是否是正在被使用的状态
16+
*/
1417
var isInUse = false
1518

19+
/**
20+
* 是否是随机刷新的Shape
21+
*/
1622
var isRandom = false
1723

24+
/**
25+
* 下落的速度,特指垂直方向,子类可以实现自己水平方向的速度
26+
*/
1827
var speed = 0.05f
1928

20-
abstract var time: Long
29+
/**
30+
* 总时间
31+
*/
32+
// abstract var time: Long
2133

2234
/**
23-
* 根据自己的规则计算加速度
35+
* 根据自己的规则计算加速度,如果是匀速直接 return 0
2436
*/
2537
abstract fun getAcceleration(): Float
2638

@@ -29,7 +41,13 @@ abstract class WeatherShape(val start: PointF, val end: PointF) {
2941
*/
3042
abstract fun draw(canvas: Canvas)
3143

44+
/**
45+
* 随机的提前量,让Shape错开
46+
*/
3247
abstract fun randomPre(): Long
3348

49+
/**
50+
* 初始化Shape风格
51+
*/
3452
abstract fun initStyle()
3553
}

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

+14-30
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package com.xiasuhuei321.gank_kotlin.customview.weather
22

33
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
58
import android.util.AttributeSet
69
import android.view.SurfaceHolder
710
import android.view.SurfaceView
811
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
9-
import com.xiasuhuei321.gank_kotlin.extension.getScreenWidth
1012
import java.lang.Exception
1113

1214
/**
1315
* Created by xiasuhuei321 on 2017/9/5.
1416
* author:luo
1517
1618
*/
17-
class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: Int) :
19+
class WeatherView(context: Context, attributeSet: AttributeSet? = null, defaultStyle: Int = 0) :
1820
SurfaceView(context, attributeSet, defaultStyle), SurfaceHolder.Callback {
1921
private val TAG = "WeatherView"
2022

21-
var time = 0L
22-
val rainWidth = 5
2323
// 低级并发,Kotlin中支持的不是很好,所以用一下黑科技
2424
val lock = Object()
2525
var type = Weather.RAIN
@@ -28,16 +28,6 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
2828
@Volatile var canRun = false
2929
@Volatile var threadQuit = false
3030

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-
4131
var thread = Thread {
4232
while (!threadQuit) {
4333
if (!canRun) {
@@ -71,6 +61,8 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
7161

7262
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
7363
// surface发生了变化
64+
// canRun = true
65+
7466
}
7567

7668
override fun surfaceDestroyed(holder: SurfaceHolder?) {
@@ -101,21 +93,6 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
10193
thread.start()
10294
}
10395

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-
11996
private fun draw(canvas: Canvas, type: Weather, startTime: Long) {
12097
// type什么的先放一边,先实现一个
12198
weatherShapePool.drawRain(canvas)
@@ -128,5 +105,12 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
128105

129106
fun onDestroy() {
130107
threadQuit = true
108+
canRun = true
109+
try {
110+
synchronized(lock) {
111+
lock.notify()
112+
}
113+
} catch (e: Exception) {
114+
}
131115
}
132116
}

app/src/main/res/layout/activity_main.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
<com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
10-
android:id="@+id/wv_test"
10+
android:id="@+id/weatherWv"
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent" />
1313

@@ -26,6 +26,7 @@
2626
android:orientation="vertical">
2727

2828
<ImageView
29+
android:id="@+id/weatherIv"
2930
android:layout_width="80dp"
3031
android:layout_height="80dp"
3132
android:layout_marginTop="100dp"

0 commit comments

Comments
 (0)