Android work multitouch button

后端 未结 4 1952
一个人的身影
一个人的身影 2020-12-01 20:22

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

i tryed to do but no example in internet..

So if you got one can you share or can you give me opinio

4条回答
  •  再見小時候
    2020-12-01 21:01

    I was a little bored with this subject and i made an handler class for leading with multitouch buttons. Feel free to use and/or update that.

    //CLASS TO HANDLE THE EVENT
        public class MultitouchButtonHandler {
                ArrayList views_info = new ArrayList();
                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;
                }
    
            }
    

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

    //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;
          }
    
     };
    

提交回复
热议问题