[英]Java android animate hide and unhide view and resize button
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.当我第二次单击时,我想:动画 GONE View 并调整按钮大小。
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.在动画完成之前,您不能调用
setVisibility(View.GONE)
。 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.