仿IOS關(guān)機(jī)滑動(dòng)View

效果圖

分析

結(jié)構(gòu)拆分
  • 灰色背景:圓角矩形
  • 白色進(jìn)度條:圓角矩形
  • 進(jìn)度條按鈕:圖片
  • 提示文字
注意
  • 快速滑動(dòng)時(shí)X軸返回值超過(guò)最大可滑動(dòng)距離要做處理
@SuppressLint("ResourceAsColor")
class CustomSlideButton @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    /**
     * 自定義屬性相關(guān)
     */
    private var thumbImg by Delegates.notNull<Int>()
    private var bgColor by Delegates.notNull<Int>()
    private var pgColor by Delegates.notNull<Int>()
    private var thumbColor by Delegates.notNull<Int>()
    private var centerTextColor by Delegates.notNull<Int>()
    private var centerTextSize by Delegates.notNull<Int>()
    private var centerText:String
    private var thumbRadius by Delegates.notNull<Int>()

    /**
     * 畫筆
     */
    private val paintBg:Paint by lazy { Paint() }
    private val paintPg:Paint by lazy { Paint() }
    private val paintCenterText:Paint by lazy { Paint() }

    private lateinit var ovalFull:RectF
    private lateinit var ovalProgress:RectF

    private var downX:Float=0f  //按下位置x
    private var downY:Float=0f  //按下位置y
    private var progressRight=0 //內(nèi)圈右邊距離
    private var progressBarPadding=0 //內(nèi)圈padding
    private var thumbBitmap: Bitmap //thump 圖片
    private var finishSlide=false //完成滑動(dòng)
    private var followMode=false //左邊跟隨滑動(dòng)
    private var moveX=0f //當(dāng)前移動(dòng)距離
    private var maxMoveX=0 //最大移動(dòng)距離
    private var viewWidth:Int=0 //width
    private var viewHeight:Int=0 //height

    private var anmitonReset:ValueAnimator?=null //重置view動(dòng)畫

    interface SlideFinishListener {
        fun finishSlide()
    }

    private var slideFinishListener: SlideFinishListener? = null

    fun setSlideFinishListener(slideFinishListener: SlideFinishListener?) {
        this.slideFinishListener = slideFinishListener
    }

    init {
        val typeArray =
            context.theme.obtainStyledAttributes(attrs, R.styleable.CustomSlideBarStyle, defStyleAttr, 0)
        thumbImg=
            typeArray.getResourceId(R.styleable.CustomSlideBarStyle_thumbImg,R.mipmap.ic_open_net)
        bgColor = typeArray.getColor(R.styleable.CustomSlideBarStyle_backgroundFull, R.color.white_70)
        pgColor =
            typeArray.getColor(R.styleable.CustomSlideBarStyle_progressColor, R.color.white)
        thumbColor =
            typeArray.getColor(R.styleable.CustomSlideBarStyle_thumbColor, R.color.white)
        centerTextColor=
            typeArray.getColor(R.styleable.CustomSlideBarStyle_centerTextColor,R.color.black)
        centerTextSize=
            typeArray.getDimensionPixelSize(R.styleable.CustomSlideBarStyle_centerTextSize,27)
        centerText=
            typeArray.getString(R.styleable.CustomSlideBarStyle_centerText).toString()
        followMode=
            typeArray.getBoolean(R.styleable.CustomSlideBarStyle_leftFollowRight,true)
        typeArray.recycle()

        progressBarPadding=context.resources.getDimensionPixelSize(R.dimen.silide_bar_padding)
        thumbBitmap=BitmapFactory.decodeResource(context.resources,thumbImg)


        paintBg.color = bgColor
        paintBg.alpha= (255*0.7).toInt()
        paintCenterText.style=Paint.Style.FILL
        paintBg.isAntiAlias=true

        paintPg.color=pgColor
        paintPg.style=Paint.Style.FILL
        paintPg.isAntiAlias=true

        paintCenterText.color=centerTextColor
        paintCenterText.style=Paint.Style.FILL
        paintCenterText.textSize= centerTextSize.toFloat()
        paintCenterText.textAlign = Paint.Align.CENTER
        paintCenterText.isFakeBoldText=true //粗體
        paintCenterText.isAntiAlias=true


    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //畫背景
        ovalFull= RectF(if(followMode)moveX else 0f,0f, viewWidth.toFloat(), viewHeight.toFloat())
        canvas.drawRoundRect(ovalFull,thumbRadius.toFloat(),thumbRadius.toFloat(),paintBg)
        //畫文字
        drawCenterText(canvas)
        //畫進(jìn)度條
        ovalProgress= RectF(if(followMode) progressBarPadding.toFloat()+ moveX else progressBarPadding.toFloat(),progressBarPadding.toFloat(), progressRight.toFloat()+moveX, viewHeight.toFloat()-progressBarPadding.toFloat())
        canvas.drawRoundRect(ovalProgress,thumbRadius.toFloat(),thumbRadius.toFloat(),paintPg)

        //畫進(jìn)度按鈕
        canvas.drawBitmap(thumbBitmap,progressBarPadding.toFloat()+moveX,progressBarPadding.toFloat(),null)

    }

    /**
     * 畫中間文字
     */
    private fun drawCenterText(canvas: Canvas) {
        if (followMode && moveX!=0f){//跟隨滑動(dòng)模式下,滑動(dòng)到最左邊才顯示中間文字
            return
        }

        //畫文字
        //計(jì)算baseline
        val fontMetrics: FontMetrics = paintCenterText.fontMetrics
        val distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        val baseline: Float = ovalFull.centerY() + distance
        //        canvas.drawText(centerText, ovalFull.centerX(), baseline, paintCenterText)
        canvas.drawText(
            centerText,
            viewHeight + (viewWidth - viewHeight) / 2f,
            baseline,
            paintCenterText
        )
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        thumbRadius=h/2
        viewWidth=w
        viewHeight=h
        progressRight=h-progressBarPadding
        maxMoveX=w-h
        //壓縮圖片到指定大小
        thumbBitmap= BitmapUtil.zoomImg2(thumbBitmap,h-progressBarPadding*2,h-progressBarPadding*2)
    }


    override fun onTouchEvent(event: MotionEvent): Boolean {
        when(event.action){
            MotionEvent.ACTION_DOWN->{
                //按下位置在按鈕范圍內(nèi)才接收
                if (event.x in 0f..viewHeight.toFloat() && event.y in 0f..viewHeight.toFloat()){
                    downX= event.x
                    downY= event.y
                }
            }
            MotionEvent.ACTION_MOVE->{
                if (downX!=0f && downY!=0f){
                    var currMovex=event.x-downX
                    if (currMovex<0){currMovex=0f}
                    if (currMovex.toInt() in 0..maxMoveX){
                        moveX=currMovex
                        invalidate()
                    }else if(currMovex.toInt()>=maxMoveX){
                        if (!finishSlide){
                            moveX= maxMoveX.toFloat() //快速滑動(dòng)超出最大可移動(dòng)范圍時(shí),直接繪制到最右端
                            invalidate()
                            finishSlide=true
                            slideFinishListener?.finishSlide()
                        }
                    }
                }
            }
            MotionEvent.ACTION_UP->{
                if (downX!=0f && downY!=0f && !finishSlide){
                    downX=0f
                    downY=0f
                    if (moveX!=0f){
                        resetSlideButton()//未完成滑動(dòng),重置view
                    }
                }
            }
        }
        return true
    }


    private fun resetSlideButton(){
        if (anmitonReset?.isRunning == true){
            return
        }
        anmitonReset = ValueAnimator.ofFloat(moveX,0f)
        anmitonReset!!.duration = 200
        anmitonReset!!.interpolator= AccelerateInterpolator()
        anmitonReset!!.addUpdateListener { animation ->
            moveX = animation.animatedValue as Float
            invalidate()
        }
        anmitonReset!!.start()
    }

    public fun resetState(){
        if (anmitonReset?.isRunning == true){
            anmitonReset?.cancel()
        }
        finishSlide=false
        downY=0f
        downX=0f
        resetSlideButton()
    }

}

