Before the age of 3.0, it takes only two steps to determine whether a point falls on a view: The first step: the rect to get the view, and the second step: Determine if the point is within the rect.
But the simple days that start with Android 3.0 are over.
The reason is that Google provides Android 3.0 with a new set of animated frames: property Animation. View/viewgroup has gained powerful animation capabilities for this, but at the cost of View/viewgroup's implementation is more complex than ever. The View/viewgroup of 3.0 before being drawn to the canvas will only undergo a matrix transformation (if the user is not customized), but now the View/viewgroup matrix transformation becomes two times, and some new methods and fields are added to the View/viewgroup class, All this adds to the complexity of the View/viewgroup class.
The view class before Android 3.0 does not hold the matrix, only canvas holds matrix,view the basic operation of some matrices (shift, rotate, scale) can be accomplished only by modifying the matrix of the canvas. But from 3.0 onwards, for a better user experience, Google let view also own the matrix (because the matrix is for property animation, so it is called the property matrix), which allows view in each Draw A new layer of matrix transformations is added. However, it is not enough to add a layer of conversion just to "draw", because the drawing is just the output, and the view also handles the input (such as touch event), so view also needs to add a layer of matrix transformations to touch processing. In short, all operations related to input and output require a property matrix transformation.
Specifically, the property matrix that the View holds is computed at Dispatchdraw and dispatchtouchevent, respectively: The Dispatchdraw is multiplied by the Bitmap by this matrix; Dispatchtoucheve NT uses motionevent coordinates (x, y) to multiply the inverse of the matrix (Matrix.invert). This can be understood as showing that the view is dealing with its own bitmap, while the view needs to handle the motion event when dealing with Motionevent.
Back to the original question: How to tell if a point falls into a view.
View provides a way for getHitRect() the document to say that it works Hit rectangle in parent‘s coordinates . But in fact this method gets the wrong result. So what's the problem with Gethitrect (), first look at getHitRect() the implementation:
/** * Hit rectangle in parent‘s coordinates * * @param outRect The hit rectangle of the view. */public void getHitRect(Rect outRect) { updateMatrix(); final TransformationInfo info = mTransformationInfo; if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) { outRect.set(mLeft, mTop, mRight, mBottom); } else { final RectF tmpRect = mAttachInfo.mTmpTransformRect; tmpRect.set(-info.mPivotX, -info.mPivotY, getWidth() - info.mPivotX, getHeight() - info.mPivotY); info.mMatrix.mapRect(tmpRect); outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop, (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop); }}
Gethitrect's approach is to calculate the rect of the view before the matrix transformation and then perform a matrix transformation on the rect to get the current rect in the parent. So the key in Gethitrect () is how to get the original Rect, which is:
left: -info.mPivotX,top: -info.mPivotY,right: getWidth() - info.mPivotX,bottom: getHeight() - info.mPivotY);
Such data as the original rect.
It is wrong to do so. There are two reasons:
- No introduction of MSCROLLX and mscrolly
- -info.mpivotx cannot be left as left; -info.mpivoty cannot be a top; GetWidth ()-Info.mpivotx cannot act as right; GetHeight ()-Info.mpivoty) also cannot be used as bottom.
The second error can be answered simply by imagining that there is a rectangle in the window coordinates (0, 0, 2, 2) and then scaling 1 times with [1, 0.5 times] for the middle axis, so that a rectangle (0.5, 0.5, 1.5, 1.5) is obtained, and obviously gethitrect () cannot be To the right results.
Therefore, how to determine whether a point falls in a View will have to re-write a method, the code is as follows:
/** * Returns True if a child view contains the specified point when transformed * to its coordinate space. */private boolean Istransformedtouchpointinview (float x, float y, View child, PointF outlocalpoint) {//get x, y offset float localx = x + getscrollx ()-Child.getleft (); float localy = y + getscrolly ()-child.gettop (); Restore Location Final float[] Localxy = new float[2]; Localxy[0] = Localx; LOCALXY[1] = localy; Final Matrix Inversematrix = new Matrix (); Child.getmatrix (). Invert (Inversematrix); Inversematrix.mappoints (LOCALXY); Localx = localxy[0]; Localy = localxy[1]; Fill out the Data final Boolean Isinview = Pointinview (Child, Localx, localy); if (Isinview && outlocalpoint! = null) {Outlocalpoint.set (localx, localy); } return Isinview;} /*** determines whether the given point, in local coordinates is inside the view.*/private static Boolean Pointinview (view VIew, float localx, float localy) {return localx >= 0 && Localx < (view.getright ()-View.getleft ()) && localy >= 0 && localy < (View.getbottom ()-view.gettop ());}
The above method transforms the point (x, y) with the view's inverse matrix and then determines whether the transformed point falls within the rect of the view.
When I first approached the property Animation, I thought it was a very cool structure, at least from the user's point of view. But now I don't think it's a mature solution, because there are some complicated situations [note 1] that Google has not considered, so be careful when using the property Animation and the APIs associated with property Animation.
Note 1: Another complex situation is the scale operation of the view with multi-touch. Because Motionevent does not have a getrawx (int) method, it can only simulate getrawx (int) with GetX (int), and GetX (int) uses coordinates relative to viewgroup, which results in a scale calculation error.
How to determine whether a point falls within the view, and whether the finger is moved to a view