@@ -3,13 +3,12 @@ package com.github.mikephil.charting.utils
33import android.graphics.Canvas
44import android.graphics.LinearGradient
55import android.graphics.Paint
6- import android.graphics.Path
76import android.graphics.RectF
87import android.graphics.Shader
98import android.graphics.drawable.Drawable
109import kotlin.math.floor
1110
12- class Fill {
11+ open class Fill {
1312 enum class Type {
1413 EMPTY , COLOR , LINEAR_GRADIENT , DRAWABLE
1514 }
@@ -28,12 +27,12 @@ class Fill {
2827 */
2928 private var mColor: Int? = null
3029
31- private var mFinalColor : Int? = null
30+ private var finalColor : Int? = null
3231
3332 /* *
3433 * the drawable to be used for filling
3534 */
36- protected var mDrawable : Drawable ? = null
35+ protected var drawable : Drawable ? = null
3736
3837 var gradientColors: IntArray = intArrayOf()
3938
@@ -44,46 +43,18 @@ class Fill {
4443 */
4544 private var mAlpha = 255
4645
47- constructor ()
48-
49- constructor (color: Int ) {
50- this .type = Type .COLOR
51- this .mColor = color
52- calculateFinalColor()
53- }
54-
5546 constructor (startColor: Int , endColor: Int ) {
5647 this .type = Type .LINEAR_GRADIENT
5748 this .gradientColors = intArrayOf(startColor, endColor)
5849 }
5950
60- constructor (gradientColors: IntArray ) {
61- this .type = Type .LINEAR_GRADIENT
62- this .gradientColors = gradientColors
63- }
64-
65- constructor (gradientColors: IntArray , gradientPositions: FloatArray ) {
66- this .type = Type .LINEAR_GRADIENT
67- this .gradientColors = gradientColors
68- this .gradientPositions = gradientPositions
69- }
70-
71- constructor (drawable: Drawable ) {
72- this .type = Type .DRAWABLE
73- this .mDrawable = drawable
74- }
75-
7651 var color: Int?
7752 get() = mColor
7853 set(color) {
7954 this .mColor = color
8055 calculateFinalColor()
8156 }
8257
83- fun setGradientColors (startColor : Int , endColor : Int ) {
84- this .gradientColors = intArrayOf(startColor, endColor)
85- }
86-
8758 var alpha: Int
8859 get() = mAlpha
8960 set(alpha) {
@@ -93,10 +64,10 @@ class Fill {
9364
9465 private fun calculateFinalColor () {
9566 if (mColor == null ) {
96- mFinalColor = null
67+ finalColor = null
9768 } else {
9869 val alpha = floor(((mColor!! shr 24 ) / 255.0 ) * (mAlpha / 255.0 ) * 255.0 ).toInt()
99- mFinalColor = (alpha shl 24 ) or (mColor!! and 0xffffff )
70+ finalColor = (alpha shl 24 ) or (mColor!! and 0xffffff )
10071 }
10172 }
10273
@@ -109,38 +80,35 @@ class Fill {
10980 Type .EMPTY -> return
11081
11182 Type .COLOR -> {
112- if (mFinalColor == null ) {
83+ if (finalColor == null ) {
11384 return
11485 }
11586
11687 if (this .isClipPathSupported) {
11788 val save = c.save()
11889
11990 c.clipRect(left, top, right, bottom)
120- c.drawColor(mFinalColor !! )
91+ c.drawColor(finalColor !! )
12192
12293 c.restoreToCount(save)
12394 } else {
12495 // save
125- val previous = paint.getStyle()
126- val previousColor = paint.getColor()
96+ val previous = paint.style
97+ val previousColor = paint.color
12798
12899 // set
129- paint.setStyle( Paint .Style .FILL )
130- paint.setColor(mFinalColor !! )
100+ paint.style = Paint .Style .FILL
101+ paint.color = finalColor !!
131102
132103 c.drawRoundRect(RectF (left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint)
133104
134105 // restore
135- paint.setColor( previousColor)
136- paint.setStyle( previous)
106+ paint.color = previousColor
107+ paint.style = previous
137108 }
138109 }
139110
140111 Type .LINEAR_GRADIENT -> {
141- if (this .gradientColors == null ) {
142- return
143- }
144112
145113 val gradient = LinearGradient (
146114 (if (gradientDirection == Direction .RIGHT )
@@ -151,126 +119,38 @@ class Fill {
151119 bottom
152120 else
153121 top).toInt().toFloat(),
154- (if (gradientDirection == Direction .RIGHT )
155- left
156- else
157- if (gradientDirection == Direction .LEFT )
158- right
159- else
160- left).toInt().toFloat(),
161- (if (gradientDirection == Direction .UP )
162- top
163- else
164- if (gradientDirection == Direction .DOWN )
165- bottom
166- else
167- top).toInt().toFloat(),
168- this .gradientColors!! ,
122+ (when (gradientDirection) {
123+ Direction .RIGHT -> left
124+ Direction .LEFT -> right
125+ else -> left
126+ }).toInt().toFloat(),
127+ (when (gradientDirection) {
128+ Direction .UP -> top
129+ Direction .DOWN -> bottom
130+ else -> top
131+ }).toInt().toFloat(),
132+ this .gradientColors,
169133 this .gradientPositions,
170134 Shader .TileMode .MIRROR
171135 )
172136
173- paint.setShader( gradient)
137+ paint.shader = gradient
174138
175139 c.drawRoundRect(RectF (left, top, right, bottom), mRoundedBarRadius, mRoundedBarRadius, paint)
176140 }
177141
178142 Type .DRAWABLE -> {
179- if (mDrawable == null ) {
180- return
181- }
182-
183- mDrawable!! .setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
184- mDrawable!! .draw(c)
185- }
186- }
187- }
188-
189- fun fillPath (
190- c : Canvas , path : Path , paint : Paint ,
191- clipRect : RectF ?
192- ) {
193- when (this .type) {
194- Type .EMPTY -> return
195-
196- Type .COLOR -> {
197- if (mFinalColor == null ) {
198- return
199- }
200-
201- if (clipRect != null && this .isClipPathSupported) {
202- val save = c.save()
203-
204- c.clipPath(path)
205- c.drawColor(mFinalColor!! )
206-
207- c.restoreToCount(save)
208- } else {
209- // save
210- val previous = paint.getStyle()
211- val previousColor = paint.getColor()
212-
213- // set
214- paint.setStyle(Paint .Style .FILL )
215- paint.setColor(mFinalColor!! )
216-
217- c.drawPath(path, paint)
218-
219- // restore
220- paint.setColor(previousColor)
221- paint.setStyle(previous)
222- }
223- }
224-
225- Type .LINEAR_GRADIENT -> {
226- if (this .gradientColors == null ) {
227- return
228- }
229-
230- val gradient = LinearGradient (
231- 0f ,
232- 0f ,
233- c.getWidth().toFloat(),
234- c.getHeight().toFloat(),
235- this .gradientColors!! ,
236- this .gradientPositions,
237- Shader .TileMode .MIRROR
238- )
239-
240- paint.setShader(gradient)
241-
242- c.drawPath(path, paint)
243- }
244-
245- Type .DRAWABLE -> {
246- if (mDrawable == null ) {
143+ if (drawable == null ) {
247144 return
248145 }
249146
250- ensureClipPathSupported()
251-
252- val save = c.save()
253- c.clipPath(path)
254-
255- mDrawable!! .setBounds(
256- if (clipRect == null ) 0 else clipRect.left.toInt(),
257- if (clipRect == null ) 0 else clipRect.top.toInt(),
258- if (clipRect == null ) c.getWidth() else clipRect.right.toInt(),
259- if (clipRect == null ) c.getHeight() else clipRect.bottom.toInt()
260- )
261- mDrawable!! .draw(c)
262-
263- c.restoreToCount(save)
147+ drawable!! .setBounds(left.toInt(), top.toInt(), right.toInt(), bottom.toInt())
148+ drawable!! .draw(c)
264149 }
265150 }
266151 }
267152
268153 private val isClipPathSupported: Boolean
269154 get() = getSDKInt() >= 18
270155
271- private fun ensureClipPathSupported () {
272- if (getSDKInt() < 18 ) {
273- throw RuntimeException (" Fill-drawables not (yet) supported below API level 18, this code was run on API level \$ {getSDKInt()}" )
274- }
275- }
276156}
0 commit comments