为什么我的android约束集动画的行为不一致?

时间:2019-08-23 00:22:15

标签: android animation android-activity constraintset

我正在创建一个动画,它将在两个不同活动之间的按钮按下时运行。它将从第一个活动开始向前运行,然后自动启动第二个活动,反之亦然(从第二个活动到第一个活动相反)。

我已经成功地使动画独立运行,但是当我介绍链接的活动时,它会间歇性地错误移动。两个气泡在向上移动(或向后移动)时,其大小应增大,但是有时它们会增大,但无法向上移动。

我完全是自学成才的,并且刚接触编码,所以请您原谅任何可怕的失误。指导表示赞赏。

这是我的布局:

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>

MainActivity代码:

 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只是空白屏幕,上面带有buttonAnimate。

0 个答案:

没有答案