Multi-touch button for working with Android

Hi i want to create 2 buttons and i want multitouch ??

I tried to do, but there is no example online.

So if you have, can you share or can you give me an opinion? my code is this but not multitouch support

 package multi.touch;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnTouchListener;
 import android.widget.AbsoluteLayout.LayoutParams;
 import android.widget.Button;
 import android.widget.TextView;

 public class baslat extends Activity implements OnTouchListener  { 
 TextView yazi;
 TextView bir,iki;
 Button buton1,buton2;

 @Override public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 yazi=(TextView)findViewById(R.id.textView1);
 bir=(TextView)findViewById(R.id.textView2);
 iki=(TextView)findViewById(R.id.textView3);
 buton1=(Button)findViewById(R.id.button1);
 buton2=(Button)findViewById(R.id.button2);

 buton2.setOnTouchListener(this);

 buton1.setOnTouchListener(this);

 }
 @Override
 public boolean onTouchEvent(MotionEvent event) {
    yazi.setText(String.valueOf(event.getPointerCount()+"\n\n"));
            bir.setText(String.valueOf("Birinci "
 + (int)event.getX(0)+"\n\n"+(int)event.getY(0)));
        iki.setText(String.valueOf("Ikinci"+
 (int)event.getX(1)+"\n\n"+(int)event.getY(1)));
    //buton2.setLayoutParams(new
 LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
 (int)event.getX(0),
 (int)event.getY(0)));      return
 super.onTouchEvent(event);
            } @Override public boolean onTouch(View v, MotionEvent event) {
    Button fds=(Button)v;


            return false; }


 }

      

+1


source to share


4 answers


Old question, but I was running my head in the wall with this problem until I finally came across just by setting

android:splitMotionEvents="true"

      



on a layout view containing button views that allow multiple buttons to be clicked which I found in ApiDemos which are available in the sdk demo

+6


source


In the Android UI, all touch events refer to the view in which the sensor originated. Therefore, if you touch your button, all sensory events are processed by that button until you lift your finger; this includes other touch pointers for multitouch. In my experience, the only way to achieve multitouch in separate View objects (your 2 buttons) is to capture all touch events in one view that spans the entire screen and delegate touch events yourself. It's a bit of a job, but it can be done.



An example would be to include an ImageView that fills the screen but does not have a Drawable source (or is completely transparent). Place this on top of other elements (probably with a FrameLayout) and in the onTouchEvent () method of the ImageView, parse the touch coordinates and fire the touch event down to the correct button. It's quite complicated with multiple touch pointers, but as far as I know, this is the only way to dispatch touch events to separate the View objects.

+4


source


I was a little bored with this subject and created a handler class to handle multitouch buttons. Feel free to use and / or update this.



//CLASS TO HANDLE THE EVENT
    public class MultitouchButtonHandler {
            ArrayList<View> views_info = new ArrayList<View>();
            public MultitouchButtonHandlerResult onTouchEvent(View v, MotionEvent ev) {

            //GET EVENT INFO 
            final int action = ev.getAction();
            int action_masked = action & MotionEvent.ACTION_MASK;
            if(action_masked==MotionEvent.ACTION_MOVE){
                return null;
            }

                //GET ABSOLUTE SIZE AND CHECK IF THIS ANY VIEW ATTACHED TO THIS POSITION
                final int original_location[] = { 0, 0 };
                v.getLocationOnScreen(original_location);
                final int actionPointerIndex = ev.getActionIndex();
                float rawX = (int) ev.getX(actionPointerIndex) + original_location[0];
                float rawY = (int) ev.getY(actionPointerIndex) + original_location[1];
                View eventView = getViewByLocation((int)rawX, (int)rawY);
                if(eventView==null) return null;

                MultitouchButtonHandlerResult result = new MultitouchButtonHandlerResult();
                result.view  = eventView;


                //CHECK WHAT KIND OF EVENT HAPPENED 
                switch (action_masked) {
                case MotionEvent.ACTION_DOWN: {
                    result.event_type = MotionEvent.ACTION_DOWN;
                    return result;
                }

                case MotionEvent.ACTION_UP: {
                    result.event_type = MotionEvent.ACTION_UP;
                    return result;
                }

                case MotionEvent.ACTION_CANCEL: {
                    result.event_type = MotionEvent.ACTION_CANCEL;
                    return result;
                }

                case MotionEvent.ACTION_POINTER_UP: {
                    result.event_type = MotionEvent.ACTION_UP;
                    return result;
                }

                case MotionEvent.ACTION_POINTER_DOWN: {
                    result.event_type = MotionEvent.ACTION_DOWN;
                    return result;
                }

                default:

                break;

                }

                return null;
            }

            public void addMultiTouchView(View v){
                views_info.add(v);;
            }
            public void removeMultiTouchView(View v){
                views_info.remove(v);;
            }

            public View getViewByLocation(int x, int y){

                for(int key=0; key!= views_info.size(); key++){
                    View v = views_info.get(key);
                    //GET BUTTON ABSOLUTE POSITION ON SCREEN
                    int[] v_location = { 0, 0 };
                    v.getLocationOnScreen(v_location);

                    //FIND THE BOUNDS
                    Point min = new Point(v_location[0], v_location[1]);
                    Point max = new Point(min.x + v.getWidth(), min.y + v.getHeight());


                    if(x>=min.x && x<=max.x && y>=min.y && y<=max.y){
                        //Log.d("mylog", "***Found a view: " + v.getId());
                        return v;
                    }

                }

                //Log.d("mylog", "Searching: " + x +", " + y + " but not found!");

                return null;
            }

        }

      

code>



//CLASS TO FULLFILL WITH RESULT
public class MultitouchButtonHandlerResult {
    public View view;
    public int event_type;
}

      

code>



//In your view 
private OnTouchListener listener_touch_button = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
      MultitouchButtonHandlerResult result=multitouch_handler.onTouchEvent(v, event);
     if(result==null) return true;

                switch(result.event_type){
                    case MotionEvent.ACTION_DOWN:
                        Log.d("mylog", "DOWN");
                break;
                    case MotionEvent.ACTION_UP:
                        Log.d("mylog", "UP");
                break;
                    case MotionEvent.ACTION_CANCEL:
                            Log.d("mylog", "CANCEL");
                break;

                }

               Log.d("mylog", "View ID: " + result.view.getId());

               return false;
      }

 };

      

code>

+2


source


0


source







All Articles