欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)循環(huán)輪播跑馬燈的效果

 更新時間:2023年05月04日 10:42:59   作者:Calculus_小王  
這篇文章主要介紹了為大家詳細介紹了如何通過Android實現(xiàn)循環(huán)輪播跑馬燈的效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

先看效果

支持暫停,恢復,view自定義和池化回收復用。使用上,只需要引入xml,并綁定factory即可,內(nèi)部會在attach時自動開始

 <MarqueeAnimalView
        android:id="@+id/marqueeView"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:background="@color/color_yellow" />
val list = mutableListOf("我是跑馬燈1", "我不是跑馬燈", "你猜我是不是跑馬燈")
var position = 0
view.marqueeView.setFactory(object : PoolViewFactory {
    override fun makeView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        val view = TextView(this@ViewActivity)
        view.setPadding(0, 0, 20.dp(), 0)
        view.textSize = 12f
        view.setTextColor(ResourceUtil.getColor(R.color.white))
        return view
    }
    override fun setAnimator(objectAnimator: ObjectAnimator, width: Int, parentWidth: Int) {
        objectAnimator.duration = (parentWidth + width) * 5L
    }
    override fun setView(view: View): Boolean {
        (view as? TextView)?.text = list[position++ % list.size]
        return true
    }
})

池化思路

參考Message的思路,對view進行回收復用,避免內(nèi)存持續(xù)增長,增大GC壓力

private fun obtain(): View? {
    synchronized(sPoolSync) {
        if (!isAttachedToWindow) {
            return null
        }
        if (queue.isNotEmpty()) {
            return queue.poll()
        }
    }
    return factory?.makeView(layoutInflater, this@MarqueeAnimalView)?.apply {
        addView(this, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }
}
private fun recycle(view: View) {
    synchronized(sPoolSync) {
        if (queue.size < MAX_POOL_SIZE) {
            queue.offer(view)
        }
    }
}

創(chuàng)造工廠

這里的思路源于ViewSwitchFactory

interface PoolViewFactory {
    fun makeView(layoutInflater: LayoutInflater, parent: ViewGroup): View
    fun setAnimator(objectAnimator: ObjectAnimator, width: Int, parentWidth: Int)
    /**
     * 返回值,代表view是否需要重新測量
     */
    fun setView(view: View): Boolean
}

輪詢切換

這里根據(jù)對動畫進行初始化,并設(shè)置合適的監(jiān)聽。此時需要獲取當view和parent的width,以用于標定始末位置,需要注意x軸的正負方向。animators用于存儲開始的動畫,這也是設(shè)計時存在的遺留問題,因為主動取消所有動畫,但view->animator是單向綁定關(guān)系,所以需要保存發(fā)生的動畫

private val animators = hashMapOf<String, ObjectAnimator>()
private fun next(view: View?) {
    view ?: return
    if (factory?.setView(view) == true) {
        view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
    }
    val width = view.measuredWidth
    val parentWidth = measuredWidth
    val targetValue = parentWidth - width
    val animator = ObjectAnimator.ofFloat(view, PROPERTY_NAME, parentWidth.toFloat(), -width.toFloat()).apply {
        // null即為默認線性插值器
        interpolator = null
        addUpdateListener(
            RecyclerAnimatorUpdateListener(targetValue) {
                next(obtain())
                removeUpdateListener(it)
            }
        )
        addListener(this@MarqueeAnimalView)
        factory?.setAnimator(this, width, parentWidth)
    }
    animators["${view.hashCode()}-${animator.hashCode()}"] = animator
    animator.start()
}

動畫監(jiān)聽

當動畫結(jié)束時,需要對view進行回收,并對動畫移除。取消動畫時,需要將view強制歸位

同時,為了方便使用,OnAttachStateChangeListener使得整體動畫更加平滑,也避免了view不可見時,動畫仍然在持續(xù)執(zhí)行浪費資源。當然如fragment不可見時的監(jiān)聽需要完善

override fun onAnimationEnd(animation: Animator?) {
    (animation as? ObjectAnimator)?.let { animator ->
        (animator.target as? View)?.let { view ->
            animators.remove("${view.hashCode()}-${animator.hashCode()}")
            recycle(view)
        }
        // target釋放
        animator.target = null
    }
}
override fun onAnimationCancel(animation: Animator?) {
    (animation as? ObjectAnimator)?.let { animator ->
        (animator.target as? View)?.let { view ->
            view.translationX = measuredWidth.toFloat()
        }
    }
}
override fun onViewAttachedToWindow(v: View?) {
    if (animators.isNotEmpty()) {
        resume()
    } else {
        start()
    }
}
override fun onViewDetachedFromWindow(v: View?) {
    pause()
}

對外能力

fun start() {
    if (measuredWidth == 0) {
        this.post {
            // 如果測量還未完成,那就等待post后發(fā)起
            next(obtain())
        }
        return
    }
    next(obtain())
}
fun stop() {
    val it = animators.values.iterator()
    while (it.hasNext()) {
        val i = it.next()
        it.remove()
        i.cancel()
    }
}
fun pause() {
    for (i in animators.values) {
        i.pause()
    }
}
fun resume() {
    for (i in animators.values) {
        i.resume()
    }
}

完整代碼

歡迎支持,搜索MarqueeAnimalView即可 github.com/wjf-962464/Self_Demo.git

到此這篇關(guān)于Android實現(xiàn)循環(huán)輪播跑馬燈的效果的文章就介紹到這了,更多相關(guān)Android跑馬燈內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論