Java android动画隐藏和取消隐藏视图和调整大小按钮

时间:2019-04-05 21:10:40

标签: java android

我尝试制作动画:

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

    <View
        android:id="@+id/theView"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:visibility="gone"
        android:layout_height="match_parent"
        android:background="#111111"
        />
    <Button
        android:id="@+id/toggleButton"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:text="Click to Toggle"
        />

</LinearLayout>

当我单击“第一次”按钮时,我想制作两个动作的动画:  动画显示查看和设置按钮动画大小

当我第二次单击时,我要:为GONE View设置动画并调整按钮的大小。

我这样做,但是效果不好:

View viewToAnimate;
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button  = (Button)findViewById(R.id.toggleButton);
    button.setOnClickListener(this);

    viewToAnimate = findViewById(R.id.theView);
}

@Override
public void onClick(View v) {
    if(viewToAnimate.getVisibility() == View.VISIBLE) {
        Animation out = AnimationUtils.makeOutAnimation(this, false);
        viewToAnimate.startAnimation(out);
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        button.startAnimation(in);
        viewToAnimate.setVisibility(View.GONE);
    } else {
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        Animation out = AnimationUtils.makeOutAnimation(this, true);
        button.startAnimation(out);
        viewToAnimate.startAnimation(in);
        viewToAnimate.setVisibility(View.VISIBLE);
    }
}

1 个答案:

答案 0 :(得分:0)

在动画结束之前,您无法调用setVisibility(View.GONE)。因此,您必须听动画并在动画结束时更改可见性。

更新的代码:

View viewToAnimate;
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button  = (Button)findViewById(R.id.toggleButton);
    button.setOnClickListener(this);

    viewToAnimate = findViewById(R.id.theView);
}

@Override
public void onClick(View v) {
    if(viewToAnimate.getVisibility() == View.VISIBLE) {
        Animation out = AnimationUtils.makeOutAnimation(this, false);
        viewToAnimate.startAnimation(out);
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        button.startAnimation(in);
        out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewToAnimate.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    } else {
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        Animation out = AnimationUtils.makeOutAnimation(this, true);
        button.startAnimation(out);
        viewToAnimate.setVisibility(View.VISIBLE);
        viewToAnimate.startAnimation(in);
    }
}