android - Get touch location using onTouch and then programmatically touch this place -
i made app gets touch location , want make tap place..
here code:
public class mainactivity extends activity { @override //oncreate protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //gettouch location @override public boolean ontouchevent(motionevent event) { toast.maketext(this, "location located",toast.length_short).show(); int eventaction = event.getaction(); point point = new point(); point.x = (int) event.getx(); point.y = (int) event.gety(); switch (eventaction) { case motionevent.action_down: break; } return false; } } } i want programmatically touch place..
how can ?
you'll need use view register touch listener. chose default relativelayout. i've set delay of callback click 2 seconds.
note need save last simulated touch (simulationevent) , check inside of ontouchlistener if it's not event. without check repeated clicks in same place on , over, simulation call back.
if want simulated touch registered, change ontouchlistener's last return true.
package com.example.virtualclicker; import android.os.handler; import android.os.systemclock; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.util.log; import android.view.motionevent; import android.view.view; import android.widget.relativelayout; /** * created simon on 2014 jul 09. */ public class mainactivity extends actionbaractivity { private motionevent simulationevent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); relativelayout relativelayout = (relativelayout) findviewbyid(r.id.mylayout); relativelayout.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { if (event == simulationevent) return false; int action = event.getaction(); int x = (int)event.getx(); int y = (int)event.gety(); log.e("ontouchlistener", "user touch @ x:" + x + " y:" + y); long length = 0; if (action == motionevent.action_down) { click(v, x, y); } return false; } }); } public void click(final view view, final int x, final int y) { new handler().postdelayed(new runnable() { @override public void run() { // simulation begins here after 2000 millis /// long touchtime = systemclock.uptimemillis(); simulationevent = motionevent.obtain(touchtime, touchtime, motionevent.action_down, x, y, 0); view.dispatchtouchevent(simulationevent); simulationevent = motionevent.obtain(touchtime, touchtime, motionevent.action_up, x, y, 0); view.dispatchtouchevent(simulationevent); log.e("simulatedtouch","simulated touch executed @ x:"+x+" y:"+y); } }, 2000); } } hope works ya.
edit:
layout/activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mylayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"/>
Comments
Post a Comment