Recently, I'm going to write a streamlined launcher with a drag function, according to the Android Launcher2 source. An interesting method--getdescendantcoordrelativetoself was found when analyzing the Draglayer class. Through the introduction of the two articles and their own experiments, it is finally clear the principle of the method.
http://blog.csdn.net/hahajluzxb/article/details/8165258
Http://www.cnblogs.com/platte/p/3534279.html
The following is the main analysis of the principle of code:
The first thing to know is that the general coordinate transformation transform, including: translation translate, scaling scale, rotation rotate and so on. The coordinate transformation changes the coordinate system, and the coordinate value of the point is not changed (unless you actively change it), but the point is drawn in the coordinate system. Changes in the coordinate system will make the point drawing change.
Next is the analysis of the Code,
1 /**2 *3 * @paramdescendant View4 * @paramcoord The coordinates of a point in a child view, and when the method returns, Coord is converted to coordinates in the topmost parentview coordinate system5 * @returnreturns the scale value of descendant relative to the top-level Parentview6 */7 Public floatGetdescendantcoordrelativetoself (View descendant,int[] coord) {8 floatScale = 1.0f;9 Ten //(Coord[0],coord[1]) is the (x, y) coordinates of the point to be converted in the child view, respectively One float[] pt = {Coord[0], coord[1]}; A - //The sub view changes the coordinate system of the sub view because of the rotation and scaling, which are reflected on the matrix of the sub view, - //the Getmatrix () method gets the matrix of the child view. The Mappoints method can obtain the coordinates of PT points in the initial coordinate system . the Descendant.getmatrix (). Mappoints (PT); - //Calculate the zoom value of the child view X axis -Scale *=Descendant.getscalex (); - + //in my experiment, I found that the left,right,top,bottom of the sub-view was constant in the pair view transformation . - //so the following two lines can calculate the coordinates of the points in the descendant in their parent view coordinate system +Pt[0] + =descendant.getleft (); APT[1] + =descendant.gettop (); at - //in the loop below, the recursive layer of the descendant calculates the coordinates of the points in the top-level view, which is the Draglayer coordinate system. - //and the total scale value of the x-axis -Viewparent viewparent =descendant.getparent (); - while(viewparentinstanceofView && viewparent! = This) { - FinalView view =(View) viewparent; in View.getmatrix (). Mappoints (PT); -Scale *=View.getscalex (); toPt[0] + = View.getleft ()-view.getscrollx (); +PT[1] + = View.gettop ()-view.getscrolly (); -Viewparent =view.getparent (); the } * $ //return ResultsPanax NotoginsengCoord[0] = (int) Math.Round (pt[0]); -COORD[1] = (int) Math.Round (pt[1]); the returnScale ; +}
The above is the explanation of the code. Note that the view is on the left side of the view in the coordinate transformation, the top, the right, the bottom distance from his parent component is getleft (), and the value returned by GetTop () is constant. At least I wrote a simple applet to run after the show is really like this.
Small program is simple, layout is a center of ImageView. Once every click ImageView, it rotates 5 degrees. Then log outputs its left,top information.
1 Public classMainactivityextendsActivity {2 3 ImageView image;4 introtation = 0;5 6 @Override7 protected voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);TenImage =(ImageView) Findviewbyid (r.id.image); OneImage.setonclicklistener (NewView.onclicklistener () { A - @Override - Public voidOnClick (View v) { the //TODO auto-generated Method Stub -Rect r =NewRect (); - V.gethitrect (r); -LOG.D ("TAG", "before change:v.left =" + v.getleft () + ", V.top =" +v.gettop ()); + float[] F1 ={0f, 0f}; - V.getmatrix (). mappoints (F1); +LOG.D ("TAG", "after Map:r.left =" + Math.Round (f1[0]) + ", R.top =" + Math.Round (f1[1])); ARotation + = 5; at //V.setscalex (0.5f); - v.setrotation (rotation); - V.gethitrect (r); -LOG.D ("TAG", "after Change:v.left =" + v.getleft () + ", V.top =" +v.gettop ()); - float[] F2 ={0f, 0f}; - V.getmatrix (). mappoints (F2); inLOG.D ("TAG", "after Map:r.left =" + Math.Round (f2[0]) + ", R.top =" + Math.Round (f2[1])); - } to }); + - } the}
01-17 05:53:08.100:d/tag (1866): Before change:v.left = 272, V.top = 7301-17 05:53:08.110:d/tag (1866): After Map:r.left = 0, R.top = 001-17 05:53:08.110:d/tag (1866): After change:v.left = 272, V.top = 7301-17 05:53:08.110:d/tag (1866): aft Er map:r.left = r.top = -2001-17 05:53:08.830:d/tag (1866): Before change:v.left = 272, V.top = 7301-17 05:53:08.830 : D/tag (1866): After map:r.left = +, R.top = -2001-17 05:53:08.830:d/tag (1866): After change:v.left = 272, V.top = 730 1-17 05:53:08.830:d/tag (1866): After map:r.left =, r.top = -3901-17 05:53:09.800:d/tag (1866): Before Change:v.left = 272, V.top = 7301-17 05:53:09.800:d/tag (1866): After map:r.left =, r.top = -3901-17 05:53:09.800:d/tag (1866): Afte R change:v.left = 272, V.top = 7301-17 05:53:09.800:d/tag (1866): After map:r.left =, r.top =-5601-17 05:53:10.910: D/tag (1866): Before change:v.left = 272, V.top = 7301-17 05:53:10.910:d/tag (1866): After map:r.left =, r.top = 5601 -17 05:53:10.910:d/tag (1866): After change:v.left = 272, V.top = 7301-17 05:53:10.910:d/tag (1866): After map:r.left =, R.top = 72
From the log information you can see that r.left = 272, R.top = 73 is constant after every click. Only in this way can the following two lines make sense.
Pt[0] + = descendant.getleft (); pt[1] + = Descendant.gettop ();
Finish (can't actually insert link!!!) )。