我使用Constraint Layout作为root,包含3个子视图:
我希望当我点击按钮时,SurfaceView出现(View.Visibility = Visible)来自:
Y = - SurfaceView.height
为:
Y = 0
当我再次点击按钮时,SurfaceView将从以下位置消失(View.Visibility = Gone):
Y = 0
为:
Y = - SurfaceView.height
<android.support.constraint.ConstraintLayout
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">
<TextView
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" />
<Button
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" />
<SurfaceView
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" />
</android.support.constraint.ConstraintLayout>
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);
}
});
}
}
});
}
}
当我第一次点击时,它不起作用(向下滑动)。
答案 0 :(得分:0)
您的SurfaceView
没有正确的初始条件。这些条件在完成动画后会自行纠正,这就是后续动画工作的原因。将以下内容添加到SurfaceView
XML:
android:translationY="-150dp"
您也可以在代码中执行此操作,但您需要在onClick()
处理程序之前执行此操作。
答案 1 :(得分:0)
如果它可以帮助某人。
ConstraintLayout
<android.support.constraint.ConstraintLayout>
...
<SurfaceView
android:id="@+id/animation_view"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:visibility="gone"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:translationY="-150dp" />
</android.support.constraint.ConstraintLayout>
MainActivity
public class MainActivity extends Activity {
float translationY;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
translationY = findViewById(R.id.animation_view).getTranslationY();
findViewById(R.id.display_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(findViewById(R.id.animation_view).getVisibility() == View.GONE){
/*
* 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).setVisibility(View.VISIBLE);
}
});
}
else if(findViewById(R.id.animation_view).getVisibility() == View.VISIBLE){
/*
* Start Animation slide to up
* */
findViewById(R.id.animation_view).animate()
.translationY(translationY)
.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);
}
});
}
}
});
}
}