获取触摸位置使用onTouch,然后以编程触摸这个地方 [英] Get touch location using onTouch and then programmatically touch this place

查看:191
本文介绍了获取触摸位置使用onTouch,然后以编程触摸这个地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个应用程序,获得了触摸位置,现在我希望把它挖掘这个地方。

I made an app that gets the touch location and now I want to make it tap this place..

下面是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 only want it to programmatically touch this place..

我该怎么办呢?

推荐答案

您将需要使用一些观点来注册你的触摸监听器。我选择的是默认的 RelativeLayout的。我给自己定了回调的延迟点击为2秒。

You'll need to use some view to register your touch listener. I chose the default RelativeLayout. I've set the delay of the callback click to be 2 seconds.

请注意,你需要保存最后模拟触摸( simulationEvent ),并检查里面的 onTouchListener ,如果是没有该事件。如果没有这个检查,你会得到一遍又一遍地重复点击在同一个地方,作为模拟会叫自己回来了。

Note that you need to save the last simulated touch (simulationEvent) and check inside of your onTouchListener if it's not that event. Without this check you would get repeated clicks in the same place over and over, as the simulation would call itself back.

如果您只想模拟触摸要登记,修改 onTouchListener 的最后回归到

If you want only the simulated touch to be registered, change the onTouchListener's last return to 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 by 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 at 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 at X:"+x+" Y:"+y);
            }
        }, 2000);
    }

}

希望这适用于你。

Hope this works for ya.

编辑:

布局/ 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"/>

这篇关于获取触摸位置使用onTouch,然后以编程触摸这个地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