? ? 在某些情况下,需要在程序模拟一些系统事件(如键盘、触摸输入事件),完成远程控制或进行软件测试等工作,本文介绍了生成系统触摸事件并在程序中接收处理该事件。 ? ? 1、生成一个android测试工程,本文工程名叫myTouch。 ? ? 2、在工程中增加一个新的View类,类名myView,文件名myView.java,文件内容如下: ?
package com.mytouch;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Administrator on 2022/2/24.
*/
public class myView extends View implements View.OnTouchListener{
private Paint paint;
private Rect rect;
private int left;
private int top;
private int right;
private int bottom;
public myView (Context context) {
super(context);
initPaint();
setOnTouchListener(this);
}
public myView (Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
setOnTouchListener(this);
}
public myView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
setOnTouchListener(this);
}
private void initPaint() {
paint=new Paint();
left=100;
top=100;
right=300;
bottom=300;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
rect=new Rect(left,top,right,bottom);
canvas.drawRect(rect,paint);
}
????@Override
public boolean onTouch(View v,MotionEvent e) {
int action=e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("dhl", "down axisX:" + e.getX());
Log.d("dhl", "down axisY:" + e.getY());
break;
case MotionEvent.ACTION_UP:
Log.d("dhl", "up axisX:" + e.getX());
Log.d("dhl", "up axisY:" + e.getY());
break;
}
return false;
}
}
3、修改程序布局文件activity_main.xml,增加一个按钮,一个上面定义的myView控件,修改后的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mytouch.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"/>
<Button
android:layout_width="300dp"
android:layout_height="100dp"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"/>
<com.mytouch.myView
android:id="@+id/custom_view"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_below="@+id/button"
android:background="#3F51B5"/>
</RelativeLayout>
4、在按钮中发送触摸事件,此处用到andriod的input命令,通过Runtime类执行该命令,发送触摸事件,程序如下:
bt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Process process=null;
Process process1=null;
try {
for (int i=0;i<5;i++) {
process = Runtime.getRuntime().exec("input tap 200 300");
Log.d("dhl","send touch event");
}
/*
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream()), 1024);
String line = bufferedReader.readLine();
while ( line != null) {
Log.d("dhl","******:"+line);
}
*/
} catch ( IOException e) {
e.printStackTrace();
}
return false;
}
});
5、点击按钮,则自动向系统中发送了ACTION_DOWN的触摸事件,在myView的触摸事件响应函数中会接收到发送的ACTION_DOWN事件,注意发送事件的坐标是屏幕的物理坐标,坐标经过转换后应该落在myView之内,否则myView将接收不到该事件。
6、关于input命令可参考Android adb input 命令介绍_Lincoln 的专栏-CSDN博客_adb input textAndroid adb input 命令介绍(有些功能很像按键精灵的……研究一下也许能写按键精灵……233333)https://blog.csdn.net/soslinken/article/details/49587497
|