-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSurfaceView.kt
144 lines (126 loc) · 3.86 KB
/
CustomSurfaceView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xuexiang.uithemesample.widget
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.text.TextPaint
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import com.xuexiang.xui.utils.DensityUtils
import java.util.concurrent.atomic.AtomicInteger
/**
* 自定义SurfaceView
*
* @author xuexiang
* @since 2023/2/28 01:34
*/
class CustomSurfaceView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : SurfaceView(context, attrs), SurfaceHolder.Callback {
private var mSurfaceHolder: SurfaceHolder = holder
private lateinit var mRenderThread: RenderThread
private var mIsDrawing = false
private var mBgColor: Int = Color.RED
private val mIndex = AtomicInteger(0)
private val mTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
private var mColors =
intArrayOf(Color.RED, Color.BLACK, Color.BLUE, Color.DKGRAY, Color.GREEN, Color.YELLOW)
init {
mSurfaceHolder.addCallback(this)
mTextPaint.run {
color = Color.WHITE
textSize = DensityUtils.sp2px(context, 24F).toFloat()
textAlign = Paint.Align.CENTER
}
mBgColor = refreshColor()
}
fun start() {
if (mIsDrawing) {
return
}
mIsDrawing = true
mRenderThread = RenderThread()
mRenderThread.start()
}
fun stop() {
if (!mIsDrawing) {
return
}
mIsDrawing = false
mRenderThread.interrupt()
}
//==================绘制========================//
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}
override fun surfaceCreated(holder: SurfaceHolder) {
start()
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
stop()
}
/**
* 绘制界面的线程
*/
private inner class RenderThread : Thread() {
override fun run() {
// 不停绘制界面
while (mIsDrawing) {
drawUI()
try {
sleep(1000)
} catch (_: InterruptedException) {
}
}
}
}
/**
* 界面绘制
*/
private fun drawUI() {
val canvas = mSurfaceHolder.lockCanvas()
try {
drawCanvas(canvas)
} catch (e: Exception) {
e.printStackTrace()
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas)
}
}
private fun drawCanvas(canvas: Canvas?) {
// 在 canvas 上绘制需要的图形
canvas?.run {
drawColor(mBgColor)
drawText(mIndex.toString(), mTextPaint)
mBgColor = refreshColor()
}
}
private fun Canvas.drawText(text: String, textPaint: Paint) {
val textRect = Rect()
textPaint.getTextBounds(text, 0, text.length, textRect)
drawText(
text,
width / 2F,
height / 2F - (textRect.top + textRect.bottom) / 2F,
textPaint
)
}
private fun refreshColor() = mColors[mIndex.getAndIncrement() % mColors.size]
}