Почему CutomView меняет свой размер в MotionLayout при фиксированной ширине?
Использую MotionLayout для анимации карточки выбранного подарка. Сама карточка это CustomView с TickerView и двумя TextView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/openGiftContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_new_loyalty_rewarding_corners_card" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:paddingVertical="@dimen/grid_3" android:orientation="vertical"> <ru.secret.secret.customviews.v3.ticker.TickerView android:id="@+id/ivIconTicker" android:layout_width="@dimen/grid_9" android:layout_height="@dimen/grid_9" tools:srcCompat="@drawable/circle_blue" /> <TextView android:id="@+id/tvTitle" style="@style/H0.TxtIconAlwaysBlack" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/grid_3_5" android:gravity="center" android:maxLines="2" android:ellipsize="end" android:paddingHorizontal="@dimen/grid_2" tools:text="word1 word2 word3 word4 word5 word6 word7 word8 word9 word10" /> <TextView android:id="@+id/tvDescription" style="@style/Body1.Regular.TxtIconNeutrals2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/grid_2_5" android:gravity="center" android:maxLines="2" android:ellipsize="end" android:paddingHorizontal="@dimen/grid_3_5" tools:text="word1 word2 word3 word4 word5 word6 word7 word8 word9 word10" tools:visibility="visible" /> </LinearLayout> |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/openGiftContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_new_loyalty_rewarding_corners_card" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:paddingVertical="@dimen/grid_3" android:orientation="vertical"> <ru.secret.secret.customviews.v3.ticker.TickerView android:id="@+id/ivIconTicker" android:layout_width="@dimen/grid_9" android:layout_height="@dimen/grid_9" tools:srcCompat="@drawable/circle_blue" /> <TextView android:id="@+id/tvTitle" style="@style/H0.TxtIconAlwaysBlack" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/grid_3_5" android:gravity="center" android:maxLines="2" android:ellipsize="end" android:paddingHorizontal="@dimen/grid_2" tools:text="word1 word2 word3 word4 word5 word6 word7 word8 word9 word10" /> <TextView android:id="@+id/tvDescription" style="@style/Body1.Regular.TxtIconNeutrals2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/grid_2_5" android:gravity="center" android:maxLines="2" android:ellipsize="end" android:paddingHorizontal="@dimen/grid_3_5" tools:text="word1 word2 word3 word4 word5 word6 word7 word8 word9 word10" tools:visibility="visible" /> </LinearLayout>
С бэка может прийти текст разной длины для TextView, но ожидаем что он будет не более двух строк. Размеры CustomView в MotionScene и Layout фрагмента установлены как
android:layout_width="match_parent" android:layout_height="wrap_content" |
android:layout_width="match_parent" android:layout_height="wrap_content"
Проблема заключается в том, что ширина везде хоть и установлена как match_parent, но я не уверен что это так во время вращения view (rotationY). Слова со второй строки обоих TextView могут уходить на первую строку во время анимации. Не знаю как это пофиксить кроме как фиксированной шириной и высотой Layout внутри CustomView. Есть ли способы получше?
сокращенный MotionScene
<?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" motion:defaultDuration="10000"> <Transition android:id="@+id/transitionOpenGiftToCongrats" motion:constraintSetEnd="@+id/endGiftScaleToCenter" motion:constraintSetStart="@id/startGiftScaleToCenter"> <KeyFrameSet> ... <!--CongratsOpenGiftView--> <KeyAttribute android:alpha="0" motion:framePosition="0" motion:motionTarget="@+id/congratsOpenGiftView" /> <KeyAttribute android:alpha="0" motion:framePosition="49" motion:motionTarget="@+id/congratsOpenGiftView" /> <KeyAttribute android:alpha="1" motion:framePosition="50" motion:motionTarget="@+id/congratsOpenGiftView" /> <KeyAttribute android:alpha="1" motion:framePosition="100" motion:motionTarget="@+id/congratsOpenGiftView" /> ... </KeyFrameSet> </Transition> <ConstraintSet android:id="@+id/startGiftScaleToCenter"> ... <Constraint android:id="@+id/congratsOpenGiftView" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="0" android:rotationY="180" android:layout_marginStart="@dimen/grid_2" android:layout_marginEnd="@dimen/grid_2" motion:layout_constraintVertical_bias="0.5" /> </ConstraintSet> <ConstraintSet android:id="@+id/endGiftScaleToCenter"> ... <Constraint android:id="@+id/congratsOpenGiftView" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="1" android:rotationY="360" android:layout_marginStart="@dimen/grid_2" android:layout_marginEnd="@dimen/grid_2" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toEndOf="parent" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" motion:layout_constraintVertical_bias="0.5" /> </MotionScene> |
<?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" motion:defaultDuration="10000"> <Transition android:id="@+id/transitionOpenGiftToCongrats" motion:constraintSetEnd="@+id/endGiftScaleToCenter" motion:constraintSetStart="@id/startGiftScaleToCenter"> <KeyFrameSet> ... <!--CongratsOpenGiftView--> <KeyAttribute android:alpha="0" motion:framePosition="0" motion:motionTarget="@+id/congratsOpenGiftView" /> <KeyAttribute android:alpha="0" motion:framePosition="49" motion:motionTarget="@+id/congratsOpenGiftView" /> <KeyAttribute android:alpha="1" motion:framePosition="50" motion:motionTarget="@+id/congratsOpenGiftView" /> <KeyAttribute android:alpha="1" motion:framePosition="100" motion:motionTarget="@+id/congratsOpenGiftView" /> ... </KeyFrameSet> </Transition> <ConstraintSet android:id="@+id/startGiftScaleToCenter"> ... <Constraint android:id="@+id/congratsOpenGiftView" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="0" android:rotationY="180" android:layout_marginStart="@dimen/grid_2" android:layout_marginEnd="@dimen/grid_2" motion:layout_constraintVertical_bias="0.5" /> </ConstraintSet> <ConstraintSet android:id="@+id/endGiftScaleToCenter"> ... <Constraint android:id="@+id/congratsOpenGiftView" android:layout_width="match_parent" android:layout_height="wrap_content" android:alpha="1" android:rotationY="360" android:layout_marginStart="@dimen/grid_2" android:layout_marginEnd="@dimen/grid_2" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toEndOf="parent" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" motion:layout_constraintVertical_bias="0.5" /> </MotionScene>
Сокращенный Layout фрагмента
<?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" android:id="@+id/rootML" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:background="@drawable/bg_choice_rewarding_liner_gradient" app:layoutDescription="@xml/open_gift_to_congrats_scene"> ... <ru.broker.feature.loyalty.impl.view.new_views.CongratsOpenGiftView android:id="@+id/congratsOpenGiftView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/grid_2" android:layout_marginEnd="@dimen/grid_2" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5"/> </androidx.constraintlayout.motion.widget.MotionLayout> |
<?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" android:id="@+id/rootML" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:background="@drawable/bg_choice_rewarding_liner_gradient" app:layoutDescription="@xml/open_gift_to_congrats_scene"> ... <ru.broker.feature.loyalty.impl.view.new_views.CongratsOpenGiftView android:id="@+id/congratsOpenGiftView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/grid_2" android:layout_marginEnd="@dimen/grid_2" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5"/> </androidx.constraintlayout.motion.widget.MotionLayout>
Дополнительно:
Опишите проблему, и специалист поможет с настройкой, исправлением ошибки или доработкой сайта. Подберём понятный план работ без лишней переписки.
Пока нет других ответов. Будьте первым, кто поможет автору.
Ответить на вопрос
Проблема, описанная в вашем вопросе, может быть вызвана несколькими факторами. Один из возможных сценариев, который может привести к изменению размера CustomView в MotionLayout при фиксированной ширине, это неправильное использование параметров разметки и ограничений, установленных в MotionLayout.
Для начала, убедитесь, что у вас правильно настроена разметка вашего MotionLayout. Проверьте, что ваш CustomView правильно размещен внутри MotionLayout и что у него установлены правильные параметры ширины и высоты.
Кроме того, убедитесь, что вы правильно настроили ограничения движения для вашего CustomView в MotionLayout. Убедитесь, что вы правильно определили начальное и конечное положение вашего CustomView, а также определили ограничения движения по оси X и Y.
Если все настройки разметки и ограничений движения верные, то возможно проблема связана с логикой вашего CustomView. Проверьте код вашего CustomView на предмет ошибок, которые могут привести к изменению его размера в MotionLayout.
Для более точной диагностики проблемы, вы можете предоставить больше информации о вашем коде разметки и логике CustomView, чтобы мы могли более точно определить возможные причины изменения размера CustomView в MotionLayout.