我有一张远程屏幕的图片,希望在点击特定按钮时出现特定事件,这张图片上有一些按钮,当我确切地点击这个图片上的按钮时,发生任何事件,我需要知道这个按钮在图片中的位置,以及不同屏幕上的 X 和 Y 坐标也会发生变化。我知道 `getX()` 和 `getY()` 函数可以用来查找触摸位置,但如何获得图像内的特定位置呢?请尽快帮助我,谢谢!
请查看此图片,当我点击图片中间时将触发某些事件。
请查看此图片,当我点击图片中间时将触发某些事件。
试试这个:
anchorView.getLocationOnScreen(location);
location 是一个数组:
int location[] = new int[2];
anchorView 是你的视图。
因此,你的函数可能如下所示:
public getLocation(View anchorView){
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
}
如果您想在该位置显示不同的视图,请执行以下操作:
yourView.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
还请注意,您可以操作location [0]和location [1]中的值。祝你好运 :)
这里是一个完整的示例,我用它在按钮下方显示片段,此过程在点击按钮时立即发生:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button proceed = (Button)popupView.findViewById(R.id.button1);
Button cancel = (Button)popupView.findViewById(R.id.button2);
cb = (CheckBox)popupView.findViewById(R.id.checkBox1);
proceed.setOnClickListener(onProceed);
cancel.setOnClickListener(onCan);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}