This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
RichPathView.kt
186 lines (164 loc) 路 5.53 KB
/
RichPathView.kt
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
package com.richpath
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.IntRange
import androidx.appcompat.widget.AppCompatImageView
import com.richpath.pathparser.PathParser
import com.richpath.model.Vector
import org.xmlpull.v1.XmlPullParserException
import com.richpath.util.XmlParser
import java.io.IOException
import kotlin.math.min
class RichPathView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : AppCompatImageView(context, attrs, defStyleAttr) {
constructor(context: Context?, attrs: AttributeSet?): this(context, attrs, 0)
private lateinit var vector: Vector
private var richPathDrawable: RichPathDrawable? = null
var onPathClickListener: RichPath.OnPathClickListener? = null
init {
init()
setupAttributes(attrs)
}
private fun init() {
//Disable hardware
setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
private fun setupAttributes(attrs: AttributeSet?) {
// Obtain a typed array of attributes
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.RichPathView, 0, 0)
// Extract custom attributes into member variables
val resID: Int
try {
resID = typedArray.getResourceId(R.styleable.RichPathView_vector, -1)
} finally { // TypedArray objects are shared and must be recycled.
typedArray.recycle()
}
if (resID != -1) {
setVectorDrawable(resID)
}
}
/**
* Set a VectorDrawable resource ID.
*
* @param resId the resource ID for VectorDrawableCompat object.
*/
@SuppressLint("ResourceType")
fun setVectorDrawable(@DrawableRes resId: Int) {
val xpp = context.resources.getXml(resId)
vector = Vector()
try {
XmlParser.parseVector(vector, xpp, context)
richPathDrawable = RichPathDrawable(vector, scaleType)
setImageDrawable(richPathDrawable)
} catch (e: IOException) {
e.printStackTrace()
} catch (e: XmlPullParserException) {
e.printStackTrace()
}
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val vector = vector ?: return
val desiredWidth = vector.width
val desiredHeight = vector.height
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
//Measure Width
val width: Int = when (widthMode) {
MeasureSpec.EXACTLY -> {
//Must be this size
widthSize
}
MeasureSpec.AT_MOST -> {
//Can't be bigger than...
min(desiredWidth.toInt(), widthSize)
}
else -> {
//Be whatever you want
desiredWidth.toInt()
}
}
//Measure Height
val height: Int = when (heightMode) {
MeasureSpec.EXACTLY -> {
//Must be this size
heightSize
}
MeasureSpec.AT_MOST -> {
//Can't be bigger than...
min(desiredHeight.toInt(), heightSize)
}
else -> {
//Be whatever you want
desiredHeight.toInt()
}
}
//MUST CALL THIS
setMeasuredDimension(width, height)
}
fun findAllRichPaths(): Array<RichPath> {
return richPathDrawable?.findAllRichPaths() ?: arrayOf()
}
fun findRichPathByName(name: String): RichPath? {
return richPathDrawable?.findRichPathByName(name)
}
/**
* find the first [RichPath] or null if not found
* <p>
* This can be in handy if the vector consists of 1 path only
*
* @return the [RichPath] object found or null
*/
fun findFirstRichPath(): RichPath? {
return richPathDrawable?.findFirstRichPath()
}
/**
* find [RichPath] by its index or null if not found
* <p>
* Note that the provided index must be the flattened index of the path
* <p>
* example:
* <pre>
* {@code <vector>
* <path> // index = 0
* <path> // index = 1
* <group>
* <path> // index = 2
* <group>
* <path> // index = 3
* </group>
* </group>
* <path> // index = 4
* </vector>}
* </pre>
*
* @param index the flattened index of the path
* @return the [RichPath] object found or null
*/
fun findRichPathByIndex(@IntRange(from = 0) index: Int): RichPath? {
return richPathDrawable?.findRichPathByIndex(index)
}
fun addPath(path: String) {
richPathDrawable?.addPath(PathParser.createPathFromPathData(path))
}
fun addPath(path: Path) {
richPathDrawable?.addPath(path)
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
when(event?.action) {
MotionEvent.ACTION_UP -> {
performClick()
}
}
richPathDrawable?.getTouchedPath(event)?.let { richPath ->
richPath.onPathClickListener?.onClick(richPath)
this.onPathClickListener?.onClick(richPath)
}
return true
}
}