attrs.xml

<declare-styleable name="CustomSlideBarStyle">
        <attr name="thumbImg" format="reference"/>
        <attr name="thumbColor" format="color"/>
        <attr name="backgroundFull" format="color"/>
        <attr name="progressColor" format="color"/>
        <attr name="centerText" format="string"/>
        <attr name="centerTextColor" format="color"/>
        <attr name="centerTextSize" format="dimension"/>
        <attr name="leftFollowRight" format="boolean"/>
</declare-styleable>

?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會(huì)卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,913評(píng)論 2 59
  • Canvas畫布的使用 Canvas類相當(dāng)于一個(gè)矩形畫布,默認(rèn)0,0坐標(biāo)是左上角。用到的坐標(biāo)都是畫布上的(即視圖坐...
    qianxL閱讀 378評(píng)論 0 0
  • 一、View事件體系 1.什么是 View 和 View的位置坐標(biāo) View是什么: View 是一種界面層的控件...
    sssssss_閱讀 1,498評(píng)論 0 7
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,194評(píng)論 8 265
  • 本文整理自: Google 官方文檔之自定義 View,筆者省略了對(duì)自己幫助不大的章節(jié),拜讀原文請(qǐng)點(diǎn)鏈接。 一、繼...
    程序員K哥閱讀 518評(píng)論 0 3

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