---
# System prepended metadata

title: Scene Transition Animation
tags: [android, animation, scene, layout transition]

---

# Scene Transition Animation
###### tags: `android` `scene` `layout transition` `animation`

Android 提供 Animation API 可以做 Layout 之間的過渡動畫。只要設定起終的layout，Android Animation Framework會自動完成過渡動畫。

```java
public class MainActivity extends AppCompatActivity
        implements View.OnClickListener {

    ViewGroup sceneRoot;
    Scene scene1;
    Scene scene2;

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

        sceneRoot = findViewById(R.id.container);
        scene1 = Scene.getSceneForLayout(sceneRoot, R.layout.fragment_scene1, this);
        scene2 = Scene.getSceneForLayout(sceneRoot, R.layout.fragment_scene2, this);

        goToScene1();
    }

    private void goToScene1() {
        goTransition(scene1);
        findViewById(R.id.btnStart).setOnClickListener(this);
    }

    private void goToScene2() {
        goTransition(scene2);
        findViewById(R.id.btnReset).setOnClickListener(this);
    }
    
    private void goTransition(Scene scene) {

        ChangeBounds changeBounds = new ChangeBounds();
        changeBounds.setDuration(300);

        Fade fadeOut = new Fade(Fade.OUT);
        fadeOut.setDuration(400);

        Fade fadeIn = new Fade(Fade.IN);
        fadeIn.setDuration(300);

        TransitionSet transitionSet = new TransitionSet();
        transitionSet.setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
        transitionSet
                .addTransition(fadeOut)
                .addTransition(changeBounds)
                .addTransition(fadeIn);

        TransitionManager.go(scene, transitionSet);
    }
    
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btnStart) {
            goToScene2();
        }else if(view.getId() == R.id.btnReset) {
            goToScene1();
        }
    }
}
```