網站首頁 編程語言 正文
背景
原生的TextView是支持跑馬燈效果的,但是在項目中實際用了之后,達不到需求,原因是內容滾動太慢,速度無法調節。因此,需要自定義一個可以調節速度的跑馬燈。
思路
目前實現的思路是對文本內容不斷地重繪,同時改變每次重繪的坐標,來在視覺上達到內容在滾動的效果。缺點是如果每次改變的坐標差值太大,會有明顯的卡頓效果。經過調試,下面源碼中的速度感覺還可以接受,如果有特殊需求,自行在調試一下。
源碼(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
相關推薦
- 2023-01-07 Android?RecyclerBarChart繪制使用教程_Android
- 2022-12-06 詳解從ObjectPool到CAS指令_C#教程
- 2022-09-07 pytest配置文件pytest.ini的具體使用_python
- 2023-05-24 Android打空包后提示沒有"android:exported"的屬性設置問題解決_Android
- 2023-05-29 tf.nn.conv2d與tf.layers.conv2d的區別及說明_python
- 2022-06-17 C#中Abstract方法和Virtual方法的區別_C#教程
- 2022-05-23 Python+OpenCV實現在圖像上繪制矩形_python
- 2022-06-26 asp.net使用WebAPI和EF框架結合實現數據的基本操作_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支