【问题标题】:Effective use of touch/hover co-ordinates on a View在视图上有效使用触摸/悬停坐标
【发布时间】:2015-05-05 01:50:07
【问题描述】:

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

【问题讨论】:

  • 例如:用户想在 (20, 20) 位置触摸屏幕。我不会在屏幕上按 (20, 20) real ,但是我想使用 (20, 20) 作为函数的参数来让操作系统知道我想按 (20, 20) 然后 APP 将像这样运行活动我按 (20, 20)。
  • 我重写了您的问题,请检查它是否适合您的需求,或者检查我是否最感兴趣。对于您的问题,我将尝试发布答案

标签: android operating-system screen coordinate


【解决方案1】:

参考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 不能被信任工作,实际上它对我不起作用,只是顺便说一句

希望我回答了你的问题。

【讨论】:

    【解决方案2】:

    您可以通过扩展 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);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 2016-09-21
      相关资源
      最近更新 更多