I try to animate this:
<?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>
When I clicked first time button I want to animate two actions: animate show View and animate resize a button
And when I clicked second time I want to: animate GONE View and resize a button.
I do this but it does not work good:
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);
}
}
You can not call setVisibility(View.GONE)
until the animation finishes. So you have to listen for the animation and change visibility when animation ends.
Updated code:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.