在android中多点触控每个手指下的像素数

时间:2016-07-05 05:38:21

标签: java android android-layout multi-touch motionevent

你好我需要计算多点触控中每个手指所覆盖的像素。当手指逐个向下时,它应该加上计数中的像素,并且手指向上应该减去该手指的像素。我已经这样做但它是没有得到确切的数字我不知道它有什么问题。请帮助我,我被困在它。

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

0 个答案:

没有答案