Почему анимация набора ограничений для Android ведет себя непоследовательно?
Я создаю анимацию, которая будет запускаться при нажатии кнопки между двумя различными действиями. Он будет двигаться вперед от первого действия, а затем автоматически запустит второе и наоборот (и обратные движения) от второго упражнения к первому.
Я успешно запустил анимацию, но когда я представляю цепочечные действия, она периодически перемещается неправильно. Два пузырька должны увеличиваться в размерах при движении вверх (или в обратном направлении), но ИНОГДА они растут, но не могут двигаться вверх в своем положении.
Я совершенно самоучка и новичок в кодировании, поэтому прошу прощения за любые ужасные ошибки. Руководство с благодарностью.
Вот мои макеты:
Activity_main (состояние 1)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cLayoutTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/cLayoutRangeBubbles"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginTop="350dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/leftBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/cLayoutRangeBubbles"
app:layout_constraintStart_toStartOf="@+id/cLayoutRangeBubbles"
app:layout_constraintTop_toTopOf="@+id/cLayoutRangeBubbles"
app:srcCompat="@drawable/ic_launcher_background" />
<ImageView
android:id="@+id/rightBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitEnd"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/cLayoutRangeBubbles"
app:layout_constraintEnd_toEndOf="@+id/cLayoutRangeBubbles"
app:layout_constraintTop_toTopOf="@+id/cLayoutRangeBubbles"
app:srcCompat="@drawable/ic_launcher_background" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Состояние 2:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cLayoutTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/cLayoutRangeBubbles"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/leftBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/cLayoutRangeBubbles"
app:layout_constraintStart_toStartOf="@+id/cLayoutRangeBubbles"
app:layout_constraintTop_toTopOf="@+id/cLayoutRangeBubbles"
app:srcCompat="@drawable/ic_launcher_background" />
<ImageView
android:id="@+id/rightBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitEnd"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/cLayoutRangeBubbles"
app:layout_constraintEnd_toEndOf="@+id/cLayoutRangeBubbles"
app:layout_constraintTop_toTopOf="@+id/cLayoutRangeBubbles"
app:srcCompat="@drawable/ic_launcher_background" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
Основной код активности:
ConstraintLayout cLayoutTop;
ConstraintSet constraintSet;
boolean goBack;
String TAG = "debug";
int animationDuration = 2000;
int pauseForLoad = 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras == null) {
goBack = false;
} else {
goBack = extras.getBoolean("goBack");
}
if (goBack == true) {
setContentView(R.layout.state2);
} else {
setContentView(R.layout.activity_main);
}
cLayoutTop = (ConstraintLayout) findViewById(R.id.cLayoutTop);
if (goBack == false) {
// happens before can see it without this
// run the animation part
final Runnable r = new Runnable() {
@Override
public void run() {
goForwards();
}
};
new Handler().postDelayed(r, pauseForLoad);
final Runnable goToDoseScreen = new Runnable() {
@Override
public void run() {
Intent intent;
intent = new Intent(MainActivity.this, Activity2.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
};
new Handler().postDelayed(goToDoseScreen, (animationDuration + pauseForLoad));
}
if (goBack == true) {
// happens before can see it without this
// run the animation part
final Runnable r = new Runnable() {
@Override
public void run() {
goBackwards();
}
};
new Handler().postDelayed(r, pauseForLoad);
final Runnable goToDoseScreen = new Runnable() {
@Override
public void run() {
Intent intent;
intent = new Intent(MainActivity.this, Activity1.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
};
new Handler().postDelayed(goToDoseScreen, (animationDuration + pauseForLoad));
}
}
public void goForwards() {
Transition changeBounds = new ChangeBounds();
changeBounds.setDuration(1000);
// changeBounds.setInterpolator(new BounceInterpolator());
TransitionManager.beginDelayedTransition(cLayoutTop,
changeBounds);
ConstraintSet clonedSet = new ConstraintSet();
clonedSet.clone(this, R.layout.state2);
clonedSet.applyTo(cLayoutTop);
}
public void goBackwards() {
Transition changeBounds = new ChangeBounds();
changeBounds.setDuration(1000);
// changeBounds.setInterpolator(new BounceInterpolator());
TransitionManager.beginDelayedTransition(cLayoutTop,
changeBounds);
ConstraintSet clonedSet = new ConstraintSet();
clonedSet.clone(this, R.layout.activity_main);
clonedSet.applyTo(cLayoutTop);
}
Activity1 и Activity2 - просто пустые экраны с кнопкой "Анимация" на них.