可自动消失的随机文字(二)
标签:
JavaScript
思路
这次采用的不是PopupWindow方式实现的,而是自定义View的方式实现,整体相对于一版本有了一定的优化。
实现
实现相对于第一种更为简洁,逻辑也相对更简单一些
代码
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Message
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.View/**
* @author:JinXuDong
* @date:2018/8/9
*/class NameView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
private val mNames: MutableList<NameText> = mutableListOf()
private var mKeepALiveTime: Long = 3000L
private val mPaint: Paint by lazy {
Paint(Paint.ANTI_ALIAS_FLAG)
}
init {
mPaint.color = Color.RED
mPaint.style = Paint.Style.STROKE
mPaint.textSize = 60f
mPaint.strokeWidth = 5f
}
fun setColor(@ColorInt color: Int) {
mPaint.color = color
}
fun setTextSize(textSize: Float) {
mPaint.textSize = textSize
}
fun setStrokeWidth(width: Float) {
mPaint.strokeWidth = width
}
fun setKeepTime(time: Long) {
mKeepALiveTime = time
}
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas?) {
mNames.forEach {
val showTime = System.currentTimeMillis() - it.time - (mKeepALiveTime * 2 / 3)
mPaint.alpha = if (showTime > 0) {
val alOffset = mKeepALiveTime / 3 - showTime if (alOffset > 0) {
(255 * alOffset / (mKeepALiveTime / 3)).toInt()
} else { 0
}
} else { 255
}
canvas?.drawText(it.text, it.x, it.y, mPaint)
}
}
fun drawName(nt: NameText) {
val temp = mNames.filter {
it.text == nt.text
} if (temp.isEmpty()) {
mNames.add(nt)
} else {
temp.forEach {
it.time = System.currentTimeMillis()
}
}
mHandler.sendEmptyMessage(0xA)
}
private val mHandler: Handler = @SuppressLint("HandlerLeak")
object : Handler() {
override fun handleMessage(msg: Message?) {
when (msg?.what) { 0xA -> {
mNames.removeAll(mNames.filter { System.currentTimeMillis() - it.time > mKeepALiveTime })
invalidate()
removeMessages(0xA)
sendEmptyMessageDelayed(0xA, 10)
}
}
}
}
data class NameText(var text: String, var time: Long = System.currentTimeMillis(), var x: Float, var y: Float)
}解释
实现很简单,就是自定义View并画文字,同时设计一个Bean类装载文字和位置,显示时间等基本属性,可以根据自己需求再拓展。
因为目的是显示3s然后自动消失,如果持续存在同一人,则会延长显示时间。
所以使用了这个方式,循环的画View。
为了消失的不那么突兀,显示时间的最后 1/3 慢慢消失,直至消失位置。
使用方法
xml布局文件
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondActivity"> <com.syxrobot.cameratest.NameView android:id="@+id/mNv" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/mBtnA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AAAAAA" app:layout_constraintBottom_toBottomOf="parent" /> <Button android:id="@+id/mBtnB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="BBBBBB" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/mBtnC" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CCCCCC" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" /></android.support.constraint.ConstraintLayout>
Activity调用代码
import android.graphics.Colorimport android.os.Bundleimport android.support.v7.app.AppCompatActivityimport kotlinx.android.synthetic.main.activity_second.*class SecondActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
mNv.setColor(Color.parseColor("#ffffb600"))
mNv.setKeepTime(4000)
mBtnA.setOnClickListener {
mNv.drawName(NameView.NameText(text = "AAAAAA", time = System.currentTimeMillis(), x = 100f, y = 100f))
}
mBtnB.setOnClickListener {
mNv.drawName(NameView.NameText(text = "BBBBBB", x = 200f, y = 200f))
}
mBtnC.setOnClickListener {
mNv.drawName(NameView.NameText(text = "CCCCCC", x = 300f, y = 300f))
}
}
}
作者:Alfredjin
链接:https://www.jianshu.com/p/9f95a698dd9e
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