单个按钮android上的多个触摸

时间:2017-06-03 18:21:53

标签: android multi-touch

我有一个带按钮的应用程序,我正在尝试处理多点触控事件。目前,我正在使用触摸式方法并寻找MotionEven.ACTION_DOWNMotionEven.ACTION_UP来跟踪触摸事件。如果同时触摸两个按钮,这可以正常工作。但是,我想让按钮在用单个手指触摸时具有一种行为,并且当用更多手指触摸时具有不同的行为。例如,如果用一根手指触摸一个按钮,该按钮变为红色,如果用两根手指触摸一个按钮,该按钮变为黄色,如果用三根手指触摸一个按钮,它将变为绿色,依此类推。

我遇到的问题是,当我用一个以上的手指触摸按钮时,第二次触摸似乎不会触发事件。如何在同一个按钮上检测多个触摸?

1 个答案:

答案 0 :(得分:0)

这是你想要的基础知识。它可能不完全是你想要的,因为我曾经认识过MotionEvent s的手指之间必须有一些空格。如果两个手指靠近在一起,则ACTION_MOVEMotionEvent。如果你真的需要,你可以解释那些&确定2个或更多手指是否靠近在一起。 MotionEvent

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final int MAX_TOUCHES = 3;

    private View buttonView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        buttonView = findViewById(R.id.button_view);
        buttonView.setOnTouchListener(new View.OnTouchListener() {
            boolean eventConsumed = false;

            // Only handling these 4 events means that there must be some space
            //      between fingers when touching
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.d(TAG, "" + motionEvent.toString());
                switch (motionEvent.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_POINTER_DOWN:
                        setButtonColor(motionEvent.getPointerCount());
                        eventConsumed = true;
                        break;

                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_POINTER_UP:
                        // subtract 1 from the count, this event still includes the touch just removed
                        setButtonColor(motionEvent.getPointerCount() - 1);
                        eventConsumed = true;
                        break;

                    default:
                        break;
                }

                return eventConsumed;
            }
        });
    }

    private void setButtonColor(int count) {
        Log.d(TAG, "count = " + count);

        if(count < 0) {
            count = 0;
        } else if(count > MAX_TOUCHES) {
            count = MAX_TOUCHES;
        }

        switch (count) {
            case 0:
                buttonView.setBackgroundColor(getButtonColor(R.color.colorNotPressed));
                break;

            case 1:
                buttonView.setBackgroundColor(getButtonColor(R.color.colorRed));
                break;

            case 2:
                buttonView.setBackgroundColor(getButtonColor(R.color.colorYellow));
                break;

            case 3:
                buttonView.setBackgroundColor(getButtonColor(R.color.colorGreen));
                break;

            default:
                break;
        }
    }

    // This keeps you from getting a warning about getResources().getColor() being deprecated
    @SuppressWarnings(value = "deprecation")
    private int getButtonColor(int id) {
        int color;

        if(Build.VERSION.SDK_INT >= 23) {
            color = getColor(id);
        } else {
            color = getResources().getColor(id);
        }
        return color;
    }
}

activity_main.xml(不包括AppBar或FAB内容)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    >

    <View
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        android:text="Big Button"
        android:background="@color/colorNotPressed"
        android:id="@+id/button_view"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Big Button"
        android:textSize="20sp"
        />

</RelativeLayout>

将这些文件放在您的colors.xml文件中

<color name="colorNotPressed">#606060</color>
<color name="colorRed">#FF0000</color>
<color name="colorYellow">#FFFF00</color>
<color name="colorGreen">#00FF00</color>