自定義 View 實(shí)戰(zhàn) 06 - 仿寫(xiě) RatingBar

RatingBar

效果分析:

star_normal.png

star_select.png

準(zhǔn)備兩張星星圖,一張默認(rèn),一張選中。初始的時(shí)候繪制默認(rèn)的 5 顆星星,然后根據(jù)手勢(shì)繪制選中的星星。

自定義屬性

<declare-styleable name="KRatingbar">
        //要繪制的個(gè)數(shù)
        <attr name="starNum" format="integer"/>
        //星星之間的間距
        <attr name="starPadding" format="integer" />
        //默認(rèn)星星的 resId
        <attr name="starNormal" format="reference" />
        //選中星星的 resId
        <attr name="starSelect" format="reference" />
</declare-styleable>

具體實(shí)現(xiàn)

class KRatingbar @JvmOverloads constructor(
    context: Context,
    attributes: AttributeSet?,
    defStyle: Int = 0
) : View(context, attributes, defStyle) {

    private var mCount = 5
    private var mNormalBitmap: Bitmap
    private var mSelectBitmap: Bitmap
    private var mStarPadding = dp2Px(5, resources)
    private var mCurSelect = -1

    init {
        val ta = context.obtainStyledAttributes(attributes, R.styleable.KRatingbar)
        mCount = ta.getInt(R.styleable.KRatingbar_starNum, 5)
        mStarPadding = ta.getInt(R.styleable.KRatingbar_starPadding, dp2Px(5, resources))
        val normalId = ta.getResourceId(R.styleable.KRatingbar_starNormal, -1)
        if (normalId == 1.unaryMinus()) throw RuntimeException("starNormal can't be empty")
        mNormalBitmap = BitmapFactory.decodeResource(resources, normalId)
        val selectId = ta.getResourceId(R.styleable.KRatingbar_starSelect, -1)
        if (selectId == 1.unaryMinus()) throw RuntimeException("starSelect can't be empty")
        mSelectBitmap = BitmapFactory.decodeResource(resources, selectId)
        ta.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //圖片寬度*繪制的個(gè)數(shù)+星星間的間隙+paddingLeft+paddingRight
        val width =
            mNormalBitmap.width * mCount + mStarPadding * (mCount - 1) + paddingLeft + paddingRight
        //圖片的高度+paddingTop+paddingBottom
        val height = mNormalBitmap.height + paddingTop + paddingBottom
        setMeasuredDimension(width, height)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        for (i in 0 until mCount) {
            var x = i * mNormalBitmap.width
            if (i != 0) {
                //平移畫(huà)布,其實(shí)就是在繪制間隙
                canvas.translate(mStarPadding.toFloat(), 0f)
            }
            if (i < mCurSelect) {
                //繪制選中的星星
                canvas.drawBitmap(mSelectBitmap, x.toFloat(), 0f, null)
            } else {
                //繪制默認(rèn)星星
                canvas.drawBitmap(mNormalBitmap, x.toFloat(), 0f, null)
            }

        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
                //根據(jù)手指按下的 x 坐標(biāo)去計(jì)算按壓的是第幾個(gè)星星
                //這里要 +1,因?yàn)?index 是從 0 開(kāi)始的
                val count = event.x.toInt() / (mNormalBitmap.width+mStarPadding) + 1
                //當(dāng)前選中的和之前一樣不再重復(fù)繪制,count !=1 是為了處理,按壓第一個(gè)時(shí),無(wú)法滑動(dòng)選中的 bug
                if (mCurSelect == count && count != 1) {
                    return false
                }
                //越界時(shí)為 0 
                if (count < 0) {
                    mCurSelect = 0
                }
                //越界時(shí)默認(rèn)為最大的繪制個(gè)數(shù)
                if (count > mCount) {
                    mCurSelect = mCount
                }
                //當(dāng)前選中了第幾個(gè)
                mCurSelect = count
                //重繪
                invalidate()
            }
        }
        //這里要返回 true 才會(huì)處理 onTouchEvent 事件
        return true
    }

}

項(xiàng)目地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容