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.

73 lines
2.0 KiB
Kotlin

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()
mTipChangeListener?.invoke()
}
// 上一次展示的时间
private var lastTipUpdateTime: Long = 0
// 文字生成的bitmap
private var bmp: Bitmap? = null
private var yuv : ByteArray?= null
// 时间格式化字符串
private val dateFormat = SimpleDateFormat("yy-MM-dd HH:mm:ss")
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
// // 回收内存
// bmp?.recycle()
// 文字转bitmap
bmp = YUVUtils.generateBitmap(
dateFormat.format(lastTipUpdateTime) + "@" + mToDoShowTip, 40, Color.WHITE
)
// 缩放旋转bitmap
// bmp = YUVUtils.rotateImage(bmp, 0);
yuv = YUVUtils.getYUVByBitmap(bmp)
}
}
fun getOverlayBitmap(): Bitmap? {
return bmp;
}
fun getOverlayYuv(): ByteArray? {
return yuv;
}
fun setTipChangeListener(onChange: () -> Unit) {
mTipChangeListener = onChange
}
}