You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB
Kotlin

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package org.easydarwin
import android.graphics.Bitmap
import android.graphics.Color
import android.text.TextUtils
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 = ""
// 外部调用,设置待显示水印文字
fun setShowTip(string: String) {
mToDoShowTip = string
buildOverlayBitmap()
}
// 上一次展示的时间
private var lastTipUpdateTime: Long = 0
private const val bmpWidth: Int = 550
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() {
if (TextUtils.isEmpty(mToDoShowTip)) {
return
}
val currentTimeMillis = System.currentTimeMillis()
// 限制获取bitmap的频率保证性能
if (TextUtils.isEmpty(mLastShowTip) || mToDoShowTip != mLastShowTip || currentTimeMillis - lastTipUpdateTime > 1000) {
// 记录更新时间和上一次的文字
lastTipUpdateTime = currentTimeMillis
mLastShowTip = mToDoShowTip
// 文字转bitmap
YUVUtils.generateBitmap(
bmp,
dateFormat.format(lastTipUpdateTime) + "@" + 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
}
}