Android开发 自定义View_利用canvas的clipRect实现文字颜色滚动效果
2022/6/2 23:24:42
本文主要是介绍Android开发 自定义View_利用canvas的clipRect实现文字颜色滚动效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前言
实现原理很简单,就是绘制2层不同颜色的文本,然后将其中一个的画布裁剪到合适的大小在向一个方向移动起来。
效果图
代码
import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.graphics.Rect import android.util.AttributeSet import android.util.TypedValue import android.view.View class ScrollGradientText(context: Context, attrs: AttributeSet?) : View(context, attrs) { private var mTextSize = 24f private var mBottomTextColor = Color.WHITE private var mTopTextColor = Color.parseColor("#48F3D0") private var mText = "hello world" private var mBottomPaint = Paint() private var mTopPaint = Paint() private var mWidth = 0 private var mHeight = 0 private var mStepCount = 0 private var mSpeed = 150 private var mLeft = 0 private var mRight = 0 init { updatePaint() } /** * 文本内容 */ fun setText(text: String) { mText = text invalidate() } /** * 文字颜色 */ fun setTextColor(topTextColor: Int, bottomTextColor: Int) { mTopTextColor = topTextColor mBottomTextColor = bottomTextColor updatePaint() invalidate() } /** * 设置文字大小 */ fun setTextSize(textSize: Float) { mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, resources.displayMetrics) updatePaint() invalidate() } /** * 设置速度 */ fun setSpeed(speed: Int) { mSpeed = speed } /** * 测量尺寸 */ override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) mWidth = mBottomPaint.measureText(mText).toInt() mHeight = MeasureSpec.getSize(heightMeasureSpec) mLeft = -mWidth / 3 mRight = 0 setMeasuredDimension(mWidth, mHeight) } private fun updatePaint() { mBottomPaint = Paint().apply { color = mBottomTextColor textSize = mTextSize textAlign = Paint.Align.CENTER isAntiAlias = true } mTopPaint = Paint().apply { color = mTopTextColor textSize = mTextSize textAlign = Paint.Align.CENTER isAntiAlias = true } } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) drawBottomText(canvas) drawTopText(canvas) } private fun drawBottomText(canvas: Canvas) { canvas.save() val offset = textCenteredOffset(mBottomPaint) canvas.drawText(mText, (width / 2).toFloat(), (height / 2).toFloat() - offset, mBottomPaint) canvas.restore() } private fun drawTopText(canvas: Canvas) { canvas.save() if (mLeft > width) { mLeft = -mWidth / 3 mRight = 0 mStepCount = 0 } mStepCount += 5 mLeft += mStepCount mRight += mStepCount canvas.clipRect(mLeft, 0, mRight, mHeight) val offSet = textCenteredOffset(mTopPaint) canvas.drawText(mText, (width / 2).toFloat(), (height / 2).toFloat() - offSet, mTopPaint) canvas.restore() postInvalidateDelayed(150) } /** * 文字居中偏移量 */ private fun textCenteredOffset(paint: Paint): Int { val bounds = Rect() paint.getTextBounds(mText, 0, mText.length, bounds) return (bounds.top + bounds.bottom) / 2 } }
End
这篇关于Android开发 自定义View_利用canvas的clipRect实现文字颜色滚动效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升