Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate the height changing

I have a simple card in my app and when the user click on it, I'm changing the height. This change of height I'd like to be animated, a smooth change of height.

@OnClick({R.id.emptyDescription})
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.emptyDescription:
            if(isCardCollapsed){

                emptyDescription.setMinimumHeight(400);

                cancelSaveLinear.setVisibility(View.VISIBLE);
                editDescription.setVisibility(View.GONE);

                isCardCollapsed = false;
            } else {
                emptyDescription.setMinimumHeight(100);

                cancelSaveLinear.setVisibility(View.GONE);
                editDescription.setVisibility(View.VISIBLE);

                isCardCollapsed = true;
            }

            break;
        case R.id.card_view:
            break;
    }
}

How could I do it?

like image 881
Guilherme Lima Pereira Avatar asked Jun 07 '17 20:06

Guilherme Lima Pereira


2 Answers

you need to set layout transition when applying the new size check out this developer.android.com/training/animation/layout.html

TransitionManager.beginDelayedTransition(transitionsContainer, new TransitionSet()
    .addTransition(new ChangeBounds())
    .addTransition(new ChangeImageTransform()));

ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.height = expanded ? ViewGroup.LayoutParams.MATCH_PARENT : 
    ViewGroup.LayoutParams.WRAP_CONTENT;
imageView.setLayoutParams(params);

imageView.setScaleType(expanded ? ImageView.ScaleType.CENTER_CROP : 
    ImageView.ScaleType.FIT_CENTER);

Source of Sample Code https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50

enter image description here

like image 55
Elias Fazel Avatar answered Sep 30 '22 07:09

Elias Fazel


Solved with this:

public class MainActivity extends AppCompatActivity {

    private CardView cardView;
    private Boolean isCollapsed = true;

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

        cardView = (CardView) findViewById(R.id.card_view);

        final int originalDimension = Math.round(getResources().getDimension(R.dimen.original_card_view_height));
        final int newDimension = Math.round(getResources().getDimension(R.dimen.new_card_view_height));

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isCollapsed){
                    TransitionManager.beginDelayedTransition(cardView, new TransitionSet()
                            .addTransition(new ChangeBounds()));

                    ViewGroup.LayoutParams params = cardView.getLayoutParams();
                    params.height = newDimension;
                    cardView.setLayoutParams(params);

                    isCollapsed = false;
                } else {
                    TransitionManager.beginDelayedTransition(cardView, new TransitionSet()
                            .addTransition(new ChangeBounds()));

                    ViewGroup.LayoutParams params = cardView.getLayoutParams();
                    params.height = originalDimension;
                    cardView.setLayoutParams(params);

                    isCollapsed = true;
                }
            }
        });
    }
}
like image 34
Guilherme Lima Pereira Avatar answered Sep 30 '22 06:09

Guilherme Lima Pereira