如何设置高度变化的动画

时间:2017-06-07 20:34:13

标签: android animation

我的应用程序中有一张简单的卡片,当用户点击它时,我正在改变高度。这种高度变化我喜欢动画,高度平滑变化。

@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;
    }
}

我怎么能这样做?

2 个答案:

答案 0 :(得分:9)

解决了这个问题:

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;
                }
            }
        });
    }
}

答案 1 :(得分:5)

您需要在应用新尺寸时设置布局转换,请查看此developer.android.com/training/animation/layout.html

id

示例代码来源 https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50

enter image description here