下面列出了android.view.MotionEvent#transform ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Recursive helper method that applies transformations in post-order.
*
* @param ev the on-screen motion event
*/
private static void transformMotionEventToLocal(@NonNull View view, @NonNull MotionEvent ev) {
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
transformMotionEventToLocal(vp, ev);
ev.offsetLocation(vp.getScrollX(), vp.getScrollY());
}
// TODO: Use reflections to access ViewRootImpl
// else if (parent instanceof ViewRootImpl) {
// final ViewRootImpl vr = (ViewRootImpl) parent;
// ev.offsetLocation(0, vr.mCurScrollY);
// }
ev.offsetLocation(-view.getLeft(), -view.getTop());
Matrix matrix = view.getMatrix();
if (matrix != null) {
ev.transform(matrix);
}
}
/**
* Recursive helper method that applies transformations in pre-order.
*
* @param ev the on-screen motion event
*/
private static void transformMotionEventToGlobal(@NonNull View view, @NonNull MotionEvent ev) {
Matrix matrix = view.getMatrix();
if (matrix != null) {
ev.transform(matrix);
}
ev.offsetLocation(view.getLeft(), view.getTop());
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
ev.offsetLocation(-vp.getScrollX(), -vp.getScrollY());
transformMotionEventToGlobal(vp, ev);
}
// TODO: Use reflections to access ViewRootImpl
// else if (parent instanceof ViewRootImpl) {
// final ViewRootImpl vr = (ViewRootImpl) parent;
// ev.offsetLocation(0, -vr.mCurScrollY);
// }
}
/**
* Recursive helper method that applies transformations in post-order.
*
* @param ev the on-screen motion event
*/
private static void transformMotionEventToLocal(@NonNull View view, @NonNull MotionEvent ev) {
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
transformMotionEventToLocal(vp, ev);
ev.offsetLocation(vp.getScrollX(), vp.getScrollY());
}
// TODO: Use reflections to access ViewRootImpl
// else if (parent instanceof ViewRootImpl) {
// final ViewRootImpl vr = (ViewRootImpl) parent;
// ev.offsetLocation(0, vr.mCurScrollY);
// }
ev.offsetLocation(-view.getLeft(), -view.getTop());
Matrix matrix = view.getMatrix();
if (matrix != null) {
ev.transform(matrix);
}
}
/**
* Recursive helper method that applies transformations in pre-order.
*
* @param ev the on-screen motion event
*/
private static void transformMotionEventToGlobal(@NonNull View view, @NonNull MotionEvent ev) {
Matrix matrix = view.getMatrix();
if (matrix != null) {
ev.transform(matrix);
}
ev.offsetLocation(view.getLeft(), view.getTop());
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
ev.offsetLocation(-vp.getScrollX(), -vp.getScrollY());
transformMotionEventToGlobal(vp, ev);
}
// TODO: Use reflections to access ViewRootImpl
// else if (parent instanceof ViewRootImpl) {
// final ViewRootImpl vr = (ViewRootImpl) parent;
// ev.offsetLocation(0, -vr.mCurScrollY);
// }
}
/**
* Recursive helper method that applies transformations in post-order.
*
* @param ev the on-screen motion event
*/
private static void transformMotionEventToLocal(@NonNull View view, @NonNull MotionEvent ev) {
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
transformMotionEventToLocal(vp, ev);
ev.offsetLocation(vp.getScrollX(), vp.getScrollY());
} // TODO: Use reflections to access ViewRootImpl
// else if (parent instanceof ViewRootImpl) {
// final ViewRootImpl vr = (ViewRootImpl) parent;
// ev.offsetLocation(0, vr.mCurScrollY);
// }
ev.offsetLocation(-view.getLeft(), -view.getTop());
Matrix matrix = view.getMatrix();
if (matrix != null) {
ev.transform(matrix);
}
}
/**
* Recursive helper method that applies transformations in pre-order.
*
* @param ev the on-screen motion event
*/
private static void transformMotionEventToGlobal(@NonNull View view, @NonNull MotionEvent ev) {
Matrix matrix = view.getMatrix();
if (matrix != null) {
ev.transform(matrix);
}
ev.offsetLocation(view.getLeft(), view.getTop());
final ViewParent parent = view.getParent();
if (parent instanceof View) {
final View vp = (View) parent;
ev.offsetLocation(-vp.getScrollX(), -vp.getScrollY());
transformMotionEventToGlobal(vp, ev);
} // TODO: Use reflections to access ViewRootImpl
// else if (parent instanceof ViewRootImpl) {
// final ViewRootImpl vr = (ViewRootImpl) parent;
// ev.offsetLocation(0, -vr.mCurScrollY);
// }
}
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
MotionEvent eventCopy = MotionEvent.obtain(event);
eventCopy.transform(inverse);
boolean result = super.dispatchTouchEvent(eventCopy);
eventCopy.recycle();
return result;
}
private MotionEvent rotateTouchEvent(MotionEvent ev) {
if (this.getMapOrientation() == 0)
return ev;
MotionEvent rotatedEvent = MotionEvent.obtain(ev);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
getProjection().unrotateAndScalePoint((int) ev.getX(), (int) ev.getY(),
mRotateScalePoint);
rotatedEvent.setLocation(mRotateScalePoint.x, mRotateScalePoint.y);
} else {
// This method is preferred since it will rotate historical touch events too
rotatedEvent.transform(getProjection().getInvertedScaleRotateCanvasMatrix());
}
return rotatedEvent;
}
@TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
private static MotionEvent transformEventNew(MotionEvent e, Matrix m) {
MotionEvent newEvent = MotionEvent.obtain(e);
newEvent.transform(m);
return newEvent;
}
private static void transformToPagerEvent(MotionEvent event, View view, ViewPager pager) {
tmpMatrix.reset();
transformMatrixToPager(tmpMatrix, view, pager);
event.transform(tmpMatrix);
}