Android自定義可控制速度的跑馬燈
背景
原生的TextView是支持跑馬燈效果的,但是在項目中實(shí)際用了之后,達(dá)不到需求,原因是內(nèi)容滾動太慢,速度無法調(diào)節(jié)。因此,需要自定義一個可以調(diào)節(jié)速度的跑馬燈。
思路
目前實(shí)現(xiàn)的思路是對文本內(nèi)容不斷地重繪,同時改變每次重繪的坐標(biāo),來在視覺上達(dá)到內(nèi)容在滾動的效果。缺點(diǎn)是如果每次改變的坐標(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于OpenGL的GLSurfaceView創(chuàng)建一個Activity實(shí)現(xiàn)方法
這篇文章主要介紹了Android基于OpenGL的GLSurfaceView創(chuàng)建一個Activity實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android中OpenGL圖形操作類GLSurfaceView的功能、用法及相關(guān)使用技巧,需要的朋友可以參考下2016-10-10
利用Android畫圓弧canvas.drawArc()實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android動態(tài)人臉檢測的示例代碼(臉數(shù)可調(diào))
本篇文章主要介紹了Android動態(tài)人臉檢測的示例代碼(臉數(shù)可調(diào)),具有一定的參考價值,有興趣的可以了解一下2017-08-08
深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動態(tài))詳解
這篇文章主要介紹了深入Android中BroadcastReceiver的兩種注冊方式(靜態(tài)和動態(tài))詳解,具有一定的參考價值,有需要的可以了解一下。2016-12-12
android與asp.net服務(wù)端共享session的方法詳解
這篇文章主要給大家介紹了關(guān)于android與asp.net服務(wù)端如何共享session的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09

