问题描述
我正在尝试制作一个包含动画的应用程序。我有一个按钮,上面有一个由 ConstraintLayout 保存的 TextView。我希望 Button 和 TextView 一起移动,所以当我开始动画时,我在 ConstraintView 上启动它。动画播放 Button 后,TextView 移动但 Button 的点击区域停留在原来的位置。
动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:ordering="together"
android:fillAfter="true">
<set android:interpolator="@android:anim/bounce_interpolator">
<translate
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="0.0"
android:toYDelta="30%p"
android:duration="1000"/>
</set>
</set>
视图:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/glButton">
<LinearLayout
android:id="@+id/llHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="10" />
<Button
android:id="@+id/btRandom"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="80"
android:background="@drawable/button_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTitle" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="10" />
</LinearLayout>
<TextView
android:id="@+id/tvButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/albas"
android:text="Sorsol"
android:textColor="@color/white"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java 代码:
btRandom = (Button)findViewById(R.id.btRandom);
clHolder = (ConstraintLayout)findViewById(R.id.clHolder);
clScreen = (ConstraintLayout)findViewById(R.id.clScreen); //this is the root view of the activity
gravityFall = AnimationUtils.loadAnimation(this,R.anim.gravity_fall);
gravityFall.setAnimationListener(gravityListener);
btRandom.setonClickListener((View v) -> {
clHolder.startAnimation(gravityFall);
});
我尝试使用 AnimationListener 修复它,因此每当动画结束时,它都会手动设置 Button 位置,但这只会导致 Button 完全消失。 那个代码:
@Override
public void onAnimationEnd(Animation animation) {
btRandom.setEnabled(true);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,70);
params.leftMargin = (clScreen.getWidth() / 2) - (btRandom.getWidth() / 2);
params.topMargin = (int) (clScreen.getHeight() * .8); //Originally it is at 50% of the screen and then it falls by 30% of the scree and that is the 80%
btRandom.setLayoutParams(params);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)