簡體   English   中英

Java android動畫隱藏和取消隱藏視圖和調整大小按鈕

[英]Java android animate hide and unhide view and resize button

我嘗試對此進行動畫處理:

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

在動畫完成之前,您不能調用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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM