Android: View moves as your fingers move

Source: Internet
Author: User

Android: View moves as your fingers move
We often need to make some effects, such as the animation after clicking, moving as the fingers move. How are they implemented? We until, the view of automatic movement, we can set the animation, such as the previous write Fragment into the with the pop-up animation: https://github.com/nuptboyzhb/FragmentAnimationDemo so, how do we move a View as the fingers move?


1. onTouch event


We add an onTouch event for the view to obtain the position of the finger relative to the screen during the movement: [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


After obtaining the position of the finger, you can dynamically set the View layout to "move" the view. [Code]

/*** Set the layout attribute of the View so that the view moves with its fingers. Note: the layout of the view must be RelativeLayout and the Center style cannot be set ** @ param view * @ param rawX * @ param rawY */private void moveViewWithFinger (View view, float rawX, float rawY) {RelativeLayout. layoutParams params = (RelativeLayout. layoutParams) view. getLayoutParams (); params. leftMargin = (int) rawX-ivMove. getWidth ()/2; params. topMargin = (int) rawY-topTitleHeight-ivMove. getHeight ()/2; view. setLayoutParams (params );}

3. Method 2: view. layout (l, t, r, B)
The limitation of method 2 is mainly to have high requirements on the layout of the view, while layout does not have many requirements, either RelativeLayout or LinearLayout.

/*** Use the layout method to move the view. * advantages: the layout of the view is not demanding. Do not use RelativeLayout, you can also modify the view Size ** @ param view * @ param rawX * @ param 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 (); view. layout (left, top, width, height );}

In addition, the view size can be dynamically changed during the moving process.




4. Objects in Surfaceview
In SurfaceView, it is easier for us to move a view. For more information, see the source code of "New Version aircraft Wars.


5. source code for methods 1 and 2: https://github.com/nuptboyzhb/MoveViewWithFinger


-------------------------------------------------------------------

For more information, the Android Development Alliance's QQ group: 272209595


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.