在视图上有效使用触摸/悬停坐标

时间:2015-05-05 01:50:08

标签: android operating-system screen coordinate

在Android手机上,我可以获得用户触摸他/她的手机屏幕或设备的坐标信息。那么我如何使用/传递这些X-Y坐标来运行我的特定函数,并在我的代码中使用它

2 个答案:

答案 0 :(得分:0)

来自post的参考,如果用户触摸了您应用中的屏幕,您可以获得坐标。在触摸侦听器上实现后,然后在if语句MotionEvent.ACTION_UP中将其放在那里

 float myX = event.getX();
 float myY = event.getY();
 // now you can then pass myX,myY in your method or function as parameters

如果您恰好定位Api 14+,那么您可以使用onhover使用与该帖子中第一个相同的方法,而不是使用setOnTouchListener使用getWindow().getDecorView().setOnHoverListener()那是关于它的。

旁注是使用第一个更好,因为onHover无法信任工作,实际上它对我不起作用,那就是btw

希望我回答你的问题。

答案 1 :(得分:0)

您可以通过扩展android视图和覆盖onTouchEvent方法来创建自定义视图。例如,

public class NotTouchableWebView extends WebView {
private Context context;

public NotTouchableWebView(Context context) {
    super(context);
    this.context=context;
}

public NotTouchableWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context=context;
}

public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context=context;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public NotTouchableWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    this.context=context;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (some condition){
        //your specific function
        return false;
    }
    return super.onTouchEvent(event);
}

}