android: move a view on touch move (ACTION_MOVE)

后端 未结 11 2341
终归单人心
终归单人心 2020-11-22 13:54

I\'d like to do a simple control: a container with a view inside. If I touch the container and I move the finger, I want to move the view to follow my finger.

What

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 14:10

    Changed a bit a solution provided by @Vyacheslav Shylkin to remove dependencies of manually entered numbers.

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewTreeObserver;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    
    public class MainActivity extends Activity implements View.OnTouchListener
    {
        private int       _xDelta;
        private int       _yDelta;
        private int       _rightMargin;
        private int       _bottomMargin;
        private ImageView _floatingView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            this._floatingView = (ImageView) findViewById(R.id.textView);
    
            this._floatingView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
            {
                @Override
                public boolean onPreDraw()
                {
                    if (_floatingView.getViewTreeObserver().isAlive())
                        _floatingView.getViewTreeObserver().removeOnPreDrawListener(this);
    
                    updateLayoutParams(_floatingView);
                    return false;
                }
            });
    
            this._floatingView.setOnTouchListener(this);
        }
    
        private void updateLayoutParams(View view)
        {
            this._rightMargin = -view.getMeasuredWidth();
            this._bottomMargin = -view.getMeasuredHeight();
    
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(view.getMeasuredWidth(), view.getMeasuredHeight());
            layoutParams.bottomMargin = this._bottomMargin;
            layoutParams.rightMargin = this._rightMargin;
    
            view.setLayoutParams(layoutParams);
        }
    
        @Override
        public boolean onTouch(View view, MotionEvent event)
        {
            if (view == this._floatingView)
            {
                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();
                        this._xDelta = X - lParams.leftMargin;
                        this._yDelta = Y - lParams.topMargin;
                        break;
    
                    case MotionEvent.ACTION_MOVE:
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        layoutParams.leftMargin = X - this._xDelta;
                        layoutParams.topMargin = Y - this._yDelta;
                        layoutParams.rightMargin = this._rightMargin;
                        layoutParams.bottomMargin = this._bottomMargin;
                        view.setLayoutParams(layoutParams);
                        break;
                }
    
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    

提交回复
热议问题