对于不同分辨率的手机,如何获取界面控件的点击点?
/**
*
* @param activity 测试的Activity
* @param view 界面的控件
* @param longClick
* @param time
*/
public static void clickOnScreen(Activity activity,View view, boolean longClick, int time) {
if (view == null)
Assert.assertTrue("View is null and can therefore not be clicked!",false);
int[] xy = new int[2];
view.getLocationOnScreen(xy);
// 获取控件的大小,并且计算出点击的控件中点位置
final int viewWidth = view.getWidth();
final int viewHeight = view.getHeight();
float x = xy[0] + (viewWidth / 2.0f);
float y = xy[1] + (viewHeight / 2.0f);
// 计算缩放比例,将要点击的x, y坐标恢复到缩放前的情况
// Activity activity = activityUtils.getCurrentActivity();
DisplayMetrics rdm = activity.getResources().getDisplayMetrics();
DisplayMetrics wdm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(wdm);
x *= wdm.scaledDensity / rdm.scaledDensity;
y *= wdm.scaledDensity / rdm.scaledDensity;
Log.d("tag","view:"+view+"X-------->" + x + " Y---------->" + y);
// 最后再发送Android点击消息
if (longClick)
solo.clickLongOnScreen(x, y, time);
else
solo.clickOnScreen(x, y);
}
作者: