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
#!/usr/bin/env bash | |
# I've found that the "Migrate to AndroidX" converter in Android Studio doesn't work very | |
# well, so I wrote my own script to do the simple job of converting package names. | |
# | |
# You can download a CSV of package names here: https://developer.android.com/topic/libraries/support-library/downloads/androidx-class-mapping.csv | |
# | |
# It'll run faster on a clean build because then there are fewer files to scan over. | |
# | |
# Uses `gsed` because I'm on a Mac. Can easily replace with `sed` if you don't have `gsed`. |
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
private fun animateWin() { | |
val valueAnimator = ValueAnimator.ofFloat(1f, 0f) | |
valueAnimator.duration = 600 | |
valueAnimator.addUpdateListener(this) | |
valueAnimator.start() | |
} | |
override fun onAnimationUpdate(animation: ValueAnimator) { | |
val measure = PathMeasure(path, false) | |
val offset = (measure.length * (animation.animatedValue as Float)) |
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
private val path = Path() | |
.. | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
drawVerticalLines(canvas) | |
drawHorizontalLines(canvas) | |
drawSquareStates(canvas) | |
if (shouldAnimate) { <--------Check Here | |
canvas.drawPath(path, paint) // path is storing our line | |
} |
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
MotionEvent.ACTION_UP -> { | |
touching = false | |
invalidate(squares[rectIndex.first][rectIndex.second]) | |
val (finalX1, finalY1) = getRectIndexesFor(x, y) | |
if ((finalX1 == rectIndex.first) && (finalY1 == rectIndex.second)) { // if initial touch and final touch is in same rectangle or not | |
squarePressListener?.onSquarePressed(rectIndex.first, rectIndex.second) | |
} | |
} |
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
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
drawVerticalLines(canvas) | |
drawHorizontalLines(canvas) | |
drawSquareStates(canvas) // <------- here | |
if (touching) { | |
drawHighlightRectangle(canvas) | |
} | |
} | |
private fun drawSquareStates(canvas: Canvas) { |
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
.. | |
highLightPaint.color = ContextCompat.getColor(context, R.color.highlight_color) | |
highLightPaint.style = Paint.Style.FILL | |
highLightPaint.isAntiAlias = true | |
.. | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
drawVerticalLines(canvas) | |
drawHorizontalLines(canvas) |
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
var rectIndex = Pair(0, 0) | |
var touching: Boolean = false | |
.. | |
.. | |
MotionEvent.ACTION_DOWN -> { | |
rectIndex = getRectIndexesFor(x, y) | |
touching = true | |
invalidate(squares[rectIndex.first][rectIndex.second]) | |
} | |
MotionEvent.ACTION_UP -> { |
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
fun getRectIndexesFor(x: Float, y: Float): Pair<Int, Int> { | |
squares.forEachIndexed { | |
i, rects -> | |
for ((j, rect) in rects.withIndex()) { | |
if (rect.contains(x.toInt(), y.toInt())) | |
return Pair(i, j) | |
} | |
} | |
return Pair(-1, -1) // x, y do not lie in our view | |
} |
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
override fun onTouchEvent(event: MotionEvent): Boolean { | |
val x = event.x | |
val y = event.y | |
when (event.action) { | |
MotionEvent.ACTION_DOWN -> { | |
} | |
MotionEvent.ACTION_MOVE -> { | |
} |
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
val X_PARTITION_RATIO = 1 / 3f | |
val Y_PARTITION_RATIO = 1 / 3f | |
... | |
private fun drawVerticalLines(canvas: Canvas) { | |
canvas.drawLine(width * X_PARTITION_RATIO, 0f, width * X_PARTITION_RATIO, height.toFloat(), paint) | |
canvas.drawLine(width * (2 * X_PARTITION_RATIO), 0f, width * (2 * X_PARTITION_RATIO), height.toFloat(), paint) | |
} | |
private fun drawHorizontalLines(canvas: Canvas) { | |
canvas.drawLine(0f, height * Y_PARTITION_RATIO, width.toFloat(), height * Y_PARTITION_RATIO, paint) |
NewerOlder