56

Androidでは、ユーザーがボタンに触れてこのボタンの領域外にドラッグしたかどうかをどのように検出できますか?

4

9 に答える 9

97

MotionEvent.MOVE_OUTSIDEを確認してください:MotionEvent.MOVEを確認してください:

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

    }
    if(event.getAction() == MotionEvent.ACTION_MOVE){
        if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
            // User moved outside bounds
        }
    }
    return false;
}

注:Android 4.0をターゲットにする場合は、新しい可能性の全世界が開きます:http: //developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER

于 2011-11-09T18:42:34.173 に答える
22

Entrecoによって投稿された回答には、私の場合は少し調整が必要でした。私は代用しなければなりませんでした:

if(!rect.contains((int)event.getX(), (int)event.getY()))

にとって

if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY()))

これは、画面全体ではなく、ImageView自体event.getX()event.getY()のみ適用されるためです。

于 2013-01-04T05:46:23.077 に答える
12

私はOPと同じ問題を抱えていました。それにより、(1)特定のタッチがいつタッチダウンされたか、およびView(2a)ダウンタッチがリリースされたViewとき、または(2b)ダウンタッチがの境界外に移動したときのいずれかを知りたいと思いました。 View。_ このスレッドでさまざまな回答をまとめて、View.OnTouchListener(名前付き)の単純な拡張を作成し、他の人がオブジェクトSimpleTouchListenerをいじる必要がないようにしました。MotionEventこのクラスのソースは、ここまたはこの回答の下部にあります。

このクラスを使用するには、View.setOnTouchListener(View.OnTouchListener)メソッドのパラメーターが次のように設定するだけです。

myView.setOnTouchListener(new SimpleTouchListener() {

    @Override
    public void onDownTouchAction() {
        // do something when the View is touched down
    }

    @Override
    public void onUpTouchAction() {
        // do something when the down touch is released on the View
    }

    @Override
    public void onCancelTouchAction() {
        // do something when the down touch is canceled
        // (e.g. because the down touch moved outside the bounds of the View
    }
});

プロジェクトに追加できるクラスのソースは次のとおりです。

public abstract class SimpleTouchListener implements View.OnTouchListener {

    /**
     * Flag determining whether the down touch has stayed with the bounds of the view.
     */
    private boolean touchStayedWithinViewBounds;

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchStayedWithinViewBounds = true;
                onDownTouchAction();
                return true;

            case MotionEvent.ACTION_UP:
                if (touchStayedWithinViewBounds) {
                    onUpTouchAction();
                }
                return true;

            case MotionEvent.ACTION_MOVE:
                if (touchStayedWithinViewBounds
                        && !isMotionEventInsideView(view, event)) {
                    onCancelTouchAction();
                    touchStayedWithinViewBounds = false;
                }
                return true;

            case MotionEvent.ACTION_CANCEL:
                onCancelTouchAction();
                return true;

            default:
                return false;
        }
    }

    /**
     * Method which is called when the {@link View} is touched down.
     */
    public abstract void onDownTouchAction();

    /**
     * Method which is called when the down touch is released on the {@link View}.
     */
    public abstract void onUpTouchAction();

    /**
     * Method which is called when the down touch is canceled,
     * e.g. because the down touch moved outside the bounds of the {@link View}.
     */
    public abstract void onCancelTouchAction();

    /**
     * Determines whether the provided {@link MotionEvent} represents a touch event
     * that occurred within the bounds of the provided {@link View}.
     *
     * @param view  the {@link View} to which the {@link MotionEvent} has been dispatched.
     * @param event the {@link MotionEvent} of interest.
     * @return true iff the provided {@link MotionEvent} represents a touch event
     * that occurred within the bounds of the provided {@link View}.
     */
    private boolean isMotionEventInsideView(View view, MotionEvent event) {
        Rect viewRect = new Rect(
                view.getLeft(),
                view.getTop(),
                view.getRight(),
                view.getBottom()
        );

        return viewRect.contains(
                view.getLeft() + (int) event.getX(),
                view.getTop() + (int) event.getY()
        );
    }
}
于 2017-03-26T16:09:45.283 に答える
8

