-
Notifications
You must be signed in to change notification settings - Fork 0
/
Untitled-1
117 lines (98 loc) · 3.45 KB
/
Untitled-1
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
package com.centling.scene.util
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.centling.scene.R
import pers.victor.ext.dp2px
/**
* Created by vincent.long on 2018/8/21. (ง •̀_•́)ง
*/
class PickView2(context: Context, attrs: AttributeSet) : View(context, attrs) {
private var mWidth = 0
private var mHeight = 0
private var textHeight = 0f
private var textWidth = 0f
private var textPadding = 10
private lateinit var textPaint: Paint
var unitBaseLine = 0f
private var rectLeft = 0
private var rectTop = 0
private var rectRight = 0
private var rectBottom = 0
private var isMove = false
private var dy = 0f
//private lateinit var rect :Rect
init {
initPaint()
}
private fun initPaint() {
textPaint = Paint()
textPaint.apply {
isAntiAlias = true
strokeWidth = 2f
textSize = dp2px(20).toFloat()
style = Paint.Style.FILL
color = resources.getColor(R.color.black_default)
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val content = "90"
val fontMetrics = textPaint.fontMetrics
textHeight = (fontMetrics.bottom - fontMetrics.top) + dp2px(textPadding).toFloat()
textWidth = textPaint.measureText(content)
(0..9).forEach {
val rect = Rect()
// left top right bottom
rect.apply {
left = 0
top = (it * textHeight).toInt()
right = mWidth
bottom = (it * textHeight).toInt() + textHeight.toInt()
if(isMove){
top = (it * textHeight).toInt() + dy.toInt()
bottom = (it * textHeight).toInt() + textHeight.toInt() + dy.toInt()
}
}
val baseLine = rect.centerY() + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
if (it == 1) {
unitBaseLine = rect.centerY() + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
}
val x = (mWidth - textWidth) / 2
canvas.drawText(content, x, baseLine, textPaint)
}
Log.d("TAG-->", "$unitBaseLine")
canvas.drawText("%", mWidth / 2 + textWidth / 2 + dp2px(textPadding), unitBaseLine, textPaint)
canvas.drawLine(0f, textHeight, mWidth.toFloat(), textHeight, textPaint)
canvas.drawLine(0f, textHeight * 2, mWidth.toFloat(), textHeight * 2, textPaint)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
mWidth = measuredWidth
mHeight = measuredHeight
}
private var mTouchY: Float = 0f
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
mTouchY = event.y
return true
}
MotionEvent.ACTION_MOVE -> {
isMove = true
dy = event.y - mTouchY
mTouchY = event.y
invalidate()
}
MotionEvent.ACTION_UP -> {
isMove = false
}
}
return super.onTouchEvent(event)
}
}