Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Using Animation to swipe views

I have a FrameLayout that recognize swipe gestures (up and down).

For example: if a swipe up are performed, I should animate the current view (that is MATCH_PARENT x MATCH_PARENT) to goes up at the same time a new view come from bottom.

I can achieve this with animations?

Upvotes: 2

Views: 6710

Answers (2)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

I solved this way:

private void swipeUp() {
    current.currentPage++;

    final View hidingView = currentView;
    TranslateAnimation hide = new TranslateAnimation(0, 0, 0, -getHeight());
    hide.setAnimationListener(new AnimationListenerAdapter() {
        @Override
        public void onAnimationEnd(Animation animation) {
            hidingView.setVisibility(View.GONE);
        }
    });
    hide.setDuration(1000);
    hidingView.startAnimation(hide);

    TranslateAnimation show = new TranslateAnimation(0, 0, getHeight(), 0);
    show.setFillAfter(true);
    show.setDuration(1000);

    View nextView = getView();
    addView(nextView, createLP());

    nextView.startAnimation(show);
    currentView = nextView;
}

Upvotes: 3

Codeman
Codeman

Reputation: 12375

If you want to actually switch views, you need to implement an AnimationListener that takes care of the animation. If you want more complex behavior like a "finger following" scroller between views, you will likely have to use something a bit more complex, but if you're just saying

if(I flicked upwards)
    move view up

then AnimationListener is perfect for you. Just make sure you set the listener to the Animation in code.

Hope this helps!

Upvotes: 1

Related Questions