如何使用动作布局在父视图中缩放textView?

时间:2018-12-03 22:37:08

标签: android android-layout android-motionlayout

我正在尝试在容器视图内缩放textView。活动使用动作布局。如果不将textView放在容器内,则可以缩放它。这是我的活动布局和动作布局描述文件。如何使scaleX和scaleY工作?

活动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.motion.MotionLayout xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/motionLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutDescription="@xml/motion_scene_text_size">

    <View
        android:id="@+id/button"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:background="@color/colorAccent"
        android:text="Button" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.motion.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:constraintSetStart="@+id/start"
        motion:constraintSetEnd="@+id/end"
        motion:duration="1000">
        <OnSwipe
            motion:touchAnchorId="@+id/button"
            motion:touchAnchorSide="right"
            motion:dragDirection="dragRight" />
    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            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" />

        <Constraint
            android:id="@id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="20sp"
            android:scaleX="1.0"
            android:scaleY="1.0"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            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" />

        <Constraint
            android:id="@id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="20sp"
            android:scaleX="2.0"
            android:scaleY="2.0"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

    </ConstraintSet>

</MotionScene>

2 个答案:

答案 0 :(得分:1)

值得注意的是,您只能为MotionLayout的直接子项设置动画。

如果必须使用此容器,则可以将matchParent用于TextView的heightwidth,然后使用AutoSizeText

<MotionLayout
   ...>

   <ConstraintLayout
    android:id="@+id/container"
    ...>

      <TextView
       android:layout_width="matchParent"
       android:layout_height="matchParent"
       app:autoSizeTextType="uniform"
       app:autoSizeMinTextSize="20sp"
       app:autoSizeMaxTextSize="40sp"
       .../>

   </ConstraintLayout>

</MotionLayout>

在您的MotionScene中,更改ConstraintLayout的大小:

<ConstraintSet android:id="@+id/endConstraintSet">

   <Constraint
    android:id="@id/container"
    android:layout_width="200dp"
    android:layout_height="200dp"
    ... />

</ConstraintSet />

答案 1 :(得分:0)

对我有用的是设置TextView的 HEIGHT TextSize 一起调整大小,例如:

<Transition
    motion:constraintSetEnd="@+id/end"
    motion:constraintSetStart="@+id/start" />

<ConstraintSet android:id="@+id/start">
    <Constraint android:id="@id/titleTextView">
        <Layout
            ........
            android:layout_width="match_parent"
            android:layout_height="32dp"
            .......
            />
        <CustomAttribute
            motion:attributeName="textSize"
            motion:customFloatValue="28" />
    </Constraint>
    ......
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Constraint android:id="@id/titleTextView">
        <Layout
            android:layout_width="match_parent"
            android:layout_height="20dp"
            ..........
        />
        <CustomAttribute
            motion:attributeName="textSize"
            motion:customFloatValue="16" />
    </Constraint>
</ConstraintSet>
相关问题