N
N
Nur-Magomed2016-03-22 13:31:48
Android
Nur-Magomed, 2016-03-22 13:31:48

How to implement swipe animation in android?

Hello!
We implemented swipe animation in our application: i.e. you can move the element with your finger and it disappears off the screen.
For animation, we catch the touch points of the finger on the screen and (when a certain area is reached) we start the animation of going beyond the given element.
However, this method has a drawback: the animation does not depend on the speed of the finger movement. With a sharp jerk with your finger, it will not work if it does not reach the border of the animation start.
Do you need to calculate the speed of your finger and set your own conditions or are there ready-made solutions?
Please provide links to examples or documentation.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aboyev, 2016-03-22
@efive

Notice the onFling event. It returns the movement speeds velocityX and velocityY:

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
    Toast.makeText(getApplicationContext(), "Fling Gesture", 100).show(); return true; 
}

Your script is very similar to Swipe-To-Dismiss, I found a couple of ready-made examples (I haven't tried it myself, but it seems to work):
SwipeDismissTouchListener Android 14+
NineOldAndroids port by Jake Wharton
Similar question on SO: Swipe to delete for a custom...

D
Denis Zagaevsky, 2016-03-22
@zagayevskiy

If you use a RecyclerView (and it's recommended to switch to it from a ListView, and it's quite easy), then there is a great thing, ItemTouchHelper .

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
    ...
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
        //Удалить свайпнутый элемент из списка и нотифицировать об этом ресайклер
    }
};

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
...
itemTouchHelper.attachToRecyclerView(recyclerView);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question