@@ -5,9 +5,11 @@ import android.graphics.Canvas
5
5
import android.graphics.Color
6
6
import android.graphics.PixelFormat
7
7
import android.graphics.PorterDuff
8
+ import android.graphics.drawable.GradientDrawable
8
9
import android.util.AttributeSet
9
10
import android.view.SurfaceHolder
10
11
import android.view.SurfaceView
12
+ import com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView.SkyBackground.RAIN_D
11
13
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
12
14
import java.lang.Exception
13
15
@@ -19,6 +21,7 @@ import java.lang.Exception
19
21
class WeatherView (context : Context , attributeSet : AttributeSet ? , defaultStyle : Int ) :
20
22
SurfaceView (context, attributeSet, defaultStyle), SurfaceHolder .Callback {
21
23
private val TAG = " WeatherView"
24
+ private val skyBackgroud = GradientDrawable (GradientDrawable .Orientation .TOP_BOTTOM , RAIN_D )
22
25
23
26
constructor (context: Context , attributeSet: AttributeSet ? ) : this (context, attributeSet, 0 )
24
27
@@ -49,13 +52,20 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
49
52
val canvas = holder.lockCanvas()
50
53
if (canvas != null ) {
51
54
canvas.drawColor(Color .TRANSPARENT , PorterDuff .Mode .CLEAR )
55
+ // 这种方式绘制比较卡,换一种
56
+ // canvas.drawColor(context.resources.getColor(R.color.sky_blue), PorterDuff.Mode.DST_OVER)
57
+ // 感谢 mixiaoxiao 大神的实现思路
58
+ skyBackgroud.setBounds(0 , 0 , measuredWidth, measuredHeight)
59
+ skyBackgroud.draw(canvas)
52
60
draw(canvas, type)
53
61
}
54
62
holder.unlockCanvasAndPost(canvas)
55
63
val drawTime = System .currentTimeMillis() - startTime
56
64
// 平均16ms一帧才能有顺畅的感觉
57
65
if (drawTime < 16 ) {
58
66
Thread .sleep(16 - drawTime)
67
+ } else {
68
+ LogUtil
59
69
}
60
70
} catch (e: Exception ) {
61
71
// e.printStackTrace()
@@ -93,7 +103,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
93
103
holder.addCallback(this )
94
104
holder.setFormat(PixelFormat .TRANSLUCENT )
95
105
// initData()
96
- setZOrderOnTop(true )
106
+ // setZOrderOnTop(true)
97
107
// setZOrderMediaOverlay(true)
98
108
thread.start()
99
109
}
@@ -124,4 +134,36 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
124
134
} catch (e: Exception ) {
125
135
}
126
136
}
137
+
138
+ object SkyBackground {
139
+ val BLACK = intArrayOf(0xff000000 .toInt(), 0xff000000 .toInt())
140
+ // public static final int[] CLEAR_D = new int[] { 0xff3d99c2, 0xff4f9ec5 };
141
+ // public static final int[] CLEAR_N = new int[] { 0xff0d1229, 0xff262c42 };
142
+
143
+ val CLEAR_D = intArrayOf(0xff3d99c2 .toInt(), 0xff4f9ec5 .toInt())
144
+ val CLEAR_N = intArrayOf(0xff0b0f25 .toInt(), 0xff252b42 .toInt())
145
+ // ////////////
146
+ val OVERCAST_D = intArrayOf(0xff33425f .toInt(), 0xff617688 .toInt())// 0xff748798, 0xff617688
147
+ val OVERCAST_N = intArrayOf(0xff262921 .toInt(), 0xff23293e .toInt())// 0xff1b2229, 0xff262921
148
+ // ////////////
149
+ val RAIN_D = intArrayOf(0xff4f80a0 .toInt(), 0xff4d748e .toInt())
150
+ val RAIN_N = intArrayOf(0xff0d0d15 .toInt(), 0xff22242f .toInt())
151
+ // ////////////
152
+ val FOG_D = intArrayOf(0xff688597 .toInt(), 0xff44515b .toInt())
153
+ val FOG_N = intArrayOf(0xff2f3c47 .toInt(), 0xff24313b .toInt())
154
+
155
+ // ////////////
156
+ val SNOW_D = intArrayOf(0xff4f80a0 .toInt(), 0xff4d748e .toInt())// 临时用RAIN_D凑数的
157
+ val SNOW_N = intArrayOf(0xff1e2029 .toInt(), 0xff212630 .toInt())
158
+ // ////////////
159
+ val CLOUDY_D = intArrayOf(0xff4f80a0 .toInt(), 0xff4d748e .toInt())// 临时用RAIN_D凑数的
160
+ val CLOUDY_N = intArrayOf(0xff071527 .toInt(), 0xff252b42 .toInt())// 0xff193353 };//{ 0xff0e1623, 0xff222830 }
161
+ // ////////////
162
+ val HAZE_D = intArrayOf(0xff616e70 .toInt(), 0xff474644 .toInt())// 0xff999b95, 0xff818e90
163
+ val HAZE_N = intArrayOf(0xff373634 .toInt(), 0xff25221d .toInt())
164
+
165
+ // ////////////
166
+ val SAND_D = intArrayOf(0xffb5a066 .toInt(), 0xffd5c086 .toInt())// 0xffa59056
167
+ val SAND_N = intArrayOf(0xff312820 .toInt(), 0xff514840 .toInt())
168
+ }
127
169
}
0 commit comments