Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate top and bottom dimensions of a view

I need to do two things with a view:

  1. Move top dimension to the very top of the window
  2. Move bottom dimension to the very bottom of the window.

In short, I need the view to cover 100% of the parent view.

Translation animation didn't work because it moves the view but it doesn't increase the size.

Scale animation works but it stretches the content of the view and I don't want that. I want to increase the visible area, not stretch the content to fit the new dimensions.

What's the correct way to do this?

like image 361
StackOverflower Avatar asked Jul 31 '17 13:07

StackOverflower


3 Answers

That can be easily achieved with Transitions API.

With Transitions API you do not take care of writing animations, you just tell what you want the end values be and Transitions API would take care of constructing animations.

Having this xml as content view (a view in the center of the screen):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view"
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="@color/colorAccent" />

</FrameLayout>

In activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.item)

    val root = findViewById(R.id.root) as ViewGroup
    val view = findViewById(R.id.view)

    view.setOnClickListener {

        // After this line Transitions API would start counting the delta
        // and will take care of creating animations for each view in `root`
        TransitionManager.beginDelayedTransition(root)

        // By default AutoTransition would be applied,
        // but you can provide your transition with the second parameter

        // val transition = AutoTransition()
        // transition.duration = 2000
        // TransitionManager.beginDelayedTransition(root, transition)

        // We are changing size of the view to match parent
        val params = view.layoutParams
        params.height = ViewGroup.LayoutParams.MATCH_PARENT
        params.width = ViewGroup.LayoutParams.MATCH_PARENT

        view.requestLayout()
    }
}

Here's the output:

Platform's Transitions API (android.transition.TransitionManager) is available from API 19, but support libraries backport the functionality upto API 14 (android.support.transition.TransitionManager).

like image 107
azizbekian Avatar answered Nov 01 '22 03:11

azizbekian


I like to keep everything as simple as it can be.

so my suggestion would be using a android Animating Layout Changes

Here is a sample:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:animationCache="true">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:background="@color/colorPrimary"
        android:layout_gravity="center" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView textView;

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

        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    protected void onResume() {
        super.onResume();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                View view = getWindow().getDecorView();

                int height = getWindow().getDecorView().getHeight();
                int width = getWindow().getDecorView().getWidth();
                textView.setLayoutParams(new LinearLayout.LayoutParams(width, height));

                LayoutTransition layoutTransition = ((ViewGroup) textView.getParent()).getLayoutTransition();
                layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
            }
        }, 2000);
    }
}
like image 24
zombie Avatar answered Nov 01 '22 02:11

zombie


You can try using ValueAnimator as shown in this answer: https://stackoverflow.com/a/32835417/3965050

Note: I wanted to write this as a comment, but I don't have the reputation. This should not be considered as a full answer.

like image 1
Matias Olocco Avatar answered Nov 01 '22 02:11

Matias Olocco