Last active
May 21, 2018 15:36
-
-
Save divyanshub024/882adf1eaa3a845fba488789e9b9e4b3 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
package com.divyanshu.myapplication | |
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.graphics.Paint | |
import android.graphics.Path | |
import android.util.AttributeSet | |
import android.view.MotionEvent | |
import android.view.View | |
class DrawView(context: Context, attrs: AttributeSet) : View(context, attrs) { | |
private var mPaint = Paint() | |
private var mPath = Path() | |
private var mCurX = 0f | |
private var mCurY = 0f | |
private var mStartX = 0f | |
private var mStartY = 0f | |
init { | |
mPaint.apply { | |
color = Color.BLACK | |
style = Paint.Style.STROKE | |
strokeJoin = Paint.Join.ROUND | |
strokeCap = Paint.Cap.ROUND | |
strokeWidth = 8f | |
isAntiAlias = true | |
} | |
} | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
canvas.drawPath(mPath, mPaint) | |
} | |
private fun actionDown(x: Float, y: Float) { | |
mPath.moveTo(x, y) | |
mCurX = x | |
mCurY = y | |
} | |
private fun actionMove(x: Float, y: Float) { | |
mPath.quadTo(mCurX, mCurY, (x + mCurX) / 2, (y + mCurY) / 2) | |
mCurX = x | |
mCurY = y | |
} | |
private fun actionUp() { | |
mPath.lineTo(mCurX, mCurY) | |
// draw a dot on click | |
if (mStartX == mCurX && mStartY == mCurY) { | |
mPath.lineTo(mCurX, mCurY + 2) | |
mPath.lineTo(mCurX + 1, mCurY + 2) | |
mPath.lineTo(mCurX + 1, mCurY) | |
} | |
} | |
override fun onTouchEvent(event: MotionEvent): Boolean { | |
val x = event.x | |
val y = event.y | |
when (event.action) { | |
MotionEvent.ACTION_DOWN -> { | |
mStartX = x | |
mStartY = y | |
actionDown(x, y) | |
} | |
MotionEvent.ACTION_MOVE -> actionMove(x, y) | |
MotionEvent.ACTION_UP -> actionUp() | |
} | |
invalidate() | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment