안드로이드

안드로이드 유튜브 스타일 모션레이아웃(3) - 모션레이아웃 적용하기[1]

알통몬_ 2020. 3. 18. 12:31
반응형


공감 및 댓글은 포스팅 하는데

 아주아주 큰 힘이 됩니다!!

포스팅 내용이 찾아주신 분들께 

도움이 되길 바라며

더 깔끔하고 좋은 포스팅을 

만들어 나가겠습니다^^

 


이번에는 FragmentHome의 item을 클릭했을 때

왼쪽 사진 -> 오른쪽 사진 으로 변하는 MotionLayout을 구성해보겠습니다.


#1. ConstraintLayout을 MotionLayout으로 바꾸기

activity_main의 최상위 레이아웃인 ConstraintLayout을

MotionLayout으로 변경해주세요. 


MotionLayout은 부모 Layout이어야하고 

자식 뷰까지만 모션을 적용할 수 있습니다. 

예를 들어 

<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:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:ignore="contentDescription">

<FrameLayout
android:id="@+id/page_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottom_nav"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

레이아웃이 이렇게 구성되어 있다고 하면, 

FragmentLayout에는 모션을 적용할 수 있지만, 

AppCompatTextView에는 적용할 수 없습니다.


#2. res/xml 디렉토리 생성하고, scene_main.xml 만들기

여기까지 따라오신 분들, 이 포스팅을 찾아오신 분들은

기본적으로 MotionLayout에 대해 알고 계시겠죠?

scene_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?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/expanded"
        motion:constraintSetStart="@id/baseState"
        motion:duration="250"
        motion:motionInterpolator="linear"/>
 
 
    <Transition
        motion:constraintSetEnd="@id/collapsed"
        motion:constraintSetStart="@id/expanded"
        motion:duration="250"
        motion:motionInterpolator="linear">
 
        <OnSwipe
            motion:dragDirection="dragDown"
            motion:onTouchUp="autoComplete"
            motion:touchAnchorId="@id/videoDetailLayout"
            motion:touchAnchorSide="bottom"
            motion:touchRegionId="@id/videoDetailLayout" />
 
        <KeyFrameSet>
 
            <KeyPosition
                motion:curveFit="linear"
                motion:framePosition="90"
                motion:motionTarget="@id/videoImage"
                motion:percentWidth="0"
                motion:percentX="0" />
 
            <KeyPosition
                motion:curveFit="linear"
                motion:framePosition="90"
                motion:motionTarget="@id/videoDetailLayout"
                motion:percentWidth="0" />
 
            <KeyPosition
                motion:curveFit="linear"
                motion:framePosition="90"
                motion:motionTarget="@id/recyclerview_container"
                motion:percentWidth="0" />
 
            <KeyAttribute
                android:alpha="0"
                motion:framePosition="75"
                motion:motionTarget="@id/recommendListView" />
 
        </KeyFrameSet>
 
    </Transition>
 
    <Transition
        motion:constraintSetEnd="@id/baseState"
        motion:constraintSetStart="@id/collapsed"
        motion:duration="300"
        motion:motionInterpolator="easeInOut">
 
        <OnSwipe
            motion:dragDirection="dragDown"
            motion:onTouchUp="autoComplete"
            motion:touchAnchorId="@id/videoDetailLayout"
            motion:touchAnchorSide="bottom"
            motion:touchRegionId="@id/videoDetailLayout" />
 
    </Transition>
 
    <ConstraintSet android:id="@+id/baseState">
 
        <Constraint
            android:id="@id/detailVideoContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0"
            android:background="#000000" />
 
        <Constraint
            android:id="@id/page_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toTopOf="@id/bottom_ynav"
            motion:layout_constraintTop_toTopOf="parent" />
 
        <Constraint
            android:id="@id/videoDetailLayout"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:background="@android:color/white"
            motion:layout_constraintTop_toBottomOf="parent" />
 
        <Constraint
            android:id="@id/videoImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            motion:layout_constraintBottom_toBottomOf="@id/videoDetailLayout"
            motion:layout_constraintDimensionRatio="W,2.5:1"
            motion:layout_constraintStart_toStartOf="@id/videoDetailLayout"
            motion:layout_constraintTop_toTopOf="@id/videoDetailLayout" />
 
 
        <Constraint
            android:id="@id/recyclerview_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:alpha="0"
            motion:layout_constraintBottom_toBottomOf="@id/videoDetailLayout"
            motion:layout_constraintTop_toBottomOf="@id/videoDetailLayout" />
 
        <Constraint
            android:id="@id/recommendListView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:alpha="0"
            motion:layout_constraintBottom_toBottomOf="@id/videoDetailLayout"
            motion:layout_constraintTop_toBottomOf="@id/videoDetailLayout" />
 
 
        <Constraint
            android:id="@id/bottom_ynav"/>
 
    </ConstraintSet>
 
    <ConstraintSet android:id="@+id/expanded">
        <Constraint
            android:id="@id/detailVideoContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="1"
            android:background="#000000" />
 
        <Constraint
            android:id="@id/videoDetailLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintDimensionRatio="16:9"
            motion:layout_constraintTop_toTopOf="parent" />
 
        <Constraint
            android:id="@id/videoImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="@id/videoDetailLayout"
            motion:layout_constraintEnd_toEndOf="@id/videoDetailLayout"
            motion:layout_constraintStart_toStartOf="@id/videoDetailLayout"
            motion:layout_constraintTop_toTopOf="@id/videoDetailLayout" />
 
        <Constraint
            android:id="@id/bottom_ynav"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="?android:attr/windowBackground"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintTop_toBottomOf="parent" />
 
 
        <Constraint
            android:id="@id/recyclerview_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/videoDetailLayout" />
 
        <Constraint
            android:id="@id/recommendListView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/videoDetailLayout">
            <PropertySet
                android:visibility="visible"
                motion:alpha="1" />
            <CustomAttribute
                motion:attributeName="backgroundColor"
                motion:customColorValue="@android:color/white" />
        </Constraint>
 
    </ConstraintSet>
 
    <ConstraintSet android:id="@+id/collapsed">
        <Constraint
            android:id="@id/detailVideoContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0"
            android:background="#000000" />
 
        <Constraint
            android:id="@id/videoDetailLayout"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginBottom="4dp"
            android:background="@drawable/border_lgtgray"
            motion:layout_constraintBottom_toTopOf="@id/bottom_ynav">
            <PropertySet motion:alpha="1" />
        </Constraint>
 
        <Constraint
            android:id="@id/videoImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:scaleType="centerCrop"
            motion:layout_constraintBottom_toBottomOf="@id/videoDetailLayout"
            motion:layout_constraintDimensionRatio="H,1:2.5"
            motion:layout_constraintStart_toStartOf="@id/videoDetailLayout"
            motion:layout_constraintTop_toTopOf="@id/videoDetailLayout">
 
        </Constraint>
 
        <Constraint
            android:id="@id/bottom_nav"
            android:layout_width="0dp"
            android:layout_height="42dp"
            android:background="?android:attr/windowBackground"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintLeft_toLeftOf="parent"
            motion:layout_constraintRight_toRightOf="parent" />
 
 
        <Constraint
            android:id="@id/recyclerview_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginBottom="4dp"
            android:alpha="1"
            motion:layout_constraintBottom_toTopOf="@id/bottom_ynav"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/videoDetailLayout" />
 
        <Constraint
            android:id="@id/recommendListView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="0"
            motion:layout_constraintBottom_toBottomOf="@id/videoDetailLayout"
            motion:layout_constraintEnd_toEndOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/videoDetailLayout" />
 
    </ConstraintSet>
 
 
</MotionScene>
cs

baseState는  아래 상태이고      expanded는 아래 상태입니다.

   

위 xml을 작성했다면 

이제 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:showPaths="true"
app:motionDebug="SHOW_ALL"
app:layoutDescription="@xml/scene_main"
tools:ignore="contentDescription">

이제 activity_main.xml은 scene_xml 영향을 받습니다.

여기서 더 작성하면 포스팅이 너무 길어져서

깃허브 소스 남깁니다.

https://github.com/Parksunggyun/MirrorYoutube

추가된 파일들로는 

anim/rotation_arrow.xml, rotation_arrow_reverse.xml

RecommendListAdapter.kt

VideoInfo.kt

Videos.kt


변경된 파일

FragmentHome.kt

MainActivity.kt

가 있습니다.

감사합니다.

반응형