Skip to content Skip to sidebar Skip to footer

Acivate View With Moving Finger

I'm trying to create a field filled with rectangles and if you move your finger the rectangles under it changes color. I've tried it many ways but i did not succeed and i don't hav

Solution 1:

You can do something like that,

     @Override
                public boolean onTouchEvent(MotionEvent event) {
                    // TODO Auto-generated method stub
                    final int action = event.getAction();

                    switch (action & MotionEventCompat.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        event.getX();
event.getY();

                        Log.d(TAG, "onTouchEvent DOWN: x " + selX + ", y " + selY);
                        break;

                    case MotionEvent.ACTION_MOVE:
                        event.getX();
                                event.getY();
                        Log.d(TAG, "onTouchEvent Move: x " + selX + ", y " + selY);
                        break;

                    case MotionEvent.ACTION_UP:
                        event.getX();
                                event.getY();
                        Log.d(TAG, "onTouchEvent UP: x " + selX + ", y " + selY);

                        break;
                    }

                    returntrue;
                }

The MotionEvent.ACTION_MOVE will detect the pointer movement. You can get the pointer and do your color filling part. Also check logcat for better understanding.. Good Luck!

Solution 2:

I figured it out... the getLeft(), getTop().. gives back 0 values so i changed it with this:

outRect.top=(int) (buttons[i][j].getY());outRect.bottom=(int) (buttons[i][j].getY()+buttons[i][j].getLayoutParams().height);outRect.left=(int) (buttons[i][j].getX());outRect.right=(int) (buttons[i][j].getX()+buttons[i][j].getLayoutParams().height);

Post a Comment for "Acivate View With Moving Finger"