2

是否可以找到我单击布局的位置?我有一个很大的 ScrollView,我想知道我点击了哪里。我的屏幕尺寸是 854x480,但 ScrollView 有 2000x480。如何找到我点击的坐标位置?

4

3 回答 3

1

在活动课上。

import android.view.MotionEvent;

...

@Override
public boolean onTouchEvent(MotionEvent e)
    {

    switch (e.getAction()) 
            {

    case MotionEvent.ACTION_DOWN: 
        float cordinate_X = e.getRawX(); 
        float cordinate_Y = e.getRawY();
        break;
    }
    return true;
}
于 2012-05-17T14:16:39.523 回答
0

你为什么不创建自定义监听器我下面的代码可以帮助你

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class ObservableScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        // if (scrollViewListener != null) {
        // scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        // }
    }

    private boolean mScrollable = true;

    public void setScrollingEnabled(boolean enabled) {
        mScrollable = enabled;
    }

    public boolean isScrollable() {
        return mScrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_MOVE:
            // if we can scroll pass the event to the superclass
            if (mScrollable)
                return super.onTouchEvent(ev);
            // only continue to handle the touch event if scrolling enabled
            return mScrollable; // mScrollable is always false at this point
        default:
            return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if
        // we are not scrollable
        if (!mScrollable)
            return false;
        else
            return super.onInterceptTouchEvent(ev);
    }

}
于 2013-12-07T06:53:48.797 回答
0

我使用以下方法建立了在细长滚动视图上单击的坐标。

float yScrollPosition = event.getY();
DisplayMetrics metrics = new DisplayMetrics();
float logicalDensity = metrics.density;
float yPosition = yScrollPosition / logicalDensity;
于 2016-02-03T07:19:49.287 回答