0

I am trying to implement a simple animation through motionLayout, the aim is to click to move an image, but the constraint sets arent allowing duplicate ids

<ConstraintSet android:id="@+id/starting_set">
        <Constraint
            android:id="@+id/tracker"
            app:layout_constraintBottom_toBottomOf="@+id/t1"
            tools:layout_editor_absoluteX="167dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            />
 </ConstraintSet>

there is an error on the ending constraint, on constraint id (tracker) of duplicacy

Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48

2 Answers2

1

for most of drag animation you don't need to to have duplicates at all. Eg sample to move view horizontally after click and drag

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

    <Transition
        motion:constraintSetStart="@layout/motion_01_cl_start"
        motion:constraintSetEnd="@layout/motion_01_cl_end"
        motion:duration="1000">
        <OnSwipe
            motion:touchAnchorId="@+id/button"
            motion:touchAnchorSide="right"
            motion:dragDirection="dragRight" />
    </Transition>

</MotionScene> 

you can fin more samples here
https://github.com/googlesamples/android-ConstraintLayoutExamples

also if you have several Constraint make sure they have Constraint name and they are in different sets. Sample below will not work and will complain about duplicates. If you cange Button to Constraint than it will work.

<Transition
    motion:constraintSetEnd="@+id/end"
    motion:constraintSetStart="@+id/start"
    motion:duration="1000"
    motion:motionInterpolator="linear">
    <OnSwipe
        motion:dragDirection="dragRight"
        motion:touchAnchorId="@id/button"
        motion:touchAnchorSide="right" />
</Transition>

<ConstraintSet android:id="@+id/start">
    <Button
        android:id="@id/button"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_marginStart="8dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent">
        <CustomAttribute
            motion:attributeName="BackgroundColor"
            motion:customColorValue="#D81B60" />
    </Button>
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Button
        android:id="@id/button"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_marginEnd="8dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintTop_toTopOf="parent">
        <CustomAttribute
            motion:attributeName="BackgroundColor"
            motion:customColorValue="#9999FF" />
    </Button>
</ConstraintSet>

Yarh
  • 4,459
  • 5
  • 45
  • 95
0

@Yarh Answer is correct. Here is the short version for my case: I accidently just copied the code from the layout-xml into the scene file. But I forgot to change the xml tag to Constraint. Doing so removed the error about DuplicateIds.

muetzenflo
  • 5,653
  • 4
  • 41
  • 82