区分两个手指旋转和简单的水龙头



我的 fragment i有 textView ,在下面有两个按钮。我需要实现 ontouchlistener 做两件事:

  1. 当我旋转两个手指时, textview 将旋转
  2. 当我点击 textView 时,下面的按钮称为

现在它不起作用,但我找不到更好的解决方案。

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_centerInParent="true"
    tools:context="com.fjord.yalc.MainActivity"
    android:id="@+id/player_fragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/buttonUp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"/>
        <Button
            android:id="@+id/buttonDown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"/>
    </LinearLayout>
    <com.fjord.yalc.AutoResizeTextView
        android:id="@+id/player"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:maxLines="1"
        android:text="@string/normal_start"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"
        android:textColor="@color/colorLightGray"
        android:textSize="300sp"
        android:textStyle="bold"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

java:

Btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.setText(String.valueOf(++life));
            }
        });
        mRotationDetector = new RotationGestureDetector(this);

        TV.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int maskedAction = event.getActionMasked();
                switch (maskedAction) {
                    case MotionEvent.ACTION_DOWN: {
                        return false;
                    }
                    case MotionEvent.ACTION_OUTSIDE: {
                        return false;
                    }
                    case MotionEvent.ACTION_MOVE: {
                        mRotationDetector.onTouchEvent(event);
                        break;
                    }
                }
                return true;
            }
        });

好的,一周后,我找到了答案。我需要使用 dispatchTouchEvent 将我的 MotionEvent 派遣给我的按钮。Action_pointer_down用于检查用户是否使用两个手指。现在,旋转转到 mrotationDetector ,而Taps则转移到 onclicklistener textView player下方的按钮 player。。

java:

player.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                MotionEvent me = MotionEvent.obtain(event);
                mRotationDetector.onTouchEvent(me);
                switch (event.getActionMasked()){
                    case (MotionEvent.ACTION_DOWN):
                        flag=true;
                        break;
                    case(MotionEvent.ACTION_POINTER_DOWN):
                        flag=false;
                        break;
                }
                buttons.dispatchTouchEvent(event);
                return true;
            }
        });

最新更新