Skip to content

Commit

Permalink
wheel view partial
Browse files Browse the repository at this point in the history
  • Loading branch information
sidsharma2002 committed Sep 6, 2022
1 parent e19aa1e commit 9be1e5a
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 12 deletions.
7 changes: 3 additions & 4 deletions app/src/main/java/com/example/spinwill/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

AppWillRoomDatabase.getInstance(this.applicationContext)
setupDependencies()
// setupWork()
setupUi()
Expand All @@ -40,10 +39,10 @@ class MainActivity : AppCompatActivity() {
// set context, remoteDb, localDb, dao, bitmap load use-case.
injector.init(this, RemoteDatabaseImpl())

val daoConc = SpinWillDaoConc()
daoConc.setDao(AppWillRoomDatabase.getInstance(this.applicationContext).getSpinWillDao())
val daoActions = SpinWillDaoConc()
daoActions.setDao(AppWillRoomDatabase.getInstance(this.applicationContext).getSpinWillDao())
injector.setLocalDatabase(
SpinWillLocalDbImpl(daoConc)
SpinWillLocalDbImpl(daoActions)
)

injector.setBitmapLoadUseCase(
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@
android:id="@+id/iv_1"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/iv_2"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintTop_toBottomOf="@id/iv_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
app:layout_constraintTop_toBottomOf="@id/iv_1" />

<ProgressBar
android:layout_width="50dp"
android:layout_height="50dp"
<com.example.spinwill.ui.WillView1
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginBottom="22dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
app:layout_constraintTop_toBottomOf="@id/iv_2" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.spinwill.ui

data class WillDimenProperties(
var paddingLeft: Int = 0,
var paddingRight: Int = 0,
var paddingTop: Int = 0,
var paddingBottom: Int = 0,
var padding: Int = 0,
var radius: Int = 0,
var center: Int = 0
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.spinwill.ui

import android.graphics.RadialGradient

data class WillGradient(
var darkYellow: RadialGradient? = null,
var lightYellow: RadialGradient? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.spinwill.ui

import android.graphics.Paint

data class WillPaintProperties(
val archPaint: Paint = Paint(),
val textPaint: Paint = Paint(),
val overlayTextPaint: Paint = Paint(),
val separationArchPaint: Paint = Paint()
)
199 changes: 199 additions & 0 deletions spinwill/src/main/java/com/example/spinwill/ui/WillView1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.example.spinwill.ui

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.example.spinwill.R
import com.example.spinwill.utils.dp
import kotlin.math.min

class WillView1 constructor(
context: Context,
attributeSet: AttributeSet?
) : View(context, attributeSet) {

companion object {
const val DEFAULT_PADDING = 5
}

private val dimenProps = WillDimenProperties()
private val paintProps = WillPaintProperties()
private val gradientProps = WillGradient()
private var willRange = RectF()
private var mPath = Path()

private val wheelSize = 8 // TODO for testing

override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
if (canvas == null) return

var tempAngle = 0f
val sweepAngle: Float = 360f / wheelSize
val rotateAngle = sweepAngle / 2 + 90

for (i in 0..7) {
// draw graphics
drawPieBackground(i, tempAngle, sweepAngle, canvas)
// drawImage(canvas, tempAngle, mWheelItems.get(i).bitmap, rotateAngle)
drawOverlayText(
canvas,
tempAngle,
sweepAngle,
i.toString()
)
drawText(
canvas,
tempAngle,
sweepAngle,
i.toString()
)
drawSeparationArc(willRange, tempAngle, sweepAngle, canvas)
// prepare for next iteration
tempAngle += sweepAngle
paintProps.archPaint.reset()
}
}

private fun drawPieBackground(i: Int, tempAngle: Float, sweepAngle: Float, canvas: Canvas) {
if (i % 2 == 0) paintProps.archPaint.shader = gradientProps.darkYellow
if (i % 2 == 1) paintProps.archPaint.shader = gradientProps.lightYellow
canvas.drawArc(willRange, tempAngle, sweepAngle, true, paintProps.archPaint)
}

private fun drawOverlayText(
canvas: Canvas,
tempAngle: Float,
sweepAngle: Float,
text: String
) {
mPath.addArc(willRange, tempAngle, sweepAngle)
val textWidth: Float = paintProps.overlayTextPaint.measureText(text)
val hOffset: Int = (dimenProps.radius * Math.PI / wheelSize - textWidth / 2).toInt()
val vOffset: Int = dimenProps.radius / 4 - 5
canvas.drawTextOnPath(
text,
mPath,
hOffset.toFloat(),
vOffset.toFloat(),
paintProps.overlayTextPaint
)
mPath.reset()
}

private fun drawText(canvas: Canvas, tempAngle: Float, sweepAngle: Float, text: String) {
mPath.addArc(willRange, tempAngle, sweepAngle) //used global Path
val textWidth: Float = paintProps.textPaint.measureText(text)
val hOffset: Int = (dimenProps.radius * Math.PI / wheelSize - textWidth / 2).toInt()
// change this '5' number to change the radial distance of text from the center
// change this '5' number to change the radial distance of text from the center
val vOffset: Int = dimenProps.radius / 5 - 3
paintProps.textPaint.color = ContextCompat.getColor(context, R.color.spinwill_text_color)
canvas.drawTextOnPath(
text,
mPath,
hOffset.toFloat(),
vOffset.toFloat(),
paintProps.textPaint
)
mPath.reset()
}

private fun drawSeparationArc(
range: RectF,
tempAngle: Float,
sweepAngle: Float,
canvas: Canvas
) {
canvas.drawArc(range, tempAngle, sweepAngle, true, paintProps.separationArchPaint)
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val width = min(measuredWidth, measuredHeight)

dimenProps.padding =
if (paddingLeft == 0) DEFAULT_PADDING else paddingLeft
dimenProps.radius = (width - (dimenProps.padding * 2)) / 2
dimenProps.center = width / 2

setMeasuredDimension(
width,
width
) // hence the size for the view will be min(w,h) i.e square

initComponents()
}

private fun initComponents() {
paintProps.archPaint.apply {
isAntiAlias = true
isDither = true
color = ContextCompat.getColor(context, R.color.coin_txt)
strokeWidth = 0.01f
style = Paint.Style.FILL_AND_STROKE
}

paintProps.textPaint.apply {
isAntiAlias = true
isDither = true
typeface = ResourcesCompat.getFont(
context,
R.font.montserrat_extrabolditalic
)
textSize = 20.dp()
letterSpacing = 0.1f
}

paintProps.overlayTextPaint.apply {
color = ContextCompat.getColor(
context,
R.color.spinwill_overalytext_color
)
isAntiAlias = true
isDither = true
typeface = ResourcesCompat.getFont(
context,
R.font.montserrat_extrabolditalic
)
textSize = 45.dp()
letterSpacing = 0.1f
}

paintProps.separationArchPaint.apply {
style = Paint.Style.STROKE
color = ContextCompat.getColor(
context,
R.color.spinwill_arc_seperation_line
)
isAntiAlias = true
isDither = true
strokeWidth = 3f
}

val padding = dimenProps.padding.toFloat()
val diameter = dimenProps.radius * 2
willRange = RectF(padding, padding, padding + diameter, padding + diameter)
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
gradientProps.darkYellow = RadialGradient(
w / 2f, h / 2f,
h / 3.5f,
ContextCompat.getColor(context, R.color.spinwill_darkyellow_gradientLight),
ContextCompat.getColor(context, R.color.spinwill_darkyellow_gradientDark),
Shader.TileMode.CLAMP
)
gradientProps.lightYellow = RadialGradient(
w / 2f, h / 2f,
h / 2.6f,
ContextCompat.getColor(context, R.color.white),
ContextCompat.getColor(context, R.color.spinwil_lightyellow_gradientDark),
Shader.TileMode.CLAMP
)
}
}
14 changes: 14 additions & 0 deletions spinwill/src/main/java/com/example/spinwill/utils/DimenExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.spinwill.utils

import android.content.res.Resources
import android.util.TypedValue

fun Int.dp(): Float {
val dip = this
val r: Resources = Resources.getSystem()
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dip.toFloat(),
r.displayMetrics
)
}
Binary file not shown.
6 changes: 6 additions & 0 deletions spinwill/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="coin_txt">#fbb42a</color>
<color name="spinwill_overalytext_color">#12770E00</color>
<color name="spinwill_arc_seperation_line">#80E08720</color>
</resources>
7 changes: 7 additions & 0 deletions spinwill/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="coin_txt">#fbb42a</color>
<color name="spinwill_overalytext_color">#12770E00</color>
<color name="spinwill_arc_seperation_line">#80E08720</color>
<color name="spinwill_darkyellow_gradientLight">#FFC87E</color>
<color name="spinwill_darkyellow_gradientDark">#F4C21A</color>
<color name="spinwil_lightyellow_gradientDark">#FDE79E</color>
<color name="spinwill_text_color">#770E00</color>
</resources>

0 comments on commit 9be1e5a

Please sign in to comment.