Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

android - Back Transitions In Motion Layout

I am trying to do a simple animation where on swiping the textview the button becomes visible and when clicking the button the button becomes invisible. The problem is I am able to do it the first time but when I try to do it again I need to reverse swipe the textview in the opposite direction before swiping again to show the button. It is like clicking the button makes it invisible but doesn't set the swipe action for textview.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/activity_check_scene"
    app:motionDebug="SHOW_ALL"
    >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="152dp"
        android:layout_marginEnd="24dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="152dp"
        android:text="Button"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />
</androidx.constraintlayout.motion.widget.MotionLayout>

<?xml version="1.0" encoding="utf-8"?>
<MotionScene

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@+id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000">
       <KeyFrameSet>
       </KeyFrameSet>
        <OnSwipe
            motion:touchAnchorId="@+id/textView3"
            motion:dragDirection="dragLeft"
            motion:touchAnchorSide="left" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@+id/button2"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            motion:layout_constraintTop_toBottomOf="@+id/textView3"
            motion:layout_constraintHorizontal_bias="0.498"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="152dp" />
        <Constraint
            android:id="@+id/textView3"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginStart="24dp"
            motion:layout_constraintHorizontal_bias="1.0"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="152dp" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/button2"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            motion:layout_constraintTop_toBottomOf="@+id/textView3"
            motion:layout_constraintHorizontal_bias="0.498"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="152dp" />
        <Constraint
            android:id="@+id/textView3"
            motion:layout_constraintEnd_toEndOf="parent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="24dp"
            android:layout_marginStart="24dp"
            motion:layout_constraintHorizontal_bias="1.0"
            motion:layout_constraintTop_toTopOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="152dp" />
    </ConstraintSet>
    <Transition
        android:id="@+id/back"
        motion:constraintSetStart="@+id/end"
        motion:constraintSetEnd="@+id/start" >
        <OnClick
            motion:clickAction="toggle"
            motion:targetId="@+id/button2" />
    </Transition>
</MotionScene>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't work because after the swipe the first transition is in the end state. So, in order for you to be able to swipe again to display the button, you need to set it somehow in the start position. The easiest solution would be to move the click trigger into the first transition, like this:

<Transition
        motion:constraintSetEnd="@id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000">
    <OnSwipe
            motion:touchAnchorId="@+id/textView3"
            motion:dragDirection="dragLeft"
            motion:touchAnchorSide="left"/>
    <OnClick
            motion:clickAction="toggle"
            motion:targetId="@+id/button2" />
</Transition>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...