OnTouchにログインを追加したところ、MotionEvent.ACTION_CANCELヒットしていることがわかりました。それは私にとって十分です...

于 2015-05-25T14:49:50.660 に答える
3

再利用可能なKotlinソリューション

私は2つのカスタム拡張機能から始めました:

val MotionEvent.up get() = action == MotionEvent.ACTION_UP

fun MotionEvent.isIn(view: View): Boolean {
    val rect = Rect(view.left, view.top, view.right, view.bottom)
    return rect.contains((view.left + x).toInt(), (view.top + y).toInt())
}

次に、ビューのタッチを聞きます。これは、ACTION_DOWNが元々ビューにあった場合にのみ発生します。指を離すと、それがまだビュー上にあるかどうかがチェックされます。

myView.setOnTouchListener { view, motionEvent ->
    if (motionEvent.up && !motionEvent.isIn(view)) {
        // Talk your action here
    }
    false
}
于 2020-01-31T02:13:26.327 に答える
2

ビューがスクロールビュー内にある場合を除いて、上位2つの回答は問題ありません。指を動かしたためにスクロールが発生した場合でも、タッチイベントとして登録されますが、MotionEvent.ACTION_MOVEイベントとしては登録されません。したがって、答えを改善するには(ビューがスクロール要素内にある場合にのみ必要です):

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

    } else if(rect != null && !rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
        // User moved outside bounds
    }
    return false;
}

これをAndroid4.3とAndroid4.4でテストしました

モリッツの答えとトップ2の違いには気づいていませんが、これは彼の答えにも当てはまります。

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

    } else if (rect != null){
        v.getHitRect(rect);
        if(rect.contains(
                Math.round(v.getX() + event.getX()),
                Math.round(v.getY() + event.getY()))) {
            // inside
        } else {
            // outside
        }
    }
    return false;
}
于 2015-04-14T08:05:45.103 に答える
2
view.setClickable(true);
view.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (!v.isPressed()) {
            Log.e("onTouch", "Moved outside view!");
        }
        return false;
    }
});

view.isPressedview.pointInViewいくつかのタッチスロップを使用し、含みます。スロップが必要ない場合は、内部からロジックをコピーするだけですview.pointInView(これは公開されていますが、非表示になっているため、公式APIの一部ではなく、いつでも表示されなくなる可能性があります)。

view.setClickable(true);
view.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            v.setTag(true);
        } else {
            boolean pointInView = event.getX() >= 0 && event.getY() >= 0
                    && event.getX() < (getRight() - getLeft())
                    && event.getY() < (getBottom() - getTop());
            boolean eventInView = ((boolean) v.getTag()) && pointInView;
            Log.e("onTouch", String.format("Dragging currently in view? %b", pointInView));
            Log.e("onTouch", String.format("Dragging always in view? %b", eventInView));
            v.setTag(eventInView);
        }
        return false;
    }
});
于 2015-10-06T22:17:16.027 に答える
1

@FrostRocketからの回答は正しいですが、view.getX()とYを使用して、翻訳の変更も考慮する必要があります。

 view.getHitRect(viewRect);
 if(viewRect.contains(
         Math.round(view.getX() + event.getX()),
         Math.round(view.getY() + event.getY()))) {
   // inside
 } else {
   // outside
 }
于 2014-10-17T10:37:13.223 に答える
1

これは、ユーザーがビューの外に指を置いている間に送信されたView.OnTouchListenerかどうかを確認するために使用できるものです。MotionEvent.ACTION_UP

private OnTouchListener mOnTouchListener = new View.OnTouchListener() {

    private Rect rect;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v == null) return true;
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            return true;
        case MotionEvent.ACTION_UP:
            if (rect != null
                    && !rect.contains(v.getLeft() + (int) event.getX(),
                        v.getTop() + (int) event.getY())) {
                // The motion event was outside of the view, handle this as a non-click event

                return true;
            }
            // The view was clicked.
            // TODO: do stuff
            return true;
        default:
            return true;
        }
    }
};
于 2015-01-19T09:04:52.377 に答える