|
|
package org.easydarwin
|
|
|
|
|
|
import android.graphics.Bitmap
|
|
|
import android.graphics.Color
|
|
|
import android.text.TextUtils
|
|
|
import com.common.commonlib.db.entity.Gas
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.launch
|
|
|
import org.easydarwin.util.YUVUtils
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
|
|
/**
|
|
|
* Created by John on 2017/2/23.
|
|
|
*/
|
|
|
object TxtOverlay {
|
|
|
// 提示变化的监听
|
|
|
private var mTipChangeListener: (() -> Unit)? = null;
|
|
|
|
|
|
// 上一次展示的提示文字
|
|
|
private var mLastShowTip = ""
|
|
|
|
|
|
// 待展示的提示文字
|
|
|
private var mToDoShowTip = ""
|
|
|
|
|
|
// 上一次展示的时间
|
|
|
private var lastTipUpdateTime: Long = 0
|
|
|
|
|
|
private const val bmpWidth: Int = 600
|
|
|
private const val bmpHeight: Int = 330
|
|
|
|
|
|
// 文字生成的bitmap
|
|
|
private var bmp: Bitmap? = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
|
|
|
|
|
|
private var yuv: ByteArray? = null
|
|
|
|
|
|
// 时间格式化字符串
|
|
|
private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
|
|
|
|
|
|
private fun buildOverlayBitmap(currentTimeMillis: Long) {
|
|
|
if (TextUtils.isEmpty(mToDoShowTip)) {
|
|
|
return
|
|
|
}
|
|
|
// 限制获取bitmap的频率,保证性能
|
|
|
if (TextUtils.isEmpty(mLastShowTip) || mToDoShowTip != mLastShowTip || currentTimeMillis - lastTipUpdateTime > 1000) {
|
|
|
// 记录更新时间和上一次的文字
|
|
|
lastTipUpdateTime = currentTimeMillis
|
|
|
mLastShowTip = mToDoShowTip
|
|
|
val timeFormat = dateFormat.format(lastTipUpdateTime)
|
|
|
// 文字转bitmap
|
|
|
YUVUtils.generateBitmap(
|
|
|
bmp,
|
|
|
"$timeFormat@$mToDoShowTip",
|
|
|
42,
|
|
|
Color.WHITE
|
|
|
)
|
|
|
yuv = YUVUtils.getYUVByBitmap(bmp)
|
|
|
mTipChangeListener?.invoke()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fun getOverlayBitmap(): Bitmap? {
|
|
|
if (!TextUtils.isEmpty(mToDoShowTip)) {
|
|
|
return bmp
|
|
|
}
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
fun getOverlayYuv(): ByteArray? {
|
|
|
if (!TextUtils.isEmpty(mToDoShowTip)) {
|
|
|
return yuv
|
|
|
}
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
fun setTipChangeListener(onChange: () -> Unit) {
|
|
|
mTipChangeListener = onChange
|
|
|
}
|
|
|
|
|
|
private val overlayBuilder: StringBuilder = java.lang.StringBuilder()
|
|
|
private var looping = false
|
|
|
|
|
|
// 外部调用,设置待显示水印文字
|
|
|
fun startShow(gasMap: HashMap<String, Gas>) {
|
|
|
if (!looping) {
|
|
|
looping = true
|
|
|
GlobalScope.launch(Dispatchers.IO) {
|
|
|
while (true) {
|
|
|
val currentTimeMillis = System.currentTimeMillis()
|
|
|
overlayBuilder.clear()
|
|
|
gasMap.forEach { item ->
|
|
|
val gas = item.value
|
|
|
val time = gas.time
|
|
|
// 3S内的数据我们认为有效
|
|
|
if (currentTimeMillis - time <= 3000) {
|
|
|
overlayBuilder.append("${gas.gasName}: ${gas.gasValue} ${gas.unit}")
|
|
|
.append("@")
|
|
|
}
|
|
|
}
|
|
|
mToDoShowTip = overlayBuilder.toString()
|
|
|
buildOverlayBitmap(currentTimeMillis)
|
|
|
delay(1000)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |