Giter Site home page Giter Site logo

Comments (10)

chongkaechin avatar chongkaechin commented on July 28, 2024 1

我刚用github不久,还不太会使用,可能要麻烦作者你自己添加一下了。
顺道说一下代码思路之类的吧:

首先要multitouch好像需要override onTouchEvent,然后在选择MotionEvent.obtain的时候选择可以做multitouch的那个函数,
然后点击和提起的顺序需要注意。基本上是这些。我暂时不惯这个贴,有问题可以讨论一下。

from robothelper.

Jinnrry avatar Jinnrry commented on July 28, 2024

你是为了做不同尺寸手机的适配吧?建议代码里面使用相对坐标。比如点击屏幕中心点,你可以写成tap(0.5*MainApplication .sceenWidth ,0.5*MainApplication .sceenHeight)

from robothelper.

chongkaechin avatar chongkaechin commented on July 28, 2024

不是手机适配耶,是真的需要缩放荧幕,里面的场景物件会跟着放大缩小,才可以用模板找图,所以需要先把荧幕画面缩到最小,而有的时候为了精确一点匹配需要放大。虽然作者有提出使用阈值控制的方法,但是我的场景里有许多相似的物件。我的游戏是“部落冲突”。

from robothelper.

Jinnrry avatar Jinnrry commented on July 28, 2024

哈哈。实现了

from robothelper.

chongkaechin avatar chongkaechin commented on July 28, 2024

额,你添加的好像是图片的缩放对吗,我需要的是像是我们使用手机时二指缩放荧幕那种。我附上我尝试的代码可能比较好理解我需要什么。

public static void rescale(boolean toSmaller,int distance,int duration_ms) {
    final int center_X= MainApplication.sceenWidth /2;
    final int center_Y=MainApplication.sceenHeight/2;

    final int interval = 25;

    if(toSmaller)
    {
        final int x1_left = center_X - distance - 20;
        final int x1_right = center_X + distance + 20;
        final int x2_left = center_X - 20;
        final int x2_right = center_X + 20;

        if (Robot.mInst == null) {
            mInst = new Instrumentation();

        }
        Thread thread = new Thread() {
            @Override
            public void run() {
                long downTime=SystemClock.uptimeMillis()+10;
                mInst.sendPointerSync(MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x1_left, center_Y, 0));    //x,y 即是事件的坐标
                mInst.sendPointerSync(MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_POINTER_DOWN, x1_right, center_Y, 0));

                SystemClock.sleep(interval);

                long moveTime=SystemClock.uptimeMillis()+10;
                mInst.sendPointerSync(MotionEvent.obtain(moveTime, moveTime+duration_ms, MotionEvent.ACTION_MOVE, x2_left, center_Y, 0));
                mInst.sendPointerSync(MotionEvent.obtain(moveTime, moveTime+duration_ms, MotionEvent.ACTION_MOVE, x2_right, center_Y, 0));

                SystemClock.sleep(interval);

                long upTime=SystemClock.uptimeMillis()+10;
                mInst.sendPointerSync(MotionEvent.obtain(upTime, upTime, MotionEvent.ACTION_UP, x2_left, center_Y, 0));    //x,y 即是事件的坐标
                mInst.sendPointerSync(MotionEvent.obtain(upTime, upTime, MotionEvent.ACTION_POINTER_UP, x2_right, center_Y, 0));


            }
        };

        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
    else {
        final int x1_left = center_X - 20;
        final int x1_right = center_X + 20;
        final int x2_left = center_X - distance - 20;
        final int x2_right = center_X + distance + 20;

        if (Robot.mInst == null) {
            mInst = new Instrumentation();

        }
        Thread thread = new Thread() {
            @Override
            public void run() {
                long downTime=SystemClock.uptimeMillis()+10;
                mInst.sendPointerSync(MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x1_left, center_Y, 0));    //x,y 即是事件的坐标
                mInst.sendPointerSync(MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_POINTER_DOWN, x1_right, center_Y, 0));

                SystemClock.sleep(interval);

                long moveTime=SystemClock.uptimeMillis()+10;
                mInst.sendPointerSync(MotionEvent.obtain(moveTime, moveTime+duration_ms, MotionEvent.ACTION_MOVE, x2_left, center_Y, 0));
                mInst.sendPointerSync(MotionEvent.obtain(moveTime, moveTime+duration_ms, MotionEvent.ACTION_MOVE, x2_right, center_Y, 0));

                SystemClock.sleep(interval);

                long upTime=SystemClock.uptimeMillis()+10;
                mInst.sendPointerSync(MotionEvent.obtain(upTime, upTime, MotionEvent.ACTION_UP, x2_left, center_Y, 0));    //x,y 即是事件的坐标
                mInst.sendPointerSync(MotionEvent.obtain(upTime, upTime, MotionEvent.ACTION_POINTER_UP, x2_right, center_Y, 0));


            }
        };

        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

