Search code examples
androidandroid-animationandroid-transitions

How to load two views with different animations when activity starts in android


I've an activity with 2 Views. When an activity starts I want that 1st view should slide from TOP while 2nd view should Slide from BOTTOM.

I've followed android tutorials, but the Transition is being applied to entire Activity Layout View rather than individual Views.

I tries using TransitionSet but it didn't work. My Code:

TransitionSet set = new TransitionSet();

    View view1 = (View) findViewById(R.id.layout1);
    Transition slideUp = new Slide(Gravity.TOP);
    slideUp.addTarget(view1);
    slideUp.setStartDelay(15000);
    slideUp.setDuration(5000);
    set.addTransition(slideUp);
    TransitionManager.beginDelayedTransition((ViewGroup)view1, slideUp);


    View view2 = (View) findViewById(R.id.layout2);
    Transition slideDown = new Slide(Gravity.BOTTOM);
    slideDown.addTarget(view2);
    slideDown.setStartDelay(15000);
    slideDown.setDuration(5000);
    set.addTransition(slideDown);
    TransitionManager.beginDelayedTransition((ViewGroup)view2, slideDown);

    getWindow().setEnterTransition(set);
    getWindow().setExitTransition(set);

Where am I commiting error.

Thanks


Solution

  • You can use ObjectAnimator to animate your views. This code will translate view2 in the y direction in 500ms.

    ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(view2, "translationY", 0, -200);
    objectAnimator.setDuration(500);
    objectAnimator.start();