lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.kuanzhai.driver.utils.view
 
import android.content.Context
import android.graphics.*
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat
import cn.sinata.xldutils.utils.backgroundDrawable
import cn.sinata.xldutils.utils.sysErr
import com.kuanzhai.driver.R
import org.jetbrains.anko.dip
 
 
/**
 * 滑动按钮
 * Created by zhangyujiu on 2018/1/2.
 */
class SlidingButton : View {
    lateinit var gradientDrawable: GradientDrawable
    lateinit var paint: Paint
    lateinit var mBound: Rect
    var mText = "出发前往预约地点"
    var flagText = mText
    lateinit var bmp: Bitmap
 
    var onSwipeListener: (() -> Unit)? = null
 
    constructor(context: Context) : super(context) {
        init()
    }
 
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }
 
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }
 
    private fun init() {
        val colors = intArrayOf(
            ContextCompat.getColor(context, R.color.main_yellow_shen),
                ContextCompat.getColor(context, R.color.main_yellow_qia))
        gradientDrawable = GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors)
        gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
        gradientDrawable.cornerRadius = dip(4).toFloat()
        backgroundDrawable = gradientDrawable
 
        mBound = Rect()
 
        paint = Paint()
        paint.isAntiAlias = true
        paint.style = Paint.Style.FILL
        paint.textSize = dip(17).toFloat()
        paint.color = ContextCompat.getColor(context, R.color.white)
 
        bmp = BitmapFactory.decodeResource(context.resources, R.mipmap.slide)
    }
 
 
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = onMeasureR(0, widthMeasureSpec)
        val height = onMeasureR(1, heightMeasureSpec)
        setMeasuredDimension(width, height)
    }
 
    /**
     * 计算控件宽高
     *
     * @param attr属性
     * [0宽,1高]
     * @param oldMeasure
     */
    fun onMeasureR(attr: Int, oldMeasure: Int): Int {
 
        var newSize = 0
        val mode = View.MeasureSpec.getMode(oldMeasure)
        val oldSize = View.MeasureSpec.getSize(oldMeasure)
 
        when (mode) {
            View.MeasureSpec.EXACTLY -> newSize = oldSize
            View.MeasureSpec.AT_MOST, View.MeasureSpec.UNSPECIFIED -> {
 
                val value: Float
 
                if (attr == 0) {
 
                    value = mBound.width().toFloat()
                    // value = mPaint.measureText(mText);
 
                    // 控件的宽度  + getPaddingLeft() +  getPaddingRight()
                    newSize = (paddingLeft.toFloat() + value + paddingRight.toFloat()).toInt()
 
                } else if (attr == 1) {
 
                    value = mBound.height().toFloat()
 
                    // 控件的高度  + getPaddingTop() +  getPaddingBottom()
                    newSize = (paddingTop.toFloat() + value + paddingBottom.toFloat()).toInt()
 
                }
            }
        }
 
        return newSize
    }
 
    var moveStartX = 0f
    var moveEndX = 0f
    var dx = 0f
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //重新测量文字
        paint.getTextBounds(mText, 0, mText.length, mBound)
        val fm = paint.fontMetricsInt
        /*
         * 控件宽度/2 - 文字宽度/2
         */
        val startX = width / 2 - mBound.width() / 2
 
        /*
         * 控件高度/2 + 文字高度/2,绘制文字从文字左下角开始,因此"+"
         */
        val startY = height / 2 - fm.descent + (fm.bottom - fm.top) / 2
 
        // 绘制文字
        canvas?.drawText(mText, startX.toFloat(), startY.toFloat(), paint)
        val bmpHeight = bmp.height
        canvas?.drawBitmap(bmp, bmp.width.toFloat() + dx, (height / 2 - bmpHeight / 2).toFloat(), paint)
        if (bmp.isRecycled) {
            bmp.recycle()
        }
    }
 
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (!isEnabled)
            return true
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                moveStartX = event.x
            }
            MotionEvent.ACTION_MOVE -> {
                moveEndX = event.x
                dx = moveEndX - moveStartX
                if (dx <= 0) {
                    dx = 0f
                }
                mText = if (dx >= width / 2) {
                    "松开触发"
                } else {
                    flagText
                }
                invalidate()
            }
            MotionEvent.ACTION_UP -> {
                if (dx >= width / 2) {
                    sysErr("松开触发")
                    onSwipeListener?.invoke()
                }
                dx = 0f
                mText=flagText
                invalidate()
            }
        }
 
        return true
    }
 
    //更新按钮文字
    fun changeButtonText(content: String) {
        if (!TextUtils.isEmpty(content)) {
            mText = content
            flagText = mText
            invalidate()
        }
    }
 
    open fun changeColor(){
        val colors = intArrayOf(
            ContextCompat.getColor(context, R.color.color_929191),
            ContextCompat.getColor(context, R.color.color_929191))
        gradientDrawable = GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors)
        gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
        gradientDrawable.cornerRadius = dip(4).toFloat()
        backgroundDrawable = gradientDrawable
        invalidate()
    }
}