在单个按钮上的多个触摸android
问题描述:
我有一个按钮的应用程序,我正在尝试处理多点触控事件。目前我正在使用触摸方式,并寻找MotionEven.ACTION_DOWN
和MotionEven.ACTION_UP
来跟踪触摸事件。如果同时触摸两个按钮,这可以正常工作。但是,我想让这些按钮在用单个手指触摸时具有一种行为,并且在用更多的手指触摸时具有不同的行为。例如,如果您用一个手指触摸该按钮变为红色的按钮,如果用2个手指触摸该按钮变为黄色的按钮,如果用3个手指触摸按钮,则变为绿色等等。在单个按钮上的多个触摸android
我遇到的问题是,当我用多个手指触摸按钮时,第二次触摸似乎不会触发事件。我如何检测同一按钮上的多个触摸?
答
这是你想要的东西的基础知识。它可能并不完全是你想要的,因为手指之间必须有一些空间,我曾经认识到这一点。如果两个手指靠在一起,它将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>
HTTPS:/ /github.com/devunwired/custom-touch-examples这个存储库可能会有所帮助。 – Raghunandan
我在找哪个确切的例子? – user381261