我使用约束布局为 3 subviews的root:TextView
按钮
SurfaceView ( 视图,可见性= 已经消失)
单击按钮时,SurfaceView显示为( 可见性。可见性= 可见):
Y = - SurfaceView.height
到:
Y = 0
当我在按钮上单击第二次时,SurfaceView会消失( 视图。可见性= 已经删除):
Y = 0
到:
Y = - SurfaceView.height
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Animation"
android:textSize="30sp"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
android:id="@+id/display_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dsiplay"
android:textSize="30sp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
android:id="@+id/animation_view"
android:layout_width="0dp"
android:layout_height="150dp"
android:visibility="gone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
MainActivity:public class MainActivity extends Activity {
boolean isVisible = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.display_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isVisible =!isVisible;
if(isVisible){
/*
* Start Animation slide to down
* */
findViewById(R.id.animation_view).animate()
. translationY(0)
. setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
findViewById(R.id.animation_view).setY(-findViewById(R.id.animation_view).getHeight());
findViewById(R.id.animation_view).setVisibility(View.VISIBLE);
}
});
}
else {
/*
* Start Animation slide to up
* */
findViewById(R.id.animation_view).animate()
. translationY(-findViewById(R.id.animation_view).getHeight())
. setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
findViewById(R.id.animation_view).setVisibility(View.GONE);
}
});
}
}
});
}
}
当我点击第一次它不工作的时候。