directory

  • preface
  • Preliminary understanding of gesture operation
  • Six rewriting methods
  • Chestnut experience it
  • Don’t forget the double click event
  • The last

preface

Gestures are crucial to Android interactions. It can be said that if a piece of software does not have good gestures, it cannot be defined as mobile software. Here’s a look at the GestureDetector class provided by Google. Use with attribute animation, the effect is better.


Preliminary understanding of gesture operation

Let’s start with a quick look at the gesture-manipulation class. . Here with the GestureDetector SimpleOnGestureListener (), for the time being understood as an adapter, rapid implementation GestureDetector. OnGestureListener interface to realize the method. That way, you can implement on demand without having to rewrite all the methods.

public class MainActivity extends AppCompatActivity {

    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btHello = (Button) findViewById(R.id.bt_hello);

        mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                Toast.makeText(MainActivity.this,
                        "onDown", Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                Toast.makeText(MainActivity.this,
                        "onShowPress", Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                Toast.makeText(MainActivity.this,
                        "onSingleTapUp", Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Toast.makeText(MainActivity.this,
                        "onScroll", Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                Toast.makeText(MainActivity.this,
                        "onLongPress", Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Toast.makeText(MainActivity.this,
                        "onFling", Toast.LENGTH_SHORT).show();
                return false; }}); btHello.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                returnmGestureDetector.onTouchEvent(event); }}); }}Copy the code

As you can see, gestures actually enrich the original touch events. Interception occurs at the touch implementation. Then there are six methods that can be overridden. Let’s analyze these methods.


Six rewriting methods

  • onDownThat’s easy to understand. It’sPress theIn the operation.
  • onShowPressWhat this means isPress the.What is pressing, just thanPress theA little harder, a little more time.
  • onLongPressThis is theLong pressFor example, if you keep pressing the button, the order of execution isonDown–>onShowPress–>onLongPress.
  • onSingleTapUpThis is theSingle point 23:43..When you click the button and raise your hand. But there is one caveat. If you click for less thanonLongPressIt will trigger if it is greater thanonLongPress, it won’t trigger.
  • onScrollDrag the corresponding. That is, drag a control, this will trigger.
  • onFlingSwipe corresponding, just a quick swipe, and it triggers.

Chestnut experience it

A little bit. Let’s try it out.

We go around with the button, and we look at the print in the background.

onDown
onShowPress
onScroll
onScroll
onScroll
onScroll
onScroll
onScroll
Copy the code

And then we’ll do it again, click on it, go around it. What happens when you click:

onDown
onShowPress
onSingleTapUp
Copy the code

Going around is:

onDown
onShowPress
onScroll
onScroll
onScroll
onFling
Copy the code

Let’s take a look at the click event, click onDown, a little bit more will trigger onShowPress, and raise your hand will trigger onSingleTapUp.

And then analyze the first loop, go onDown, stay on onShowPress, drag onScroll, and then drag it multiple times. The second time the onFling is in place, the onFling is not 100% while the drag is in place. Therefore, it is important to distinguish between these two operations to avoid conflicts.


Don’t forget the double click event

On PCS, double-clicking events are very common, but on mobile, they are not used as much. But let’s see. Chestnuts first:

@Override
public boolean onDoubleTap(MotionEvent e) {
    Toast.makeText(MainActivity.this,
            "onDoubleTap", Toast.LENGTH_SHORT).show();
    LogUtil.i("onDoubleTap");
    return super.onDoubleTap(e);
}
Copy the code

After double clicking the button:

onDown
onShowPress
onSingleTapUp
onDoubleTap
onDown
onShowPress
Copy the code

There’s the new onDoubleTap, where quick clicks are recognized. Why can join directly, because the GestureDetector. SimpleOnGestureListener also realized the GestureDetector of (). The content of OnDoubleTapListener. If you don’t use GestureDetector. SimpleOnGestureListener (), you can use the following code to achieve GestureDetector. OnDoubleTapListener interface.

class MyDoubleTap implements GestureDetector.OnDoubleTapListener{

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false; }}Copy the code

So GestureDetector. SimpleOnGestureListener () really good ah, but, if you have multiple objects to use or to copy.


The last

There are a lot of new gestures, and definitely more than the ones I wrote about in my article. But that’s enough to get started and make most gestures. Like remember to like or follow me oh.