Skip to content

Commit

Permalink
Merge pull request #357 from MikeOrtiz/TouchCoordinates
Browse files Browse the repository at this point in the history
Touch coordinates
  • Loading branch information
hannesa2 authored May 10, 2021
2 parents 9425af9 + 1e520db commit 04f7267
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
27 changes: 27 additions & 0 deletions app/src/androidTest/java/info/touchimage/demo/TouchTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package info.touchimage.demo

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.moka.utils.Screenshot
import info.touchimage.demo.utils.TouchAction
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TouchTest {

@get:Rule
var activityScenarioRule = ActivityScenarioRule(SingleTouchImageViewActivity::class.java)

@Test
fun testSingleTouch() {
onView(withId(R.id.imageSingle)).perform(TouchAction(4f, 8f))
Screenshot.takeScreenshot("touch1")
onView(withId(R.id.imageSingle)).perform(TouchAction(40f, 80f))
Screenshot.takeScreenshot("touch2")
}

}
30 changes: 30 additions & 0 deletions app/src/androidTest/java/info/touchimage/demo/utils/TouchAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package info.touchimage.demo.utils

import android.view.View
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.MotionEvents
import androidx.test.espresso.matcher.ViewMatchers
import org.hamcrest.Matcher

class TouchAction(private val x: Float, private val y: Float) : ViewAction {

override fun getConstraints(): Matcher<View> = ViewMatchers.isDisplayed()

override fun getDescription() = "Send touch events"

override fun perform(uiController: UiController, view: View) {
// Get view absolute position
val location = IntArray(2)
view.getLocationOnScreen(location)

// Offset coordinates by view position
val coordinates = floatArrayOf(x + location[0], y + location[1])
val precision = floatArrayOf(1f, 1f)

// Send down event, pause, and send up
val down = MotionEvents.sendDown(uiController, coordinates, precision).down
uiController.loopMainThreadForAtLeast(200)
MotionEvents.sendUp(uiController, down, coordinates)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package info.touchimage.demo

import android.annotation.SuppressLint
import android.graphics.PointF
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.ortiz.touchview.OnTouchCoordinatesListener
import com.ortiz.touchview.OnTouchImageViewListener
import info.touchimage.demo.databinding.ActivitySingleTouchimageviewBinding
import java.text.DecimalFormat
Expand Down Expand Up @@ -38,5 +42,11 @@ class SingleTouchImageViewActivity : AppCompatActivity() {
}
}
)

binding.imageSingle.setOnTouchCoordinatesListener(object: OnTouchCoordinatesListener {
override fun onTouchCoordinate(view: View, event: MotionEvent, bitmapPoint: PointF) {
binding.touchCoordinates.text = "touch coordinates x=${bitmapPoint.x} y=${bitmapPoint.y}"
}
})
}
}
24 changes: 17 additions & 7 deletions app/src/main/res/layout/activity_single_touchimageview.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

Expand All @@ -14,7 +15,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:padding="5dp">
Expand All @@ -23,29 +23,31 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="3dp"
android:paddingRight="3dp"
android:text="@string/get_scroll_position" />
android:text="@string/get_scroll_position"
tools:ignore="RtlSymmetry" />

<TextView
android:id="@+id/scroll_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
tools:text="scroll_position" />

</LinearLayout>

<LinearLayout
android:id="@+id/second_text_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/top_text_line"
android:layout_centerHorizontal="true"
android:background="@android:color/white"
android:padding="5dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="3dp"
android:text="@string/get_zoomed_rect" />
android:paddingEnd="3dp"
android:text="@string/get_zoomed_rect"
tools:ignore="RtlSymmetry" />

<TextView
android:id="@+id/zoomed_rect"
Expand All @@ -54,6 +56,14 @@

</LinearLayout>

<TextView
android:id="@+id/touchCoordinates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/second_text_line"
android:background="@android:color/white"
android:padding="5dp" />

<TextView
android:id="@+id/current_zoom"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ortiz.touchview

import android.graphics.PointF
import android.view.MotionEvent
import android.view.View

interface OnTouchCoordinatesListener {
fun onTouchCoordinate(view: View, event: MotionEvent, bitmapPoint: PointF)
}
10 changes: 10 additions & 0 deletions touchview/src/main/java/com/ortiz/touchview/TouchImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
private var prevMatchViewHeight = 0f
private var scaleDetector: ScaleGestureDetector
private var gestureDetector: GestureDetector
private var touchCoordinatesListener: OnTouchCoordinatesListener? = null
private var doubleTapListener: OnDoubleTapListener? = null
private var userTouchListener: OnTouchListener? = null
private var touchImageViewListener: OnTouchImageViewListener? = null
Expand Down Expand Up @@ -139,6 +140,10 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
doubleTapListener = onDoubleTapListener
}

fun setOnTouchCoordinatesListener(onTouchCoordinatesListener: OnTouchCoordinatesListener) {
touchCoordinatesListener = onTouchCoordinatesListener
}

override fun setImageResource(resId: Int) {
imageRenderedAtLeastOnce = false
super.setImageResource(resId)
Expand Down Expand Up @@ -867,6 +872,11 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
}
}

touchCoordinatesListener?.let {
val bitmapPoint = transformCoordTouchToBitmap(event.x, event.y, true)
it.onTouchCoordinate(v, event, bitmapPoint)
}

imageMatrix = touchMatrix

// User-defined OnTouchListener
Expand Down

0 comments on commit 04f7267

Please sign in to comment.