Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

메모장 줌 기능 추가 #98

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,46 @@ class PainterView @JvmOverloads constructor(
}

override fun onTouchEvent(event: MotionEvent): Boolean {
val x = event.x
val y = event.y

when (event.action) {
MotionEvent.ACTION_DOWN -> {
if (tool == PEN) {
path = Path().apply {
moveTo(x, y)
paths.add(this)
listener?.onPainted(paths)
if (event.pointerCount == 1) {
val x = event.x
val y = event.y

when (event.action) {
MotionEvent.ACTION_DOWN -> {
if (tool == PEN) {
path = Path().apply {
moveTo(x, y)
paths.add(this)
listener?.onPainted(paths)
}
} else {
erase(x, y)
}
} else {
erase(x, y)
}
}

MotionEvent.ACTION_MOVE -> {
if (tool == PEN) {
path?.lineTo(x, y)
} else {
erase(x, y)
MotionEvent.ACTION_MOVE -> {
if (tool == PEN) {
path?.lineTo(x, y)
} else {
erase(x, y)
}
}
}

MotionEvent.ACTION_UP -> {
path?.let {
paths.add(it)
listener?.onPainted(paths)
MotionEvent.ACTION_UP -> {
path?.let {
paths.add(it)
listener?.onPainted(paths)
}
path = null
}
path = null
}

else -> return false
else -> return false
}
invalidate()
} else if (event.pointerCount == 2) {
return false
}
invalidate()

return true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.nextroom.nextroom.presentation.customview

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.ScaleGestureDetector.OnScaleGestureListener
import android.view.View
import android.widget.FrameLayout
import kotlin.math.max
import kotlin.math.min
import kotlin.math.sign


class ZoomLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : FrameLayout(context, attrs, defStyle), OnScaleGestureListener {

private var scale = 1.0f
private var lastScaleFactor = 0f
private val scaleDetector by lazy { ScaleGestureDetector(context, this) }

override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
scaleDetector.onTouchEvent(ev)
applyScaleAndTranslation()
return false
}

override fun onScale(scaleDetector: ScaleGestureDetector): Boolean {
val scaleFactor = scaleDetector.scaleFactor
if (lastScaleFactor == 0f || (sign(scaleFactor.toDouble()) == sign(lastScaleFactor.toDouble()))) {
scale *= scaleFactor
scale = max(MIN_ZOOM.toDouble(), min(scale.toDouble(), MAX_ZOOM.toDouble())).toFloat()
lastScaleFactor = scaleFactor
} else {
lastScaleFactor = 0f
}

return true
}

override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
return true
}

override fun onScaleEnd(scaleDetector: ScaleGestureDetector) {}

private fun applyScaleAndTranslation() {
child().scaleX = scale
child().scaleY = scale
}

private fun child(): View {
return getChildAt(0)
}

companion object {
private const val MIN_ZOOM = 1.0f
private const val MAX_ZOOM = 8.0f
}
}
11 changes: 8 additions & 3 deletions presentation/src/main/res/layout/fragment_memo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
android:id="@+id/tb_memo"
layout="@layout/common_toolbar" />

<com.nextroom.nextroom.presentation.customview.PainterView
android:id="@+id/custom_painter_view"
<com.nextroom.nextroom.presentation.customview.ZoomLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tb_memo" />
app:layout_constraintTop_toBottomOf="@id/tb_memo">

<com.nextroom.nextroom.presentation.customview.PainterView
android:id="@+id/custom_painter_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.nextroom.nextroom.presentation.customview.ZoomLayout>

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guide_end"
Expand Down