美文网首页
字母滑动控件

字母滑动控件

作者: 淹死丶的鱼 | 来源:发表于2019-12-11 17:04 被阅读0次
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

/**
 *created by 淹死的鱼 on 2019/12/11
 */


class LetterSideBar @JvmOverloads constructor(
    context: Context,
    attSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attSet, defStyleAttr) {
    private val paint: Paint
    private val rect: Rect
    private var padding = 0f
    private var textsize = 0f
    private var unTextColor = 0
    private var textColor = 0
    private var current = 0
    private var itemHeight = 0
    private var baseLine = 0
    private val lists = arrayListOf(
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
        "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"
    )
    private var listener: TouchListener? = null

    init {
        val typedArray = context.obtainStyledAttributes(attSet, R.styleable.LetterSideBar)
        padding = typedArray.getDimension(R.styleable.LetterSideBar_letterPadding, padding)
        textsize = typedArray.getDimension(R.styleable.LetterSideBar_letterTextSize, textsize)
        unTextColor = typedArray.getColor(R.styleable.LetterSideBar_letterUnSelectColor, unTextColor)
        textColor = typedArray.getColor(R.styleable.LetterSideBar_letterSelectColor, textColor)
        typedArray.recycle()
        rect = Rect()
        paint = Paint()
        paint.color = unTextColor
        paint.isAntiAlias = true
        paint.textSize = textsize
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        paint.getTextBounds("E", 0, 1, rect)
        val metricsInt = paint.fontMetricsInt
        val dy = (metricsInt.bottom - metricsInt.top) / 2 - metricsInt.bottom
        baseLine = rect.height() / 2 + dy
        //计算宽度
        val width = padding * 2 + rect.width()
        //高度
        val height = MeasureSpec.getSize(heightMeasureSpec)
        itemHeight = height / lists.size
        setMeasuredDimension(width.toInt(), height)
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        for (i in 0 until lists.size) {
            if (i == current) {
                paint.color = textColor
            } else {
                paint.color = unTextColor
            }
            if (lists[i] == "I") {
                canvas.drawText(lists[i], width / 2f, baseLine.toFloat() + i * itemHeight, paint)
            } else {
                canvas.drawText(lists[i], width / 2 - rect.width() / 2f, baseLine.toFloat() + i * itemHeight, paint)
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        return when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                var tmp = event.y / itemHeight
                tmp = if (tmp % 1 > 0.5) tmp + 1 else tmp
                if (tmp.toInt() != current) {
                    current = tmp.toInt()
                    if (current < lists.size) {
                        listener?.listener(current, lists[current])
                        invalidate()
                    }
                }
                true
            }
            MotionEvent.ACTION_UP,MotionEvent.ACTION_CANCEL->{
                listener?.cancel()
                true
            }
            else -> super.onTouchEvent(event)
        }
    }

    fun setProgress(current: Int) {
        this.current = current
        listener?.listener(current, lists[current])
        invalidate()
    }

    fun setListener(listener: TouchListener) {
        this.listener = listener
    }

}

interface TouchListener {
    fun listener(current: Int, text: String)
    fun cancel()
}
    <declare-styleable name="LetterSideBar">
        <attr name="letterTextSize" format="dimension" />
        <attr name="letterPadding" format="dimension" />
        <attr name="letterUnSelectColor" format="color" />
        <attr name="letterSelectColor" format="color" />
    </declare-styleable>

相关文章

网友评论

      本文标题:字母滑动控件

      本文链接:https://www.haomeiwen.com/subject/szqagctx.html