Wednesday, July 18, 2018

VIEW POSITION MOVE

Animations with just one line of code. What’s interesting here is that not only the text visibility was animated, but also a button position. The Transition framework automatically animates layout change caused by appearance of TextView, so you don’t have to do it by yourself. You can even start new animation while old one is still running. The Transition framework will stop animation in progress and then continue to animate views from their current position. All under the hood, automagically.
We can specify the exact Transition types that will be applied via the second parameter of beginDelayedTransition method.
XML CODE
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/transitions_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center"
             android:orientation="vertical">
 
    <Button
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="DO MAGIC"/>
 
    <TextView
       android:id="@+id/text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="16dp"
       android:text="Transitions are awesome!"
       android:visibility="gone"/>
 
</LinearLayout>
JAVA CODE
final ViewGroup transitionsContainer = (ViewGroup)findViewById(R.id.transitions_container);
final TextView text = (TextView) transitionsContainer.findViewById(R.id.text);
final Button button = (Button) transitionsContainer.findViewById(R.id.button);
 
button.setOnClickListener(new View.OnClickListener() {

    boolean visible;

    @Override
    public void onClick(View v) {
        TransitionManager.beginDelayedTransition(transitionsContainer);
        visible = !visible;
        text.setVisibility(visible ? View.VISIBLE : View.GONE);          
    }

});
RESULT


No comments:

Post a Comment