from robothelper.

Jinnrry avatar Jinnrry commented on July 28, 2024

额。。你想要的是多点触控,你如果要用Instrumentation实现的话可以看看这篇文章

https://juejin.im/post/6844903575340974087

from robothelper.

chongkaechin avatar chongkaechin commented on July 28, 2024

这是我看了作者你给的网站之后修改的函数,但是会直接停止robothelper,想问一下是我哪里写错了吗

public static void rescale(boolean toSmaller,int distance,int duration_ms) {
final int center_X= MainApplication.sceenWidth /2;
final int center_Y=MainApplication.sceenHeight/2;

    final int interval = 25;

    if(toSmaller)
    {
        final int x1_left = center_X - distance - 20;
        final int x1_right = center_X + distance + 20;
        final int x2_left = center_X - 20;
        final int x2_right = center_X + 20;

        if (Robot.mInst == null) {
            mInst = new Instrumentation();

        }

        MotionEvent.PointerCoords[] pCoord = new MotionEvent.PointerCoords[2];
        MotionEvent.PointerProperties[] pProp = new MotionEvent.PointerProperties[2];
        pCoord[0].pressure = 1;
        pCoord[0].x = x1_left;
        pCoord[0].y =center_Y;
        pCoord[1].pressure = 1;
        pCoord[1].x = x1_right;
        pCoord[1].y =center_Y;

        pProp[0].id=0;
        pProp[1].id=1;
        pProp[0].toolType=1;
        pProp[1].toolType=1;

        Thread thread = new Thread() {
            @Override
            public void run() {
                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_POINTER_DOWN,2,pProp,pCoord,0,1,1,1,0,0,0,0));

                SystemClock.sleep(interval);
                pCoord[0].x = x2_left;
                pCoord[1].x = x2_right;

                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis()+duration_ms, MotionEvent.ACTION_MOVE,2,pProp,pCoord,0,1,1,1,0,0,0,0));

                SystemClock.sleep(interval);

                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_POINTER_UP,2,pProp,pCoord,0,1,1,1,0,0,0,0));


            }
        };

        thread.start();

        try {
            thread.join();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
    else {
        final int x1_left = center_X - 20;
        final int x1_right = center_X + 20;
        final int x2_left = center_X - distance - 20;
        final int x2_right = center_X + distance + 20;

        if (Robot.mInst == null) {
            mInst = new Instrumentation();

        }

        MotionEvent.PointerCoords[] pCoord = new MotionEvent.PointerCoords[2];
        MotionEvent.PointerProperties[] pProp = new MotionEvent.PointerProperties[2];
        pCoord[0].pressure = 1;
        pCoord[0].x = x1_left;
        pCoord[0].y =center_Y;
        pCoord[1].pressure = 1;
        pCoord[1].x = x1_right;
        pCoord[1].y =center_Y;

        pProp[0].id=0;
        pProp[1].id=1;
        pProp[0].toolType=1;
        pProp[1].toolType=1;

        Thread thread = new Thread() {
            @Override
            public void run() {
                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_POINTER_DOWN,2,pProp,pCoord,0,1,1,1,0,0,0,0));

                SystemClock.sleep(interval);
                pCoord[0].x = x2_left;
                pCoord[1].x = x2_right;

                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis()+duration_ms, MotionEvent.ACTION_MOVE,2,pProp,pCoord,0,1,1,1,0,0,0,0));

                SystemClock.sleep(interval);

                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_POINTER_UP,2,pProp,pCoord,0,1,1,1,0,0,0,0));


            }
        };


        thread.start();

        try {
            thread.join();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

from robothelper.

Jinnrry avatar Jinnrry commented on July 28, 2024

我自己尝试了好久也没成功,把找到的一些资料分享出来吧。你弄出来的话提个pr过来吧。

https://github.com/AirtestProject/PocoService.apk/blob/master/pocoservice/src/main/java/com/netease/open/pocoservice/Input.java#L79

https://juejin.im/post/6844903575340974087

https://www.cnblogs.com/season-xie/p/6345320.html

https://stackoverflow.com/questions/5182185/android-testing-how-to-simulate-multitouch-zoom-in-out-using-instruments

https://www.jianshu.com/p/dd70b1645499

https://juejin.im/post/6844903919110324232

https://developer.android.com/reference/android/view/MotionEvent.html#top_of_page

from robothelper.

chongkaechin avatar chongkaechin commented on July 28, 2024

尝试了好久终于成功了。
附上添加了的代码:

import android.app.Activity;
import static android.os.SystemClock.sleep;

public class Robot extends Activity {
private static Instrumentation mInst = null;

@Override public boolean onTouchEvent(MotionEvent event) {

    final int action = event.getAction();
    switch(action){
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_2_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_POINTER_2_UP:
        case MotionEvent.ACTION_UP:
            return true;

        default:
            return false;


    }
public static void zoomInOrOut(boolean zoomIn,int duration_ms) {

    final int center_X = MainApplication.sceenWidth /2;
    final int center_Y = MainApplication.sceenHeight/2;

    MotionEvent.PointerCoords pOneStart=new MotionEvent.PointerCoords();
    pOneStart.pressure=1;
    pOneStart.x = center_X/2;
    pOneStart.y = center_Y;
    pOneStart.size=1;

    MotionEvent.PointerCoords pTwoStart=new MotionEvent.PointerCoords();
    pTwoStart.pressure = 1;
    pTwoStart.x = center_X/2*3;
    pTwoStart.y = center_Y;
    pTwoStart.size=1;

    MotionEvent.PointerCoords pOneEnd=new MotionEvent.PointerCoords();
    pOneEnd.pressure = 1;
    pOneEnd.x = center_X;
    pOneEnd.y = center_Y;
    pOneEnd.size =1;

    MotionEvent.PointerCoords pTwoEnd=new MotionEvent.PointerCoords();
    pTwoEnd.pressure = 1;
    pTwoEnd.x = center_X;
    pTwoEnd.y = center_Y;
    pTwoEnd.size =1;

    MotionEvent.PointerProperties pProp1 = new MotionEvent.PointerProperties();
    pProp1.id=0;
    pProp1.toolType=MotionEvent.TOOL_TYPE_FINGER;

    MotionEvent.PointerProperties pProp2 = new MotionEvent.PointerProperties();
    pProp2.id=1;
    pProp2.toolType=MotionEvent.TOOL_TYPE_FINGER;


    MotionEvent.PointerCoords[] pCordStart = new MotionEvent.PointerCoords[]{pOneStart,pTwoStart};
    MotionEvent.PointerCoords[] pCordEnd = new MotionEvent.PointerCoords[]{pOneEnd,pTwoEnd};
    MotionEvent.PointerProperties[] pProp = new MotionEvent.PointerProperties[]{pProp1,pProp2};


    if (Robot.mInst == null) {
        mInst = new Instrumentation();
    }

    if (zoomIn) {

        MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 25, 
                                           MotionEvent.ACTION_DOWN, 1, pProp, pCordStart, 0, 0, 1, 1, 0, 0, 0, 0);
        mInst.sendPointerSync(event);

        event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 25,                                                                                               
                     MotionEvent.ACTION_POINTER_2_DOWN, 2, pProp, pCordStart, 0, 0, 1, 1, 0, 0, 0, 0);
        mInst.sendPointerSync(event);

        int stepCount=duration_ms/25;
        int stepDistance=(center_X/stepCount)/2;
        for(int i=0;i<stepCount;i++)
        {

            MotionEvent.PointerCoords pOneTemp=new MotionEvent.PointerCoords();
            pOneTemp.pressure = 1;
            pOneTemp.x = (center_X/2)+(i*stepDistance);
            pOneTemp.y = center_Y;
            pOneTemp.size =1;
            MotionEvent.PointerCoords pTwoTemp=new MotionEvent.PointerCoords();
            pTwoTemp.pressure = 1;
            pTwoTemp.x = (center_X/2*3)-(i*stepDistance);
            pTwoTemp.y = center_Y;
            pTwoTemp.size =1;
            MotionEvent.PointerCoords[] pCordTemp = new MotionEvent.PointerCoords[]{pOneTemp,pTwoTemp};


            event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), 
                         MotionEvent.ACTION_MOVE, 2, pProp, pCordTemp, 0, 0, 1, 1, 0, 0, 0,0);
            mInst.sendPointerSync(event);
            sleep(25);
        }


        event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis()+25, 
                     MotionEvent.ACTION_POINTER_2_UP, 2, pProp, pCordEnd, 0, 0, 1, 1, 0, 0, 0,0);
        mInst.sendPointerSync(event);


        event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis()+25, 
                     MotionEvent.ACTION_UP, 1, pProp, pCordEnd, 0, 0, 1, 1, 0, 0, 0,0);
        mInst.sendPointerSync(event);
    }
    else{

        MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 25, 
                                           MotionEvent.ACTION_DOWN, 1, pProp, pCordEnd, 0, 0, 1, 1, 0, 0, 0, 0);
        mInst.sendPointerSync(event);

        event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 25, 
                     MotionEvent.ACTION_POINTER_2_DOWN, 2, pProp, pCordEnd, 0, 0, 1, 1, 0, 0, 0, 0);
        mInst.sendPointerSync(event);

        int stepCount=duration_ms/25;
        int stepDistance=(center_X/stepCount)/2;
        for(int i=0;i<stepCount;i++)
        {

            MotionEvent.PointerCoords pOneTemp=new MotionEvent.PointerCoords();
            pOneTemp.pressure = 1;
            pOneTemp.x = (center_X)-(i*stepDistance);
            pOneTemp.y = center_Y;
            pOneTemp.size =1;
            MotionEvent.PointerCoords pTwoTemp=new MotionEvent.PointerCoords();
            pTwoTemp.pressure = 1;
            pTwoTemp.x = (center_X)+(i*stepDistance);
            pTwoTemp.y = center_Y;
            pTwoTemp.size =1;
            MotionEvent.PointerCoords[] pCordTemp = new MotionEvent.PointerCoords[]{pOneTemp,pTwoTemp};


            event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), 
                         MotionEvent.ACTION_MOVE, 2, pProp, pCordTemp, 0, 0, 1, 1, 0, 0, 0,0);
            mInst.sendPointerSync(event);
            sleep(25);
        }


        event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis()+25, 
                     MotionEvent.ACTION_POINTER_2_UP, 2, pProp, pCordStart, 0, 0, 1, 1, 0, 0, 0,0);
        mInst.sendPointerSync(event);


        event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis()+25,
                     MotionEvent.ACTION_UP, 1, pProp, pCordStart, 0, 0, 1, 1, 0, 0, 0,0);
        mInst.sendPointerSync(event);
    }
}

from robothelper.

Jinnrry avatar Jinnrry commented on July 28, 2024

感谢分享!代码稍微做了点修改,已经合到主分支了

from robothelper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.