Search code examples
androidmovetouch-eventontouchlistenerdouble-click

Move and Double click for an ImageView


I have this problem:

I have an ImageView and i set it in onTouchListener and i'm able to move it. The problem is that i want to set the same imageview to listen a DoubleClick event.

Do you have any examples that can help me to resolve this issue?

I need move and double tap on the same imageview!


Solution

  • public class Touch_Screen  extends Activity implements OnTouchListener {
    private ImageView mImageView;
    
    protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.touch_test);
            super.onCreate(savedInstanceState);
    
                mRrootLayout = (ViewGroup) findViewById(R.id.root);
                mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);
                mImageView.setVisibility(View.INVISIBLE);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
                mImageView.setLayoutParams(layoutParams);
    
                mImageView.setOnTouchListener(this);
    }
    
    @Override
        public boolean onTouch(View view, MotionEvent event) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
                // TODO: handle exception
            }
            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
    
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
    
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
            }
            mRrootLayout.invalidate();
            return true;
        }
    
    }