-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e19aa1e
commit 9be1e5a
Showing
10 changed files
with
268 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
spinwill/src/main/java/com/example/spinwill/ui/WillDimenProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
8 changes: 8 additions & 0 deletions
8
spinwill/src/main/java/com/example/spinwill/ui/WillGradient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
10 changes: 10 additions & 0 deletions
10
spinwill/src/main/java/com/example/spinwill/ui/WillPaintProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
199
spinwill/src/main/java/com/example/spinwill/ui/WillView1.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
spinwill/src/main/java/com/example/spinwill/utils/DimenExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters