網(wǎng)站首頁 編程語言 正文
背景
原生的TextView是支持跑馬燈效果的,但是在項目中實際用了之后,達(dá)不到需求,原因是內(nèi)容滾動太慢,速度無法調(diào)節(jié)。因此,需要自定義一個可以調(diào)節(jié)速度的跑馬燈。
思路
目前實現(xiàn)的思路是對文本內(nèi)容不斷地重繪,同時改變每次重繪的坐標(biāo),來在視覺上達(dá)到內(nèi)容在滾動的效果。缺點是如果每次改變的坐標(biāo)差值太大,會有明顯的卡頓效果。經(jīng)過調(diào)試,下面源碼中的速度感覺還可以接受,如果有特殊需求,自行在調(diào)試一下。
源碼(Kotlin)
class CustomMarqueeView : AppCompatTextView {
? ? companion object {
? ? ? ? val SPEED_FAST = 9
? ? ? ? val SPEED_MEDIUM = 6
? ? ? ? val SPEED_SLOW = 3
? ? }
? ? //View寬度
? ? private var mViewWidth = 0
? ? private var mViewHeight = 0
? ? private var mScrollX = 0F
? ? private var mMarqueeMode = 3
? ? private val rect = Rect()
? ? constructor(context: Context) : this(context, null)
? ? constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
? ? constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
? ? ? ? context,
? ? ? ? attrs,
? ? ? ? defStyleAttr
? ? ) {
? ? ? ? includeFontPadding = false
? ? ? ? initAttrs(context, attrs)
? ? }
? ? fun setScrollSpeed(speed: Int) {
? ? ? ? if (speed == SPEED_FAST || speed == SPEED_MEDIUM || speed == SPEED_SLOW) {
? ? ? ? ? ? mMarqueeMode = speed
? ? ? ? }
? ? }
? ? override fun onDraw(canvas: Canvas?) {
? ? ? ? val textContentText = text.toString().trim()
? ? ? ? if (TextUtils.isEmpty(textContentText)) {
? ? ? ? ? ? return
? ? ? ? }
? ? ? ? val x = mViewWidth - mScrollX
? ? ? ? val y = mViewHeight / 2F + getTextContentHeight() / 2
? ? ? ? canvas?.drawText(textContentText, x, y, paint)
? ? ? ? mScrollX += mMarqueeMode
? ? ? ? if (mScrollX >= (mViewWidth + getTextContentWdith())) {
? ? ? ? ? ? mScrollX = 0F
? ? ? ? }
? ? ? ? invalidate()
? ? }
? ? override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec)
? ? ? ? mViewWidth = MeasureSpec.getSize(widthMeasureSpec)
? ? ? ? mViewHeight = MeasureSpec.getSize(heightMeasureSpec)
? ? }
? ??
? ? override fun setTextColor(color: Int) {
? ? ? ? super.setTextColor(color)
? ? ? ? paint.setColor(color)
? ? }
? ? private fun initAttrs(context: Context, attrs: AttributeSet?) {
? ? ? ? val typeArray = context.obtainStyledAttributes(attrs, R.styleable.CustomMarqueeView)
? ? ? ? mMarqueeMode =
? ? ? ? ? ? typeArray.getInt(R.styleable.CustomMarqueeView_customScrollSpeed, mMarqueeMode)
? ? ? ? typeArray.recycle()
? ? }
? ? /**
? ? ?* 測量文字寬度
? ? ?* @return 文字寬度
? ? ?*/
? ? private fun getTextContentWdith(): Int {
? ? ? ? val textContent = text.toString().trim()
? ? ? ? if (!TextUtils.isEmpty(textContent)) {
? ? ? ? ? ? paint.getTextBounds(textContent, 0, textContent.length, rect)
? ? ? ? ? ? return rect.width()
? ? ? ? }
? ? ? ? return 0
? ? }
? ? /**
? ? ?* 測量文字高度
? ? ?* @return 文字高度
? ? ?*/
? ? private fun getTextContentHeight(): Int {
? ? ? ? val textContent = text.toString().trim()
? ? ? ? if (!TextUtils.isEmpty(textContent)) {
? ? ? ? ? ? paint.getTextBounds(textContent, 0, textContent.length, rect)
? ? ? ? ? ? return rect.height()
? ? ? ? }
? ? ? ? return 0
? ? }
}
自定義屬性
<declare-styleable name="CustomMarqueeView"> ? <attr name="customScrollSpeed"> ? ? <enum name="fast" value="9" /> ? ? <enum name="medium" value="6" /> ? ? <enum name="slow" value="3" /> ? </attr> </declare-styleable>
原文鏈接:https://blog.csdn.net/Common_it/article/details/104280994
相關(guān)推薦
- 2022-08-10 Qt利用DOM類實現(xiàn)讀取xml文件_C 語言
- 2022-02-18 微信小程序----------父組件調(diào)用子組件的方法
- 2023-07-04 JUC阻塞隊列BlockingQueue---LinkedBlockingQueue
- 2024-02-17 序列轉(zhuǎn)序模型及其pytorch實現(xiàn)
- 2022-08-30 MongoDB數(shù)據(jù)庫權(quán)限管理詳解_MongoDB
- 2021-12-01 C語言中單目操作符++、–的實例講解_C 語言
- 2022-09-01 C語言中static與sizeof查缺補(bǔ)漏篇_C 語言
- 2023-04-21 C語言哈希表概念超詳細(xì)講解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支