I am trying to add an onTouchEvent
to a View
, but I then see the deprecation support on this link: https://developer.android.com/sdk/support_api_diff/26.0.0-alpha1/changes/android.support.v4.view.MotionEventCompat.html.
It says, they are all deprecated. So what code should be used to replace the MotionEventCompat
functions?
You can instead use the MotionEvent
object directly to achieve the same.
Please see the example & comparison as below:
public boolean onTouch(MotionEvent motionEvent) {
// previously you would do this
final int action = MotionEventCompat.getActionMasked(motionEvent);
// now you would do this
final int action = motionEvent.getActionMasked();
}
I think you are confused, as some of the Android documentation has yet to update their sample code, so some of their sample code is still using the deprecated methods.
For more info, please read here.
Hope this helps.
I know it is an old question but, I had to refactor some code and, I had to update these deprecated functions. It is easy to refactorize it, just do this:
MotionEventCompat.ACTION_MASK
, then replace them for MotionEvent.ACTION_MASK
MotionEventCompat.getPointerId(ev, index);
do this instead: ev.getPointerId(index);
And that is all! I will list a few more examples to be clearer:
MotionEventCompat.ACTION_POINTER_DOWN
by MotionEvent.ACTION_POINTER_DOWN
MotionEventCompat.ACTION_POINTER_UP
by MotionEvent.ACTION_POINTER_UP
MotionEventCompat.getX(ev, pointerIndex);
by ev.getX(pointerIndex);
MotionEventCompat.getY(ev, pointerIndex);
by ev.getY(pointerIndex);
MotionEventCompat.getActionIndex(ev);
by ev.getActionIndex();
Also (not directly related to this question) I had to replace VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId);
by velocityTracker.getXVelocity(mActivePointerId);
I hope this can help someone else in the future :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With