mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-06-05 21:59:17 +02:00
fix #81, allow zooming canvas
This commit is contained in:
@ -6,6 +6,7 @@ import android.graphics.*
|
|||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.view.MotionEvent
|
import android.view.MotionEvent
|
||||||
|
import android.view.ScaleGestureDetector
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import com.bumptech.glide.Glide
|
import com.bumptech.glide.Glide
|
||||||
import com.bumptech.glide.load.DecodeFormat
|
import com.bumptech.glide.load.DecodeFormat
|
||||||
@ -43,6 +44,11 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
private var mIsStrokeWidthBarEnabled = false
|
private var mIsStrokeWidthBarEnabled = false
|
||||||
private var mIsEraserOn = false
|
private var mIsEraserOn = false
|
||||||
private var mBackgroundColor = 0
|
private var mBackgroundColor = 0
|
||||||
|
private var mCenter: PointF? = null
|
||||||
|
|
||||||
|
private var mScaleDetector: ScaleGestureDetector? = null
|
||||||
|
private var mScaleFactor = 1f
|
||||||
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
mPaint.apply {
|
mPaint.apply {
|
||||||
@ -54,6 +60,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
isAntiAlias = true
|
isAntiAlias = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mScaleDetector = ScaleGestureDetector(context, ScaleListener())
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,6 +180,13 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
|
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
|
canvas.save()
|
||||||
|
|
||||||
|
if (mCenter == null) {
|
||||||
|
mCenter = PointF(width / 2f, height / 2f)
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.scale(mScaleFactor, mScaleFactor, mCenter!!.x, mCenter!!.y)
|
||||||
|
|
||||||
if (mBackgroundBitmap != null) {
|
if (mBackgroundBitmap != null) {
|
||||||
val left = (width - mBackgroundBitmap!!.width) / 2
|
val left = (width - mBackgroundBitmap!!.width) / 2
|
||||||
@ -191,6 +205,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
if (mIsStrokeWidthBarEnabled && !mIsSaving) {
|
if (mIsStrokeWidthBarEnabled && !mIsSaving) {
|
||||||
drawPreviewCircle(canvas)
|
drawPreviewCircle(canvas)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canvas.restore()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawPreviewCircle(canvas: Canvas) {
|
private fun drawPreviewCircle(canvas: Canvas) {
|
||||||
@ -250,7 +266,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
mPath.lineTo(mCurX + 1, mCurY)
|
mPath.lineTo(mCurX + 1, mCurY)
|
||||||
}
|
}
|
||||||
|
|
||||||
mPaths.put(mPath, mPaintOptions)
|
mPaths[mPath] = mPaintOptions
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
mPath = MyPath()
|
mPath = MyPath()
|
||||||
mPaintOptions = PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth, mPaintOptions.isEraser)
|
mPaintOptions = PaintOptions(mPaintOptions.color, mPaintOptions.strokeWidth, mPaintOptions.isEraser)
|
||||||
@ -261,6 +277,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||||
|
mScaleDetector!!.onTouchEvent(event)
|
||||||
val x = event.x
|
val x = event.x
|
||||||
val y = event.y
|
val y = event.y
|
||||||
|
|
||||||
@ -272,8 +289,13 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
mUndonePaths.clear()
|
mUndonePaths.clear()
|
||||||
mListener?.toggleRedoVisibility(false)
|
mListener?.toggleRedoVisibility(false)
|
||||||
}
|
}
|
||||||
MotionEvent.ACTION_MOVE -> actionMove(x, y)
|
|
||||||
MotionEvent.ACTION_UP -> actionUp()
|
MotionEvent.ACTION_MOVE -> {
|
||||||
|
if (!mScaleDetector!!.isInProgress && event.pointerCount == 1) {
|
||||||
|
actionMove(x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> actionUp()
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidate()
|
invalidate()
|
||||||
@ -297,4 +319,13 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
|
|||||||
mPaths = state.paths
|
mPaths = state.paths
|
||||||
pathsUpdated()
|
pathsUpdated()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
||||||
|
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
||||||
|
mScaleFactor *= detector.scaleFactor
|
||||||
|
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f))
|
||||||
|
invalidate()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user