3 Ways to summarize Android view move _android

Source: Internet
Author: User

Objective

In Android development, view has always been a heart of Android developers, on the one hand want to step, on the one hand afraid of advanced, you can say that Android view is the biggest stumbling block on the advanced road, because it involves too much, such as this time we want to write the view move, It also includes the transfer of view touch events and the creation of Custom view, which are extremely important and difficult to face. But in any case, the difficulties that are not overcome will be overcome in the future.

Before that, let's understand the definition rules of the Android coordinate system and some of the location parameters of the view.

Android coordinate system

The position and size of the view is determined by four parameters, left, top, right, bottom, and the four parameters are relative to their parent view.

int width = right-left;
 int height = bottom-top;

After the layout is complete in the activity, we can get the parameter information through the view methods:

The Left,top,right,bottom value gets
 int left = GetLeft ();
 int top = GetTop ();
 int right = GetRight ();
 int bottom = Getbottom ();

In addition, Android 3.0 after adding X,y,translationx,translationy parameters. (X,y) Represents the value of the x,y in the upper-left corner of the view in ViewGroup, translationx,translationy in the translation of a view. The default is 0, which is changed after the view is invoked setTranslationX()/setTranslationY() .

X,y,translationx,translationy parameters are obtained
 int x = GetX ();
 int y = GetY ();
 int translationx = Gettranslationx ();
 int translationy = Gettranslationy ();

PS: The and method of invoking view setTranslationX() setTranslationY() may allow the view translation to specify a distance, but the process is instantaneous. To make the view move smoother, you can use the view's property animation to specify Translationx and Translationy.

Objectanimator valueanimator = objectanimator.offloat (TextView, "Translationx");
 Valueanimator.setduration ();
 Valueanimator.start ();

Also, if you set and after the view, setTranslationX() setTranslationY() If you set the value unchanged, it moves only once, that is, the first specified move distance. After viewing the source code we found that the reason: the original after setting the value of the value and the current translationx,translationy will be compared, inconsistent to move.

Having learned some of the basic parameters of view, let's look at three ways to move the view.

First, use the Scrollto ()/scrollby () method provided by the Android system to achieve view movement.

Whether scrollTo() or not the scrollBy() essence of its movement is the content of the View/viewgroup. And the process of its movement is instantaneous, so in order to achieve a better moving effect, he needs to be used in conjunction with the Scroller class. In addition, it is different from the above translation, the move is the view itself, which needs to be well understood.

scrollTo()And scrollBy() are the methods in view, not the methods in scroller, but the smooth movement of control view is inseparable from the Scroller class.

scrollTo() :Refers to the absolute position of the movement, if the position does not change, multiple calls will not work.

Scrollto Moving Process Diagram

scrollBy() :Its essence is still called scrollTo() , refers to the relative distance of the current position (each time the current position and the set distance are combined and the call Scrollto (), so that if you call it multiple times, you will find that it moves a distance each time, which is the essential difference from Scrollto).

Scrollby Moving Process Diagram

PS: about the above two pictures, in fact, I have not fully understand what is relatively absolute, so the two hand chart may make it easier for people to understand. There is also scrollTo() the scrollBy() problem of moving direction, above we have drawn the Android coordinate system, the x axis Left → Right is positive, y axis from top → Bottom is positive. But this does not apply to Scrollto and Scrollby,scrollto and Scrollby just the opposite, that is, x-axis left → Right is negative, y-axis from the top → negative, is a bit of a pit dad ah.

Scroller class Analysis: And why use the methods in the Scroller class to move the content of View/viewgroup? Now let's try to analyze it.

First of all

We create an object Mscroller for the Scroller class.

And then

To move the view to a specified location at a given time, we call the method, which is a method startScroll() startScroll() Scroller in the class, and a method that is commonly used in the class Scroller filing() , which mainly deals with smooth movement and generally creates the inertia effect after sliding, Makes the view move more lifelike. Below we look at startScroll() the source code:

It receives four/five parameters. If duration is not set, it is the default. These four parameters are not difficult to understand, this is no longer an explanation. Public
 void Startscroll (int startx, int starty, int dx, int dy, int duration) { 
 ...
 }

And generally we call this method to tune the view invalidate() , this method can trigger the view of the draw() method. Instead draw()中 computeScroll() , we found an empty method in the source code computeScroll()是 , which is why we need to rewrite computeScroll() the method. Because the moving operation is in the computeScroll() .

@Override public
 void Computescroll () {
 if (Mscroller.computescrolloffset ()) {
  Scrollto ( Mscroller.getcurrx (), Mscroller.getcurry ());
  You must call the view's Postinvalidate ()/invalidate (), which will cause the view to move only the first frame if it is not added.
  postinvalidate ();
 }
 Super.computescroll ();
 }

Above we see that there is also a computescrolloffset () method in the Scroller class, what does it do? Its main function is to Judge Mcurrx, and Mcurry whether there is a change, there are returns True, no returns FALSE. This method can be used to point out whether a continuous call to Scrollto () is required to move the view. Here's another example, using Scrollto () to move the view along with the finger:

public class Cuview extends LinearLayout {private float mstartx;
 private float Mstarty;
 Private Scroller Mscroller;

 /** * The first slide is completed * * Private Boolean isfirstfinish;
 Public Cuview {Super (context);
 Init (context);
 Public Cuview (context, AttributeSet attrs) {Super (context, attrs);
 Init (context);
 private void init (context context) {Mscroller = new scroller (context);
 Public Cuview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
 Init (context); } @TargetApi (Build.version_codes. Lollipop) public Cuview (context, AttributeSet attrs, int defstyleattr, int defstyleres) {Super (context, attrs,
 Defstyleattr, Defstyleres);
 Init (context);  /** * Let the view follow your finger. * @param event * @return/@Override public boolean ontouchevent (Motionevent event) {int
 Action = Event.getaction (); Switch (action) {case Motionevent.action_down:/** * Once the first move is complete, we do not need to pick up the starting position, otherwise it will cause view toThe starting position of the move.
   */if (!isfirstfinish) {mstartx = Event.getrawx ();
  Mstarty = Event.getrawy ();
  } break;
  Case MotionEvent.ACTION_MOVE:scrollTo ((int) (MSTARTX-EVENT.GETRAWX ()), (int) (Mstarty-event.getrawy ()));
  Break
  Case MOTIONEVENT.ACTION_UP://First Move complete isfirstfinish = true;
 Break
 return true; /** * Test startscroll/public void Startscroll () {/** * Note scroller move direction, * * Mscroller.startscroll (20, 20,-50
 0,-500, 5000);
 Invalidate (); @Override public void Computescroll () {if (Mscroller.computescrolloffset ()) {Scrollto () (Mscroller.getcurrx ()), MSCR
  Oller.getcurry ());
 Invalidate ();
 } super.computescroll (); }
}

Second, the use of animation to achieve view movement.

This includes the view's tween Animation/frame Animation, and the property Animation that was added after 3.0. It moves an image of view, and the position and size of the view itself has not changed.

Third, set the view Layoutparams to move view

Linearlayout.layoutparams layoutparams = (linearlayout.layoutparams) textview.getlayoutparams ();
 Layoutparams.leftmargin = m;
 Textview.requestlayout ();

Summarize

The above is the summary of the Android view Mobile 3 ways to complete the content of the content, I hope this article on the development of Android can help you, if you have questions you can message exchange.

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.