Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • count pixels under each finger in multi touch in android

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 393
    Answer it

    Hello I need to count pixels that are covered by each fingers in multi-touch.When fingers down one by one it should plus pixels in count and finger up it should subtract pixels of that finger.I have did this but it is not getting exact counts i don't know what's wrong with it.please help me i am stucked at it.

     public class MainActivity extends AppCompatActivity {
    
    private static final int INVALID_POINTER_ID = -1;
    
    private float mLastTouchX;
    private float mLastTouchY;
    private int mActivePointerId = INVALID_POINTER_ID;
    
    private float mPosX;
    private float mPosY;
    
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;
    
    float TouchMajor=0.0f;
    float TouchMinor=0.0f;
    //float Size = 0.0f;
    
    Boolean IsRightSideActive = false;
    Boolean IsLeftSideActive = false;
    float RightPixSize=0.0f;
    float LeftPixSize=0.0f;
    float RightPixChange = 0.0f;
    float LeftPixChange = 0.0f;
    float Pressure = 0.0f;
    String Orientaation = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Image img = new Image(getApplicationContext());
        setContentView(R.layout.activity_main);
    
        final ImageView MyimageView = (ImageView)findViewById(R.id.imageView);
        final TextView LeftPixelView = (TextView)findViewById(R.id.LeftPixelView);
        final TextView RightPixelView = (TextView)findViewById(R.id.RightPixelView);
        final TextView PressureView = (TextView)findViewById(R.id.PressureView);
        final TextView OrientationView = (TextView)findViewById(R.id.OrientationView);
        final Display display = getWindowManager().getDefaultDisplay();
        final int ScreenWidth = display.getWidth();
    
    
    
    
        if (MyimageView != null) {
            MyimageView.setOnTouchListener(new View.OnTouchListener() {
                @Override
    
            public boolean onTouch(View arg0, MotionEvent event) {
    
                    // get pointer index from the event object
                 //   int pointerIndex = event.getActionIndex();
    
                    mScaleDetector.onTouchEvent(event);
    
                    // get pointer ID
                  //  int pointerId = event.getPointerId(pointerIndex);
    
                    // get masked (not specific to a pointer) action
    
    
                    int maskedAction = event.getActionMasked();
    
                    switch (maskedAction) {
    
                        case MotionEvent.ACTION_DOWN: {
    
                            final float x = event.getX();
                            final float y = event.getY();
    
                            mLastTouchX = x;
                            mLastTouchY = y;
    
                            ///Toast toast = Toast.makeText(getApplicationContext(), "Action down", Toast.LENGTH_SHORT);
                             //toast.show();
                            int pointerIndex = event.getActionIndex();
                            mActivePointerId = event.getPointerId(0);
    
                            TouchMajor = event.getTouchMajor(pointerIndex);
                            TouchMinor = event.getTouchMinor(pointerIndex);
    
                            Pressure = event.getPressure(pointerIndex);
                            PressureView.setText("Pressure :"+Float.toString(Pressure));
    
                            //Orientaation =Double.toString( event.getOrientation(mActivePointerId));
                            //left Side of scren
    
                            if(event.getX(mActivePointerId) < ScreenWidth/2)
                            {
                                LeftPixChange =  (TouchMajor + TouchMinor) / 2;
                                LeftPixSize = LeftPixChange;
                                LeftPixelView.setText(Float.toString(LeftPixSize));
                                OrientationView.setText("Left Side Clicked");
                            }
                            else if(event.getX(mActivePointerId) > ScreenWidth/2)
                            {
                                 RightPixChange =  (TouchMajor + TouchMinor) / 2;
                                 RightPixSize = RightPixChange;
                                RightPixelView.setText(Float.toString(RightPixSize));
                                OrientationView.setText("Right Side Clicked");
                            }
    
                            break;
                        }
                        case MotionEvent.ACTION_POINTER_DOWN: {
                           final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                            final int pointerId = event.getPointerId(pointerIndex);
    
                            if (pointerId == mActivePointerId) {
                                // This was our active pointer going up. Choose a new
                                // active pointer and adjust accordingly.
                                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                                 mLastTouchX = event.getX(newPointerIndex);
                                 mLastTouchY = event.getY(newPointerIndex);
                                mActivePointerId = event.getPointerId(newPointerIndex);
                            }
    
                            TouchMajor = event.getTouchMajor(pointerIndex);
                            TouchMinor = event.getTouchMinor(pointerIndex);
    
                            Pressure =Pressure +  event.getPressure(pointerIndex);
                            PressureView.setText("Cummulative pressure :"+Float.toString(Pressure));
    
                            //Orientaation = Orientaation+","+ event.getOrientation(mActivePointerId);
                           // OrientationView.setText(Orientaation);
                           //we take pointerindex here because pointerId remains same but index will be increased
                            if(event.getX(pointerIndex) < ScreenWidth/2)
                            {
                                LeftPixChange = (TouchMajor + TouchMinor) / 2;
                                LeftPixSize =LeftPixSize + LeftPixChange;
                                LeftPixelView.setText(Float.toString(LeftPixSize));
                                OrientationView.setText("Multi Left side clicked");
    
                                IsLeftSideActive = true;
                            }
                            else if(event.getX(pointerIndex) > ScreenWidth/2)
                            {
                                RightPixChange = (TouchMajor + TouchMinor) / 2;
                                RightPixSize =RightPixSize + RightPixChange;
                                RightPixelView.setText(Float.toString(RightPixSize));
    
                                OrientationView.setText("Multi Right side clicked");
    
                                IsRightSideActive = true;
                            }
                            else
                            {
                                OrientationView.setText("Both Sidse Clicked simultaneously");
                            }
                            break;
                        }
                        case MotionEvent.ACTION_MOVE: { // a pointer was moved
                            // TODO use data
                            final int pointerIndex = event.findPointerIndex(mActivePointerId);
    
                            final float x = event.getX(pointerIndex);
                            final float y = event.getY(pointerIndex);
    
                          if (!mScaleDetector.isInProgress()) {
                                final float dx = x - mLastTouchX;
                                final float dy = y - mLastTouchY;
    
                                mPosX += dx;
                                mPosY += dy;
    
                                MyimageView.invalidate();
                            }
    
                            mLastTouchX = x;
                            mLastTouchY = y;
                            break;
                        }
    
    
                        case MotionEvent.ACTION_UP:{
    
                            mActivePointerId = INVALID_POINTER_ID;
                            //Toast toast = Toast.makeText(getContext(), "Action UP", Toast.LENGTH_SHORT);
                            //toast.show();
                            RightPixelView.setText(Float.toString(0.0f));
                            LeftPixelView.setText(Float.toString(0.0f));
                            PressureView.setText("Pressure : 0.0");
                            OrientationView.setText("");
                            break;
                        }
                        case MotionEvent.ACTION_POINTER_UP:{
                            final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                            final int pointerId = event.getPointerId(pointerIndex);
    
                            if (pointerId == mActivePointerId) {
                                // This was our active pointer going up. Choose a new
                                // active pointer and adjust accordingly.
                                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                                 mLastTouchX = event.getX(newPointerIndex);
                                 mLastTouchY = event.getY(newPointerIndex);
                                mActivePointerId = event.getPointerId(newPointerIndex);
    
    
                            }
                            if(event.getX(pointerIndex) > ScreenWidth/2)
                            {
                                RightPixSize = RightPixSize - RightPixChange;
                                RightPixelView.setText(Double.toString(RightPixSize));
                            }
                            else if(event.getX(pointerIndex) < ScreenWidth/2){
                                LeftPixSize = LeftPixSize - LeftPixChange;
                                LeftPixelView.setText(Double.toString(LeftPixSize));
                                IsRightSideActive = false;
                            }
    
    
                            Pressure = Pressure - event.getPressure(event.getActionIndex());
    
                            PressureView.setText("Pressure :"+ Double.toString(Pressure));
    
    
    
    
    
                            break;
    
                        }
                        case MotionEvent.ACTION_CANCEL: {
                            // TODO use data
    
                            mActivePointerId = INVALID_POINTER_ID;
    
    
    
                            MyimageView.invalidate();
    
                            break;
                        }
                    }
                      MyimageView.invalidate();
    
                    return true;
                }});
        }

     

 1 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: