日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android自定義可控制速度的跑馬燈_Android

作者:Android師哥 ? 更新時間: 2022-08-20 編程語言

背景

原生的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

欄目分類
最近更新