Skip to content

Commit 8a6fffc

Browse files
committed
add alpha to duration dialog page numbers
1 parent f609e99 commit 8a6fffc

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

features/feature_dialogs/src/main/java/com/example/util/simpletimetracker/feature_dialogs/duration/customView/DurationView.kt

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import android.annotation.SuppressLint
55
import android.content.Context
66
import android.graphics.Canvas
77
import android.graphics.Color
8+
import android.graphics.LinearGradient
89
import android.graphics.Paint
910
import android.graphics.Rect
11+
import android.graphics.Shader
1012
import android.util.AttributeSet
1113
import android.view.MotionEvent
1214
import android.view.View
@@ -17,6 +19,7 @@ import com.example.util.simpletimetracker.domain.extension.toDuration
1719
import com.example.util.simpletimetracker.feature_dialogs.R
1820
import kotlin.math.round
1921
import androidx.core.content.withStyledAttributes
22+
import com.example.util.simpletimetracker.feature_views.ColorUtils
2023

2124
class DurationView @JvmOverloads constructor(
2225
context: Context,
@@ -31,19 +34,23 @@ class DurationView @JvmOverloads constructor(
3134

3235
// Attrs
3336
private var textColor: Int = 0
37+
private var backgroundColor: Int = 0
3438
private var legendTextColor: Int = 0
3539
private var legendTextSize: Float = 0f
3640
private var legendPadding: Float = 0f
3741
// End of attrs
3842

3943
private var data: ViewData = ViewData.Empty
4044
private val textPaint: Paint = Paint()
45+
private val backgroundPaintTop: Paint = Paint()
46+
private val backgroundPaintBottom: Paint = Paint()
4147
private val legendTextPaint: Paint = Paint()
4248
private var textHeight: Float = 0f
4349
private var textStartHorizontal: Float = 0f
4450
private var textStartVertical: Float = 0f
4551
private val swipeSpeedCoefficient: Float = 2f
4652
private val pageAlpha: Float = 0.3f
53+
private val pageBackgroundAlpha: Float = 0.5f
4754
private val settlingAnimationDurationMs: Long = 300
4855
private val bounds: Rect = Rect()
4956
private val minPageValue = 0
@@ -80,7 +87,7 @@ class DurationView @JvmOverloads constructor(
8087
val h = height.toFloat()
8188

8289
calculateDimensions(w, h)
83-
drawText(canvas, h)
90+
drawText(canvas, w, h)
8491
}
8592

8693
@SuppressLint("ClickableViewAccessibility")
@@ -130,6 +137,9 @@ class DurationView @JvmOverloads constructor(
130137
textColor = getColor(
131138
R.styleable.DurationView_durationTextColor, Color.BLACK,
132139
)
140+
backgroundColor = getColor(
141+
R.styleable.DurationView_durationBackgroundColor, Color.WHITE,
142+
)
133143
legendTextColor = getColor(
134144
R.styleable.DurationView_durationLegendTextColor, Color.BLACK,
135145
)
@@ -178,9 +188,29 @@ class DurationView @JvmOverloads constructor(
178188
textPaint.getTextBounds("0", 0, 1, bounds)
179189
textHeight = bounds.height().toFloat()
180190
textStartVertical = textHeight + (h - textHeight) / 2f
191+
192+
val backgroundColorWithAlpha = ColorUtils.changeAlpha(backgroundColor, pageBackgroundAlpha)
193+
backgroundPaintTop.shader = LinearGradient(
194+
0f,
195+
0f,
196+
0f,
197+
textStartVertical - textHeight,
198+
backgroundColorWithAlpha,
199+
Color.TRANSPARENT,
200+
Shader.TileMode.CLAMP,
201+
)
202+
backgroundPaintBottom.shader = LinearGradient(
203+
0f,
204+
textStartVertical,
205+
0f,
206+
h,
207+
Color.TRANSPARENT,
208+
backgroundColorWithAlpha,
209+
Shader.TileMode.CLAMP,
210+
)
181211
}
182212

183-
private fun drawText(canvas: Canvas, h: Float) {
213+
private fun drawText(canvas: Canvas, w: Float, h: Float) {
184214
// Center text
185215
var currentTextStartHorizontal = textStartHorizontal
186216

@@ -206,6 +236,7 @@ class DurationView @JvmOverloads constructor(
206236
)
207237
drawPages(
208238
canvas = canvas,
239+
w = w,
209240
h = h,
210241
data = data.hours,
211242
currentTextStartHorizontal = currentTextStartHorizontal,
@@ -232,6 +263,7 @@ class DurationView @JvmOverloads constructor(
232263
)
233264
drawPages(
234265
canvas = canvas,
266+
w = w,
235267
h = h,
236268
data = data.minutes,
237269
currentTextStartHorizontal = currentTextStartHorizontal,
@@ -257,6 +289,7 @@ class DurationView @JvmOverloads constructor(
257289
)
258290
drawPages(
259291
canvas = canvas,
292+
w = w,
260293
h = h,
261294
data = data.seconds,
262295
currentTextStartHorizontal = currentTextStartHorizontal,
@@ -270,6 +303,7 @@ class DurationView @JvmOverloads constructor(
270303

271304
private fun drawPages(
272305
canvas: Canvas,
306+
w: Float,
273307
h: Float,
274308
data: Long,
275309
currentTextStartHorizontal: Float,
@@ -317,6 +351,22 @@ class DurationView @JvmOverloads constructor(
317351
pageNumber += 1
318352
}
319353
textPaint.alpha = defaultAlpha
354+
355+
// Draw alpha gradient
356+
canvas.drawRect(
357+
0f,
358+
0f,
359+
w,
360+
textStartVertical - textHeight,
361+
backgroundPaintTop,
362+
)
363+
canvas.drawRect(
364+
0f,
365+
textStartVertical,
366+
w,
367+
h,
368+
backgroundPaintBottom,
369+
)
320370
}
321371

322372
private fun getPageStartHorizontal(

features/feature_dialogs/src/main/res/layout/duration_dialog_fragment.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
android:layout_marginHorizontal="8dp"
2424
android:layout_marginTop="8dp"
2525
android:visibility="visible"
26+
app:durationBackgroundColor="?appBackgroundDialogColor"
2627
app:durationLegendPadding="4dp"
2728
app:durationLegendTextColor="?appTextHintColor"
2829
app:durationLegendTextSize="32sp"

features/feature_dialogs/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<resources>
33
<declare-styleable name="DurationView">
44
<attr name="durationTextColor" format="color" />
5+
<attr name="durationBackgroundColor" format="color" />
56
<attr name="durationLegendTextColor" format="color" />
67
<attr name="durationLegendTextSize" format="dimension" />
78
<attr name="durationLegendPadding" format="dimension" />

0 commit comments

Comments
 (0)