2017-06-03 79 views
1

我有一個按鈕的應用程序,我正在嘗試處理多點觸控事件。目前我正在使用觸摸方式,並尋找MotionEven.ACTION_DOWNMotionEven.ACTION_UP來跟蹤觸摸事件。如果同時觸摸兩個按鈕,這可以正常工作。但是,我想讓這些按鈕在用單個手指觸摸時具有一種行爲,並且在用更多的手指觸摸時具有不同的行爲。例如,如果您用一個手指觸摸該按鈕變爲紅色的按鈕,如果用2個手指觸摸該按鈕變爲黃色的按鈕,如果用3個手指觸摸按鈕,則變爲綠色等等。在單個按鈕上的多個觸摸android

我遇到的問題是,當我用多個手指觸摸按鈕時,第二次觸摸似乎不會觸發事件。我如何檢測同一按鈕上的多個觸摸?

+0

HTTPS:/ /github.com/devunwired/custom-touch-examples這個存儲庫可能會有所幫助。 – Raghunandan

+0

我在找哪個確切的例子? – user381261

回答

0

這是你想要的東西的基礎知識。它可能並不完全是你想要的,因爲手指之間必須有一些空間,我曾經認識到這一點。如果兩個手指靠在一起,它將ACTION_MOVE作爲MotionEvent。如果你真的需要,你可能會解釋這些&確定是否有兩個或更多的手指緊靠在一起。 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>