Created
July 4, 2024 07:44
-
-
Save imandaliya/21e782402eae936006f896316e4214ca to your computer and use it in GitHub Desktop.
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
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.Paint | |
import android.util.AttributeSet | |
import android.view.View | |
class TransparentGridLinesView @JvmOverloads constructor( | |
context: Context, attrs: AttributeSet? = null, defStyle: Int = 0 | |
) : View(context, attrs, defStyle) { | |
private val blackPaint = Paint().apply { | |
color = 0xFF000000.toInt() // Black color | |
strokeWidth = 2f // Set the width of the lines | |
} | |
private val grayPaint = Paint().apply { | |
color = 0xFF888888.toInt() // Gray color | |
strokeWidth = 2f // Set the width of the lines | |
} | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
val centerX = width / 2f | |
val centerY = height / 2f | |
// Grid spacing | |
val gridSpacing = 50f | |
// Draw vertical lines from center | |
var x = centerX | |
var paint = blackPaint | |
while (x <= width) { | |
canvas.drawLine(x, 0f, x, height.toFloat(), paint) | |
paint = if (paint == blackPaint) grayPaint else blackPaint | |
x += gridSpacing | |
} | |
x = centerX - gridSpacing | |
paint = grayPaint | |
while (x >= 0) { | |
canvas.drawLine(x, 0f, x, height.toFloat(), paint) | |
paint = if (paint == blackPaint) grayPaint else blackPaint | |
x -= gridSpacing | |
} | |
// Draw horizontal lines from center | |
var y = centerY | |
paint = blackPaint | |
while (y <= height) { | |
canvas.drawLine(0f, y, width.toFloat(), y, paint) | |
paint = if (paint == blackPaint) grayPaint else blackPaint | |
y += gridSpacing | |
} | |
y = centerY - gridSpacing | |
paint = grayPaint | |
while (y >= 0) { | |
canvas.drawLine(0f, y, width.toFloat(), y, paint) | |
paint = if (paint == blackPaint) grayPaint else blackPaint | |
y -= gridSpacing | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment