Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

没有可以进行荧幕缩放的函数 #13

Closed
chongkaechin opened this issue Aug 21, 2020 · 10 comments
Closed

没有可以进行荧幕缩放的函数 #13

chongkaechin opened this issue Aug 21, 2020 · 10 comments

Comments

@chongkaechin
Copy link

目前我写的脚本有需要做到荧幕的缩放,但是使用现有的函数比如down,up,moveTo来重写一个都没办法做到。因为对android及java其实不是很熟悉,所以希望作者可以写一下实现的方法,谢谢。

@Jinnrry
Copy link
Owner

Jinnrry commented Aug 22, 2020

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

@chongkaechin
Copy link
Author

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

@Jinnrry Jinnrry mentioned this issue Aug 22, 2020
@Jinnrry
Copy link
Owner

Jinnrry commented Aug 22, 2020

哈哈。实现了

@chongkaechin
Copy link
Author

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

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();
        }
    }

}

@Jinnrry
Copy link
Owner

Jinnrry commented Aug 22, 2020

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

https://juejin.im/post/6844903575340974087

@chongkaechin
Copy link
Author

这是我看了作者你给的网站之后修改的函数,但是会直接停止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();
        }
    }

}

@chongkaechin
Copy link
Author

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

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);
    }
}

@chongkaechin
Copy link
Author

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

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

@Jinnrry
Copy link
Owner

Jinnrry commented Sep 6, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants