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?
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
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;
}
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With