Skip to content

Commit 04f7267

Browse files
authored
Merge pull request #357 from MikeOrtiz/TouchCoordinates
Touch coordinates
2 parents 9425af9 + 1e520db commit 04f7267

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package info.touchimage.demo
2+
3+
import androidx.test.espresso.Espresso.onView
4+
import androidx.test.espresso.matcher.ViewMatchers.withId
5+
import androidx.test.ext.junit.rules.ActivityScenarioRule
6+
import androidx.test.ext.junit.runners.AndroidJUnit4
7+
import com.moka.utils.Screenshot
8+
import info.touchimage.demo.utils.TouchAction
9+
import org.junit.Rule
10+
import org.junit.Test
11+
import org.junit.runner.RunWith
12+
13+
@RunWith(AndroidJUnit4::class)
14+
class TouchTest {
15+
16+
@get:Rule
17+
var activityScenarioRule = ActivityScenarioRule(SingleTouchImageViewActivity::class.java)
18+
19+
@Test
20+
fun testSingleTouch() {
21+
onView(withId(R.id.imageSingle)).perform(TouchAction(4f, 8f))
22+
Screenshot.takeScreenshot("touch1")
23+
onView(withId(R.id.imageSingle)).perform(TouchAction(40f, 80f))
24+
Screenshot.takeScreenshot("touch2")
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package info.touchimage.demo.utils
2+
3+
import android.view.View
4+
import androidx.test.espresso.UiController
5+
import androidx.test.espresso.ViewAction
6+
import androidx.test.espresso.action.MotionEvents
7+
import androidx.test.espresso.matcher.ViewMatchers
8+
import org.hamcrest.Matcher
9+
10+
class TouchAction(private val x: Float, private val y: Float) : ViewAction {
11+
12+
override fun getConstraints(): Matcher<View> = ViewMatchers.isDisplayed()
13+
14+
override fun getDescription() = "Send touch events"
15+
16+
override fun perform(uiController: UiController, view: View) {
17+
// Get view absolute position
18+
val location = IntArray(2)
19+
view.getLocationOnScreen(location)
20+
21+
// Offset coordinates by view position
22+
val coordinates = floatArrayOf(x + location[0], y + location[1])
23+
val precision = floatArrayOf(1f, 1f)
24+
25+
// Send down event, pause, and send up
26+
val down = MotionEvents.sendDown(uiController, coordinates, precision).down
27+
uiController.loopMainThreadForAtLeast(200)
28+
MotionEvents.sendUp(uiController, down, coordinates)
29+
}
30+
}

app/src/main/java/info/touchimage/demo/SingleTouchImageViewActivity.kt

+10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package info.touchimage.demo
22

33
import android.annotation.SuppressLint
4+
import android.graphics.PointF
45
import android.os.Bundle
6+
import android.view.MotionEvent
7+
import android.view.View
58
import androidx.appcompat.app.AppCompatActivity
9+
import com.ortiz.touchview.OnTouchCoordinatesListener
610
import com.ortiz.touchview.OnTouchImageViewListener
711
import info.touchimage.demo.databinding.ActivitySingleTouchimageviewBinding
812
import java.text.DecimalFormat
@@ -38,5 +42,11 @@ class SingleTouchImageViewActivity : AppCompatActivity() {
3842
}
3943
}
4044
)
45+
46+
binding.imageSingle.setOnTouchCoordinatesListener(object: OnTouchCoordinatesListener {
47+
override fun onTouchCoordinate(view: View, event: MotionEvent, bitmapPoint: PointF) {
48+
binding.touchCoordinates.text = "touch coordinates x=${bitmapPoint.x} y=${bitmapPoint.y}"
49+
}
50+
})
4151
}
4252
}

app/src/main/res/layout/activity_single_touchimageview.xml

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
android:layout_width="match_parent"
45
android:layout_height="match_parent">
56

