we often have to do something, for example, after clicking the animation, moving as the finger moves. So how do they make it work? Until we, the automatic movement of the view, we can set the animation, such as before the fragment of the entry and pop-up animation: Https://github.com/nuptboyzhb/FragmentAnimationDemo Then, How do we move a view as the finger moves?
1.onTouch Events
We add the Ontouch event to the view to get the position of the finger relative to the screen during the move: [Code]
@Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case Motionevent.action_down: Break;case MotionEvent.ACTION_MOVE:moveViewWithFinger (view, EVENT.GETRAWX (), Event.getrawy ()); break;case MotionEvent.ACTION_UP:break;} return true;}
2. Method 1:setlayoutparams
Once you have the position of your finger, you can dynamically set the view's layout to achieve the "move" view. [Code]
/** * Set the layout properties of the view so that the view moves with the finger Note: The layout of the view must use Relativelayout and should not be centered such as style * * @param view * @param rawx * @param rawy */private void Moveviewwithfinger (view view, float rawx, float rawy) {relativelayout.layoutparams params = (relativelayou T.layoutparams) view.getlayoutparams ();p arams.leftmargin = (int) rawx-ivmove.getwidth ()/2;params.topmargin = (int) RA Wy-toptitleheight-ivmove.getheight ()/2;view.setlayoutparams (params);}
3. Method 2:view.layout (L,T,R,B)
Method Two limitations, mainly on the layout of the view is higher requirements, and layout is not too many requirements, can be relativelayout, can also be linearlayout.
/** * Through the layout method, mobile View * Advantages: The layout of the view, not demanding, not relativelayout, and can modify the size of the view * * @param view * @param rawx * @pa Ram Rawy */private void Moveviewbylayout (view view, int rawx, int rawy) {int left = Rawx-ivmove.getwidth ()/2;int top = Rawy-toptitleheight-ivmove.getheight ()/2;int width = left + view.getwidth (); int height = top + view.getheight (); Vie W.layout (left, top, width, height);}
In addition, the size of the view can be changed dynamically during the move.
Objects in 4.Surfaceview
In Surfaceview, we are more likely to move a view, see the "new Aircraft Wars" source code [Https://github.com/nuptboyzhb/newplanegame], the movement of the main plane, moving with the movement of the finger.
5. Source code for methods 1 and 2: Https://github.com/nuptboyzhb/MoveViewWithFinger
-------------------------------------------------------------------
more communication, Android Development Alliance QQ Group: 272209595
Android:view moves with the fingers