在 ConstraintLayout 中滑动 Up/Down SurfaceView
Slide Up/Down SurfaceView in ConstraintLayout
我使用 Constraint Layout 作为 root 和 3 个子视图:
- 文本视图
- 按钮
- SurfaceView(View.Visibility = 消失)
我希望当我点击 Button 时,SurfaceView 出现(View.Visibility = 可见)来自:
Y = - SurfaceView.height
至:
Y = 0
当我第二次点击按钮时,SurfaceView 消失(View.Visibility = 消失)来自:
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>
主要活动:
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);
}
});
}
}
});
}
}
当我第一次点击时它不起作用(向下滑动)。
您的 SurfaceView
没有正确的初始条件。这些条件在经过动画后会自行纠正,因此这就是后续动画起作用的原因。将以下内容添加到 SurfaceView
XML:
android:translationY="-150dp"
您也可以在代码中执行此操作,但您需要比在 onClick()
处理程序中更早执行此操作。
如果能帮到一些人。
约束布局
<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>
主要活动
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);
}
});
}
}
});
}
}
我使用 Constraint Layout 作为 root 和 3 个子视图:
- 文本视图
- 按钮
- SurfaceView(View.Visibility = 消失)
我希望当我点击 Button 时,SurfaceView 出现(View.Visibility = 可见)来自:
Y = - SurfaceView.height
至:
Y = 0
当我第二次点击按钮时,SurfaceView 消失(View.Visibility = 消失)来自:
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>
主要活动:
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);
}
});
}
}
});
}
}
当我第一次点击时它不起作用(向下滑动)。
您的 SurfaceView
没有正确的初始条件。这些条件在经过动画后会自行纠正,因此这就是后续动画起作用的原因。将以下内容添加到 SurfaceView
XML:
android:translationY="-150dp"
您也可以在代码中执行此操作,但您需要比在 onClick()
处理程序中更早执行此操作。
如果能帮到一些人。
约束布局
<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>
主要活动
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);
}
});
}
}
});
}
}