ZawaWorks’s diary

プログラミング技術メモ

Android×Processing:adb input touchscreen編

はじめに

 この記事ではProcessingを使ってAndroid端末を操作しようと思います。

環境設定

'input touchscreen'

 タッチや直線のスワイプといった単純な操作にはinput touchscreenを使います

タッチの場合
  • adb shell input touchscreen tap x y
  • 画面上の座標の(x,y)にタップする命令
  • xとyは10進数
スワイプの場合
  • adb shell input touchscreen swipe x1 y1 x2 y2 t
    • (x1,y1)から(x2,y2)までをスワイプする命令
    • tはミリ秒 f:id:ZawaWorks:20171029022807p:plain

Processingとの連帯

今回使う命令
launch()
例:adb shell input touchscreen tap x y
String [] AdbTap = {"adb","shell","input","touchscreen","tap",str(x),str(y)};//xとyはString型に変換


launch(AdbTap);
input tapの場合

コード

String [] AdbTap = {"adb", "shell", "input", "touchscreen", "tap", "", ""};
  
  void setup() {
    size(360, 640);//Xperoa XZの解像度は 1080 x 1920
    background(0, 0, 255);
  }
  
  void draw() {
    if (!mousePressed) return;
  
    fill(-1);
    ellipse(mouseX, mouseY, 15, 15);
  
    AdbTap[5] = str(mouseX*3);
    AdbTap[6] = str(mouseY*3);
  
    launch(AdbTap);
  }

デモ

youtu.be

input Swipeの場合

コード

ArrayList<PVector>vector = new ArrayList();
Boolean launch_able = true;

int time = 100;

void setup() {
  size(360, 640);
  background(0, 0, 255);
}

void draw() {
  background(0, 0, 255);

  for (int i=0; i<vector.size(); i++) {
    PVector v = vector.get(i);

    fill(-1);
    ellipse(v.x, v.y, 20, 20);
  }

  if (vector.size() != 2) return;

  PVector v1 = vector.get(0);
  PVector v2 = vector.get(1);
  stroke(-1);
  line(v1.x, v1.y, v2.x, v2.y);

  if (!launch_able)return;
  launch("adb shell input touchscreen swipe"+" "+ v1.x+" "+ v1.y+" "+v2.x+" "+v2.y+" "+time);
  launch_able = false;
}

void mousePressed() {
  if (vector.size()%2 == 0) {
    vector = new ArrayList();
    launch_able = true;
  }

  vector.add(new PVector(mouseX, mouseY));
}


デモ

youtu.be - t = 100のときとt = 50のときを比較

GitHub

github.com

参考資料

www.excite.co.jp