From 89c03362772b33888a05b87366ce4a077db8a206 Mon Sep 17 00:00:00 2001 From: Balazs Rozsenich Date: Wed, 1 Nov 2017 18:05:50 +0100 Subject: [PATCH 1/2] Ability to set gradient colors for active line --- .gitignore | 3 + .../res/layout/fragment_basic_example.xml | 21 ++++++ .../bendik/simplerangeview/SimpleRangeView.kt | 67 ++++++++++++++++++- .../src/main/res/values/attrs_rangeview.xml | 6 ++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9254366..70cbf38 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ captures/ # Keystore files *.jks .DS_Store +.gradle/ +.idea/ +build/kotlin-build/ diff --git a/app/src/main/res/layout/fragment_basic_example.xml b/app/src/main/res/layout/fragment_basic_example.xml index 3410236..35caaad 100644 --- a/app/src/main/res/layout/fragment_basic_example.xml +++ b/app/src/main/res/layout/fragment_basic_example.xml @@ -120,5 +120,26 @@ app:end="8" app:minDistance="3" /> + + + + + \ No newline at end of file diff --git a/simplerangeview/src/main/kotlin/me/bendik/simplerangeview/SimpleRangeView.kt b/simplerangeview/src/main/kotlin/me/bendik/simplerangeview/SimpleRangeView.kt index 6263f48..8708281 100644 --- a/simplerangeview/src/main/kotlin/me/bendik/simplerangeview/SimpleRangeView.kt +++ b/simplerangeview/src/main/kotlin/me/bendik/simplerangeview/SimpleRangeView.kt @@ -67,7 +67,6 @@ open class SimpleRangeView @JvmOverloads constructor( invalidate() } - var lineColor = DEFAULT_LINE_COLOR set(value) { field = value @@ -75,6 +74,13 @@ open class SimpleRangeView @JvmOverloads constructor( invalidate() } + var activeLineColorMode = DEFAULT_LINE_COLOR_MODE + set(value) { + field = value + initPaints() + invalidate() + } + var activeLineColor = DEFAULT_ACTIVE_LINE_COLOR set(value) { field = value @@ -82,6 +88,20 @@ open class SimpleRangeView @JvmOverloads constructor( invalidate() } + var activeLineGradientStart = DEFAULT_LINE_COLOR + set(value) { + field = value + initPaints() + invalidate() + } + + var activeLineGradientEnd = DEFAULT_LINE_COLOR + set(value) { + field = value + initPaints() + invalidate() + } + var fixedLineColor = DEFAULT_FIXED_LINE_COLOR set(value) { field = value @@ -349,7 +369,10 @@ open class SimpleRangeView @JvmOverloads constructor( fixedThumbLabelColor = styledAttrs.getColor(R.styleable.SimpleRangeView_fixedThumbLabelColor, fixedThumbLabelColor) lineColor = styledAttrs.getColor(R.styleable.SimpleRangeView_lineColor, lineColor) + activeLineColorMode = styledAttrs.getInt(R.styleable.SimpleRangeView_activeLineColorMode, activeLineColorMode) activeLineColor = styledAttrs.getColor(R.styleable.SimpleRangeView_activeLineColor, activeLineColor) + activeLineGradientStart = styledAttrs.getColor(R.styleable.SimpleRangeView_activeLineGradientStart, activeLineGradientStart) + activeLineGradientEnd = styledAttrs.getColor(R.styleable.SimpleRangeView_activeLineGradientEnd, activeLineGradientEnd) fixedLineColor = styledAttrs.getColor(R.styleable.SimpleRangeView_fixedLineColor, fixedLineColor) tickColor = styledAttrs.getColor(R.styleable.SimpleRangeView_tickColor, tickColor) activeTickColor = styledAttrs.getColor(R.styleable.SimpleRangeView_activeTickColor, activeTickColor) @@ -433,7 +456,6 @@ open class SimpleRangeView @JvmOverloads constructor( paintActive = Paint(Paint.ANTI_ALIAS_FLAG) paintActive.style = Paint.Style.FILL - paintActive.color = activeLineColor paintTick = Paint(Paint.ANTI_ALIAS_FLAG) paintTick.style = Paint.Style.FILL @@ -620,6 +642,14 @@ open class SimpleRangeView @JvmOverloads constructor( } protected open fun drawActiveLine(canvas: Canvas, x: Float, y: Float, w: Float, h: Float) { + if (activeLineColorMode == ColorMode.SOLID) + paintActive.color = activeLineColor + else + paintActive.shader = LinearGradient(x, y, x + w, y + h, + activeLineGradientStart, + activeLineGradientEnd, + Shader.TileMode.MIRROR) + drawLine(canvas, x, y, w, h, paintActive) } @@ -1013,7 +1043,10 @@ open class SimpleRangeView @JvmOverloads constructor( this.fixedLabelColor = state.fixedLabelColor this.fixedThumbLabelColor = state.fixedThumbLabelColor this.lineColor = state.lineColor + this.activeLineColorMode = state.activeLineColorMode this.activeLineColor = state.activeLineColor + this.activeLineGradientStart = state.activeLineGradientStart + this.activeLineGradientEnd = state.activeLineGradientEnd this.fixedLineColor = state.fixedLineColor this.tickColor = state.tickColor this.activeTickColor = state.activeTickColor @@ -1058,7 +1091,10 @@ open class SimpleRangeView @JvmOverloads constructor( var fixedLabelColor: Int = 0 var fixedThumbLabelColor: Int = 0 var lineColor: Int = 0 + var activeLineColorMode: Int = 0 var activeLineColor: Int = 0 + var activeLineGradientStart: Int = 0 + var activeLineGradientEnd: Int = 0 var fixedLineColor: Int = 0 var tickColor: Int = 0 var activeTickColor: Int = 0 @@ -1108,7 +1144,10 @@ open class SimpleRangeView @JvmOverloads constructor( this.fixedThumbLabelColor = input.readInt() this.lineColor = input.readInt() + this.activeLineColorMode = input.readInt() this.activeLineColor = input.readInt() + this.activeLineGradientStart = input.readInt() + this.activeLineGradientEnd = input.readInt() this.fixedLineColor = input.readInt() this.tickColor = input.readInt() this.activeTickColor = input.readInt() @@ -1158,7 +1197,10 @@ open class SimpleRangeView @JvmOverloads constructor( output.writeInt(this.fixedLabelColor) output.writeInt(this.fixedThumbLabelColor) output.writeInt(this.lineColor) + output.writeInt(this.activeLineColorMode) output.writeInt(this.activeLineColor) + output.writeInt(this.activeLineGradientStart) + output.writeInt(this.activeLineGradientEnd) output.writeInt(this.fixedLineColor) output.writeInt(this.tickColor) output.writeInt(this.activeTickColor) @@ -1267,11 +1309,26 @@ open class SimpleRangeView @JvmOverloads constructor( return this } + fun activeLineColorMode(colorMode: Int): Builder { + rangeView.activeLineColorMode = colorMode + return this + } + fun activeLineColor(color: Int): Builder { rangeView.activeLineColor = color return this } + fun activeLineGradientStart(color: Int): Builder { + rangeView.activeLineGradientStart = color + return this + } + + fun activeLineGradientEnd(color: Int): Builder { + rangeView.activeLineGradientEnd = color + return this + } + fun fixedLineColor(color: Int): Builder { rangeView.fixedLineColor = color return this @@ -1477,6 +1534,7 @@ open class SimpleRangeView @JvmOverloads constructor( val DEFAULT_FIXED_LABEL_COLOR = Color.parseColor("#C5C5C5") val DEFAULT_FIXED_THUMB_LABEL_COLOR = Color.parseColor("#C5C5C5") + val DEFAULT_LINE_COLOR_MODE = ColorMode.SOLID val DEFAULT_LINE_COLOR = Color.parseColor("#F7F7F7") val DEFAULT_ACTIVE_LINE_COLOR = Color.parseColor("#0C6CE1") val DEFAULT_FIXED_LINE_COLOR = Color.parseColor("#E3E3E3") @@ -1525,5 +1583,10 @@ open class SimpleRangeView @JvmOverloads constructor( val DEFAULT_LABEL_FONT_SIZE = 12f } + + object ColorMode { + val SOLID = 0 + val GRADIENT = 1 + } } diff --git a/simplerangeview/src/main/res/values/attrs_rangeview.xml b/simplerangeview/src/main/res/values/attrs_rangeview.xml index 9059e31..f8566ef 100644 --- a/simplerangeview/src/main/res/values/attrs_rangeview.xml +++ b/simplerangeview/src/main/res/values/attrs_rangeview.xml @@ -2,7 +2,13 @@ + + + + + + From a305a8ffe793bca453bfe44881099de0763b3fd1 Mon Sep 17 00:00:00 2001 From: Balazs Rozsenich Date: Wed, 1 Nov 2017 18:21:01 +0100 Subject: [PATCH 2/2] README update according to new attributes * ignore iml files --- .gitignore | 6 ++++++ README.md | 3 +++ 2 files changed, 9 insertions(+) diff --git a/.gitignore b/.gitignore index 70cbf38..0fecd16 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,9 @@ captures/ .gradle/ .idea/ build/kotlin-build/ +app/app.iml +build/ +gradle/ +gradlew +gradlew.bat +simplerangeview/simplerangeview.iml diff --git a/README.md b/README.md index 3085958..c6df669 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,10 @@ Developers can customize the following attributes (both via XML and programmatic | name | format | description | default value | | :--------------------------:| :------: | :-----------: | :-----------: | | lineColor | color | Range bar color | #F7F7F7 | +| activeLineColorMode | integer | Active range bar coloring (solid or gradient) | 0 (ColorMode.SOLID) | | activeLineColor | color | Active range bar color | #0C6CE1 | +| activeLineGradientStart | color | Active range bar gradient start color | #0C6CE1 | +| activeLineGradientEnd | color | Active range bar gradient end color | #0C6CE1 | | fixedLineColor | color | Fixed range bar color | #E3E3E3 | | lineThickness | dimension | Range bar thickness | 4 | | activeLineThickness | dimension | Active range bar thickness | 6 |