Animate a view from one Layout to other Layout

后端 未结 2 404
时光说笑
时光说笑 2021-02-03 12:15

Check attached image for easy explanation.

Translate animation works but it animates inside the same view. I want view to fly out from one layout to other.

2条回答
  •  情书的邮戳
    2021-02-03 13:03

    If someone is looking for simpler solution, you can use transitions framework: https://developer.android.com/training/transitions/index.html

    To animate translation from one parent view to another, there is special transition ChangeTransform: https://developer.android.com/reference/android/transition/ChangeTransform.html

    And a small example:

    animatedView = View.inflate(ActivityMain.this, R.layout.item, firstParent);
    animatedView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    Transition move = new ChangeTransform()
                            .addTarget(animatedView)
                            .setDuration(2000));
    TransitionManager.beginDelayedTransition(rootParent, move);
    firstParent.removeView(animatedView);
    animatedView.setPadding(2,2,2,2);
    animatedView.setElevation(4);
    secondParent.addView(animatedView, 0);
    

提交回复
热议问题