@@ -14,7 +15,6 @@
1415
android:layout_width="wrap_content"
1516
android:layout_height="wrap_content"
1617
android:layout_alignParentTop="true"
17-
android:layout_centerHorizontal="true"
1818
android:layout_marginTop="10dp"
1919
android:background="@android:color/white"
2020
android:padding="5dp">
@@ -23,29 +23,31 @@
2323
android:layout_width="wrap_content"
2424
android:layout_height="wrap_content"
2525
android:paddingEnd="3dp"
26-
android:paddingRight="3dp"
27-
android:text="@string/get_scroll_position" />
26+
android:text="@string/get_scroll_position"
27+
tools:ignore="RtlSymmetry" />
2828

2929
<TextView
3030
android:id="@+id/scroll_position"
3131
android:layout_width="wrap_content"
32-
android:layout_height="wrap_content" />
32+
android:layout_height="wrap_content"
33+
tools:text="scroll_position" />
3334

3435
</LinearLayout>
3536

3637
<LinearLayout
38+
android:id="@+id/second_text_line"
3739
android:layout_width="wrap_content"
3840
android:layout_height="wrap_content"
3941
android:layout_below="@id/top_text_line"
40-
android:layout_centerHorizontal="true"
4142
android:background="@android:color/white"
4243
android:padding="5dp">
4344

4445
<TextView
4546
android:layout_width="wrap_content"
4647
android:layout_height="wrap_content"
47-
android:paddingRight="3dp"
48-
android:text="@string/get_zoomed_rect" />
48+
android:paddingEnd="3dp"
49+
android:text="@string/get_zoomed_rect"
50+
tools:ignore="RtlSymmetry" />
4951

5052
<TextView
5153
android:id="@+id/zoomed_rect"
@@ -54,6 +56,14 @@
5456

5557
</LinearLayout>
5658

59+
<TextView
60+
android:id="@+id/touchCoordinates"
61+
android:layout_width="wrap_content"
62+
android:layout_height="wrap_content"
63+
android:layout_below="@id/second_text_line"
64+
android:background="@android:color/white"
65+
android:padding="5dp" />
66+
5767
<TextView
5868
android:id="@+id/current_zoom"
5969
android:layout_width="wrap_content"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.ortiz.touchview
2+
3+
import android.graphics.PointF
4+
import android.view.MotionEvent
5+
import android.view.View
6+
7+
interface OnTouchCoordinatesListener {
8+
fun onTouchCoordinate(view: View, event: MotionEvent, bitmapPoint: PointF)
9+
}

touchview/src/main/java/com/ortiz/touchview/TouchImageView.kt

+10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
8787
private var prevMatchViewHeight = 0f
8888
private var scaleDetector: ScaleGestureDetector
8989
private var gestureDetector: GestureDetector
90+
private var touchCoordinatesListener: OnTouchCoordinatesListener? = null
9091
private var doubleTapListener: OnDoubleTapListener? = null
9192
private var userTouchListener: OnTouchListener? = null
9293
private var touchImageViewListener: OnTouchImageViewListener? = null
@@ -139,6 +140,10 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
139140
doubleTapListener = onDoubleTapListener
140141
}
141142

143+
fun setOnTouchCoordinatesListener(onTouchCoordinatesListener: OnTouchCoordinatesListener) {
144+
touchCoordinatesListener = onTouchCoordinatesListener
145+
}
146+
142147
override fun setImageResource(resId: Int) {
143148
imageRenderedAtLeastOnce = false
144149
super.setImageResource(resId)
@@ -867,6 +872,11 @@ open class TouchImageView @JvmOverloads constructor(context: Context, attrs: Att
867872
}
868873
}
869874

875+
touchCoordinatesListener?.let {
876+
val bitmapPoint = transformCoordTouchToBitmap(event.x, event.y, true)
877+
it.onTouchCoordinate(v, event, bitmapPoint)
878+
}
879+
870880
imageMatrix = touchMatrix
871881

872882
// User-defined OnTouchListener

0 commit comments

Comments
 (0)