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

Add zoomable(pinch zoom) feature to the crop activity #170

Open
wants to merge 2 commits into
base: master
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
33 changes: 18 additions & 15 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.sample.edgedetection.EdgeDetectionHandler
import com.sample.edgedetection.R
import com.sample.edgedetection.base.BaseActivity
import com.sample.edgedetection.view.PaperRectangle
import com.sample.edgedetection.view.ZoomableImageView

class CropActivity : BaseActivity(), ICropView.Proxy {

Expand Down Expand Up @@ -46,7 +47,7 @@ class CropActivity : BaseActivity(), ICropView.Proxy {
}
}

override fun getPaper(): ImageView = findViewById(R.id.paper)
override fun getPaper(): ZoomableImageView = findViewById(R.id.paper)

override fun getPaperRect() = findViewById<PaperRectangle>(R.id.paper_rect)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package com.sample.edgedetection.crop

import android.widget.ImageView
import com.sample.edgedetection.view.PaperRectangle
import com.sample.edgedetection.view.ZoomableImageView



/**
* Created by pengke on 15/09/2017.
*/
class ICropView {
interface Proxy {
fun getPaper(): ImageView
fun getPaper(): ZoomableImageView
fun getPaperRect(): PaperRectangle
fun getCroppedPaper(): ImageView
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,50 @@ class PaperRectangle : View {
}
}

override fun onTouchEvent(event: MotionEvent?): Boolean {

override fun onTouchEvent(event: MotionEvent?): Boolean {
if (!cropMode) {
return false
}
when (event?.action) {

val touchX = event?.x ?: return false
val touchY = event.y

when (event.action) {
MotionEvent.ACTION_DOWN -> {
latestDownX = event.x
latestDownY = event.y
calculatePoint2Move(event.x, event.y)

if (isCornerTouched(touchX, touchY)) {
latestDownX = touchX
latestDownY = touchY
calculatePoint2Move(touchX, touchY)
return true
}
}
MotionEvent.ACTION_MOVE -> {
point2Move.x = (event.x - latestDownX) + point2Move.x
point2Move.y = (event.y - latestDownY) + point2Move.y
movePoints()
latestDownY = event.y
latestDownX = event.x
if (point2Move.x != 0.0 && point2Move.y != 0.0) {
point2Move.x += (touchX - latestDownX)
point2Move.y += (touchY - latestDownY)
movePoints()
latestDownX = touchX
latestDownY = touchY
return true
}
}
}

return false
}

private fun isCornerTouched(touchX: Float, touchY: Float): Boolean {
val cornerRadius = 20F
val corners = listOf(tl, tr, br, bl)
for (corner in corners) {
val distance = Math.sqrt(Math.pow((touchX - corner.x), 2.0) + Math.pow((touchY - corner.y), 2.0))
if (distance <= cornerRadius) {
return true
}
}
return true
return false
}

private fun calculatePoint2Move(downX: Float, downY: Float) {
Expand Down
Loading