I want a button that can detect multiple fingers. Like ı am holding with 1 finger and then 1 more finger touches that button it has to detect that.
Or
a button that when pushed, automatically release that button dont care if it actually released or not.
I use onTouchevent for multiple buttons for my drum kit project.
#Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
Animation zoomIn = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoomin);
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
switch(view.getId())
{
case R.id.BassL:
findViewById(R.id.bass).startAnimation(zoomIn);
sp.play(BassD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.BassR:
findViewById(R.id.bass).startAnimation(zoomIn);
sp.play(BassD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.SnareL:
findViewById(R.id.snare).startAnimation(zoomIn);
sp.play(SnareD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.SnareR:
findViewById(R.id.snare).startAnimation(zoomIn);
sp.play(SnareD,1.0f,1.0f,0,0,1.0f);
break;
case R.id.OpenHihat:
findViewById(R.id.hihat).startAnimation(zoomIn);
sp.stop(OpenH);
sp.play(OpenH,1.0f,1.0f,0,0,1.0f);
break;
case R.id.CloseHihat:
findViewById(R.id.hihat).startAnimation(zoomIn);
sp.play(CloseH,1.0f,1.0f,0,0,1.0f);
break;
case R.id.RideSurface:
findViewById(R.id.ride).startAnimation(zoomIn);
sp.stop(RideS);
sp.play(RideS,1.0f,1.0f,0,0,1.0f);
break;
case R.id.RideTop:
findViewById(R.id.ride).startAnimation(zoomIn);
sp.play(RideT,1.0f,1.0f,0,0,1.0f);
break;
}
}
return true;
}
}
You can check the pointer id associated to the MotionEvent you've just received.
See the docs for more info:
https://developer.android.com/reference/android/view/MotionEvent.html#getPointerId(int)
Related
I am currently using dispatchTouchEvent to grab touch events, is there an easy way to distinguish between a click and a "drag" stylt gesture?
DispatchTouchEvent is called with a MotionEvent parameter. Method getAction within MotionEvent can return
ACTION_DOWN
ACTION_MOVE
ACTION_UP
ACTION_CANCEL
Then set on ACTION_DOWN flag isClick. If there is ACTION_MOVE clear isClick flag.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
isClick = true;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (isClick) {
//TODO Click action
}
break;
case MotionEvent.ACTION_MOVE:
isClick = false;
break;
default:
break;
}
return true;
}
set up a threshold limit.When you move the pointer within a small range make it recognized as a click or else a movement
So I have two buttons and would like to press both at the same time. If I press the first it logs a "C" (as a piano note), the other logs a D.
So far:
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
switch (v.getId()) {
case R.id.cnotebutton:
Log.i("C", "C1");
return true;
case R.id.c2notebutton:
Log.i("D", "D1");
return true;
default:
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
switch (v.getId()) {
case R.id.cnotebutton:
Log.i("C", "C2");
return true;
case R.id.c2notebutton:
Log.i("D", "D2");
return true;
default:
}
break;
case MotionEvent.ACTION_MOVE:
break;
default:
}
return true;
}
I Log C1 and C2 to distinguish the cases, but every time I press the buttons I get only C1,C2 or D1,D2 as if I touched the same buttons with both of my fingers. I should get C1,D2 or D1,C2 depending on which finger pointed first. Any suggestions?
Anyway I havent found a piano sample project yet, but that would definetly help me, if this is not going to work or my approach is totally wrong. Is there any?
Thanks in advance!
I think public boolean onInterceptTouchEvent (MotionEvent ev)is what you are looking for. Take a look at this.
How can I detect multitouch events? The code im trying is:
ImageView im = (ImageView) findViewById(R.id.imageView1);
im.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.e("case MotionEvent.ACTION_POINTER_DOWN","case MotionEvent.ACTION_POINTER_DOWN");
break;
case MotionEvent.ACTION_DOWN:
Log.e("case MotionEvent.ACTION_DOWN","case MotionEvent.ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
Log.e("case MotionEvent.ACTION_UP","case MotionEvent.ACTION_UP");
break;
case MotionEvent.ACTION_MOVE:
Log.e("case MotionEvent.ACTION_MOVE","case MotionEvent.ACTION_MOVE");
break;
}
return false;
}
});
It detects the first touch and the output in the log cat is MotionEvent.ACTION_POINTER_DOWN. How to know whether a second touch has taken place?
There are many ways to do it, so u can choose one of many here some link have write a complete application to handle multi touch.
multitouch
multi-touch-handling
You should add follwing line to imageView1 in your xml layout file:
android:longClickable="true"
This will enable long clicks and other events will be received too.
I'm trying to use multitouch as a method to press 2 things simultaneously.
Here's my Code:
TouchHandle.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
int actions = event.getActionMasked();
switch (actions)
{
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
SendKeyCode(Server, "keydec.down");
case MotionEvent.ACTION_POINTER_UP:
SendKeyCode(Server, "keydec.up");
case MotionEvent.ACTION_UP:
}
return true;
}
});
The problem is that when I touch the screen, the server is retrieving "keydec.down" and "keydec.up" even thought I haven't removed my finger yet from the touchscreen. So the View TouchHandle is detecting an ACTION_POINTER_DOWN and ACTION_POINTER_UP when my finger touches the screen even if I haven't pulled my finger up yet. Any thoughts?
int actions = event.getAction();
switch (actions)
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_POINTER_DOWN:
SendKeyCode(Server, "keydec.down");
break;
case MotionEvent.ACTION_POINTER_UP:
SendKeyCode(Server, "keydec.up");
break;
case MotionEvent.ACTION_UP:
break;
}
You should use breaks between cases. And i also think that these MotionEvent constants might work with event.getAction() try that one.
Is it possible to detect anytime a user moves their finger off of a button after they have been pressing down on it?
I have determined that onTouchListener will get an action when the user moves off of the button vertically (It gives an action: ACTION_CANCEL), but I have not been able to find a way to determine when a user moves off the button horizontally.
public boolean onTouch(View arg0, MotionEvent arg1) {
switch(arg1.getAction()){
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
switch(arg0.getId()){
case R.id.my_button:
button.setBackgroundResource(R.drawable.untouched);
break;
}
break;
case MotionEvent.ACTION_DOWN:
switch(arg0.getId()){
case R.id.my_button:
button.setBackgroundResource(R.drawable.touched);
break;
}
break;
}
return false;
}
Try go next:
arg1.getX();
arg1.getY();
arg1 - is from your code
Here you can get coordinates of MotionEvent. Next you will do anything with this coordinates
Hope, it help you