diff --git a/app/src/androidTest/java/info/touchimage/demo/TouchTest.kt b/app/src/androidTest/java/info/touchimage/demo/TouchTest.kt new file mode 100644 index 00000000..74cdaf57 --- /dev/null +++ b/app/src/androidTest/java/info/touchimage/demo/TouchTest.kt @@ -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") + } + +} diff --git a/app/src/androidTest/java/info/touchimage/demo/utils/TouchAction.kt b/app/src/androidTest/java/info/touchimage/demo/utils/TouchAction.kt new file mode 100644 index 00000000..45cd65f4 --- /dev/null +++ b/app/src/androidTest/java/info/touchimage/demo/utils/TouchAction.kt @@ -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 = 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) + } +} diff --git a/app/src/main/java/info/touchimage/demo/SingleTouchImageViewActivity.kt b/app/src/main/java/info/touchimage/demo/SingleTouchImageViewActivity.kt index 2a85e5d0..8fa736ae 100644 --- a/app/src/main/java/info/touchimage/demo/SingleTouchImageViewActivity.kt +++ b/app/src/main/java/info/touchimage/demo/SingleTouchImageViewActivity.kt @@ -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 @@ -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}" + } + }) } } diff --git a/app/src/main/res/layout/activity_single_touchimageview.xml b/app/src/main/res/layout/activity_single_touchimageview.xml index f64b1c48..cf7a29df 100644 --- a/app/src/main/res/layout/activity_single_touchimageview.xml +++ b/app/src/main/res/layout/activity_single_touchimageview.xml @@ -1,5 +1,6 @@ @@ -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"> @@ -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" /> + android:layout_height="wrap_content" + tools:text="scroll_position" /> + android:paddingEnd="3dp" + android:text="@string/get_zoomed_rect" + tools:ignore="RtlSymmetry" /> + +