-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChoiceOfRectanglesView.kt
303 lines (269 loc) · 11.5 KB
/
ChoiceOfRectanglesView.kt
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.example
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.OvershootInterpolator
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
private const val ROTATION_RECT_SHUFFLE_INITIAL = 0f
private const val ROTATION_RECT_SHUFFLE_PEAK = 45f
private val TX_RECT_BEING_SHUFFLED = 45.dp.toFloat()
private val TY_RECT_BEING_SHUFFLED = -120.dp.toFloat()
private const val Z_BACKEST = -3f
private const val Z_BACK = -2f
private const val Z_MIDDLE = -1f
private const val Z_FRONT = 0f
private const val Z_FRONTEST = 1f
internal class ChoiceOfRectanglesView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {
private var isSpread = false
private var front = Rectangle.RED
private val blue: ColoredRectangleView
private val yellow: ColoredRectangleView
private val red: ColoredRectangleView
init {
clipChildren = false
val inflater = LayoutInflater.from(context)
blue = inflater.addColoredRectangleView(Color.parseColor("#005ab3"))
yellow = inflater.addColoredRectangleView(Color.parseColor("#ffd500"))
red = inflater.addColoredRectangleView(ContextCompat.getColor(inflater.context, android.R.color.holo_red_light))
spread()
}
private fun LayoutInflater.addColoredRectangleView(@ColorInt color: Int): ColoredRectangleView {
val view = inflate(R.layout.view_item_choice_of_rectangles, this@ChoiceOfRectanglesView, false) as ColoredRectangleView
view.setColor(color)
addView(view)
return view
}
private fun spread() {
isSpread = true
AnimatorSet().apply {
duration = 600
startDelay = 1000
interpolator = OvershootInterpolator(3f)
playTogether(
blue.rotateChildAnimator(-17f),
blue.translationAnimator(-12.dp.toFloat(), -60.dp.toFloat()),
yellow.rotateChildAnimator(0f),
yellow.translationAnimator(4.dp.toFloat(), 0f),
red.rotateChildAnimator(17f),
red.translationAnimator(0f, 60.dp.toFloat())
)
}.start()
}
fun show(rectangle: Rectangle) {
if (isSpread) {
val animatorSet = AnimatorSet()
animatorSet.playSequentially(
collateRectanglesAnimator().apply { duration = 200 },
rectangle.showAtFrontAnimator()
)
animatorSet.start()
isSpread = false
} else {
rectangle.showAtFrontAnimator().start()
}
}
private fun collateRectanglesAnimator(): AnimatorSet {
return AnimatorSet().apply {
playTogether(
blue.rotateChildAnimator(BACK_CHILD_ROTATION),
blue.resetTranslationAnimator(),
yellow.rotateChildAnimator(MIDDLE_CHILD_ROTATION),
yellow.resetTranslationAnimator(),
red.rotateChildAnimator(FRONT_CHILD_ROTATION),
red.resetTranslationAnimator()
)
}
}
private fun Rectangle.showAtFrontAnimator(): Animator {
val animatorSet = AnimatorSet().apply {
duration = 300
interpolator = FastOutSlowInInterpolator()
}
when (front) {
Rectangle.BLUE -> {
if (this == Rectangle.YELLOW) {
animatorSet.apply {
doOnEnd {
red.z = Z_BACK
blue.z = Z_MIDDLE
}
}.playTogether(
yellow.shuffleFromBackToFront(),
red.rotateChildAnimator(BACK_CHILD_ROTATION),
blue.rotateChildAnimator(MIDDLE_CHILD_ROTATION)
)
} else if (this == Rectangle.RED) {
animatorSet.apply {
doOnEnd {
yellow.z = Z_MIDDLE
red.z = Z_FRONT
}
}.playTogether(
yellow.rotateChildAnimator(MIDDLE_CHILD_ROTATION),
red.rotateChildAnimator(FRONT_CHILD_ROTATION),
blue.shuffleFromFrontToBack()
)
}
}
Rectangle.YELLOW -> {
if (this == Rectangle.RED) {
animatorSet.apply {
doOnEnd {
blue.z = Z_BACK
yellow.z = Z_MIDDLE
}
}.playTogether(
red.shuffleFromBackToFront(),
blue.rotateChildAnimator(BACK_CHILD_ROTATION),
yellow.rotateChildAnimator(MIDDLE_CHILD_ROTATION)
)
} else if (this == Rectangle.BLUE) {
animatorSet.apply {
doOnEnd {
red.z = Z_MIDDLE
blue.z = Z_FRONT
}
}.playTogether(
red.rotateChildAnimator(MIDDLE_CHILD_ROTATION),
blue.rotateChildAnimator(FRONT_CHILD_ROTATION),
yellow.shuffleFromFrontToBack()
)
}
}
Rectangle.RED -> {
if (this == Rectangle.BLUE) {
animatorSet.apply {
doOnEnd {
yellow.z = Z_BACK
red.z = Z_MIDDLE
}
}.playTogether(
blue.shuffleFromBackToFront(),
yellow.rotateChildAnimator(BACK_CHILD_ROTATION),
red.rotateChildAnimator(MIDDLE_CHILD_ROTATION)
)
} else if (this == Rectangle.YELLOW) {
animatorSet.apply {
doOnEnd {
blue.z = Z_MIDDLE
yellow.z = Z_FRONT
}
}.playTogether(
blue.rotateChildAnimator(MIDDLE_CHILD_ROTATION),
yellow.rotateChildAnimator(FRONT_CHILD_ROTATION),
red.shuffleFromFrontToBack()
)
}
}
}
front = this
return animatorSet
}
private fun View.resetTranslationAnimator(): ObjectAnimator = translationAnimator(0f, 0f)
private fun View.translationAnimator(x: Float, y: Float): ObjectAnimator {
return ObjectAnimator.ofPropertyValuesHolder(
this,
PropertyValuesHolder.ofFloat(View.TRANSLATION_X, x),
PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, y)
)
}
private fun ColoredRectangleView.setPivotAtBottomRight() {
pivotX = width.toFloat()
pivotY = height.toFloat()
}
private fun ColoredRectangleView.shuffleFromFrontToBack(): Animator {
setPivotAtBottomRight()
return AnimatorSet().apply {
val rectangleOut = ObjectAnimator.ofPropertyValuesHolder(
this@shuffleFromFrontToBack,
PropertyValuesHolder.ofFloat(View.ROTATION, ROTATION_RECT_SHUFFLE_PEAK),
PropertyValuesHolder.ofFloat(View.TRANSLATION_X, TX_RECT_BEING_SHUFFLED),
PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, TY_RECT_BEING_SHUFFLED)
).doOnEnd { z = Z_BACKEST }
val rectangleIn = AnimatorSet().apply {
val childRotate = rotateChildAnimator(BACK_CHILD_ROTATION)
val rectangleToBack = ObjectAnimator.ofPropertyValuesHolder(
this@shuffleFromFrontToBack,
PropertyValuesHolder.ofFloat(View.ROTATION, ROTATION_RECT_SHUFFLE_INITIAL),
PropertyValuesHolder.ofFloat(View.TRANSLATION_X, 0f),
PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 0f)
)
playTogether(rectangleToBack, childRotate)
}.doOnEnd { z = Z_BACK }
playSequentially(rectangleOut, rectangleIn)
}
}
private fun ColoredRectangleView.shuffleFromBackToFront(): Animator {
setPivotAtBottomRight()
return AnimatorSet().apply {
val rectangleOut = ObjectAnimator.ofPropertyValuesHolder(
this@shuffleFromBackToFront,
PropertyValuesHolder.ofFloat(View.ROTATION, ROTATION_RECT_SHUFFLE_PEAK),
PropertyValuesHolder.ofFloat(View.TRANSLATION_X, TX_RECT_BEING_SHUFFLED),
PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, TY_RECT_BEING_SHUFFLED)
).doOnEnd { z = Z_FRONTEST }
val rectangleIn = AnimatorSet().apply {
val childRotate = rotateChildAnimator(FRONT_CHILD_ROTATION)
val rectangleToFront = ObjectAnimator.ofPropertyValuesHolder(
this@shuffleFromBackToFront,
PropertyValuesHolder.ofFloat(View.ROTATION, ROTATION_RECT_SHUFFLE_INITIAL),
PropertyValuesHolder.ofFloat(View.TRANSLATION_X, 0f),
PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, 0f)
)
playTogether(rectangleToFront, childRotate)
}.doOnEnd { z = Z_FRONT }
playSequentially(rectangleOut, rectangleIn)
}
}
enum class Rectangle {
BLUE,
YELLOW,
RED
}
}
private fun Animator.doOnEnd(action: () -> Unit): Animator {
addListener(object : SimpleAnimationListener() {
override fun onAnimationEnd(animation: Animator?) {
action()
}
})
return this
}
val Int.dp: Int
get() {
val density = Resources.getSystem().displayMetrics.density
return Math.round(this * density)
}
private const val BACK_CHILD_ROTATION = 7f
private const val MIDDLE_CHILD_ROTATION = -7f
private const val FRONT_CHILD_ROTATION = 0f
internal class ColoredRectangleView(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {
private val coloredView: View = View(context).apply {
layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
addView(this)
}
fun setColor(@ColorInt color: Int) = coloredView.setBackgroundColor(color)
fun rotateChildAnimator(rotation: Float): Animator {
return ObjectAnimator.ofFloat(coloredView, View.ROTATION, rotation)
}
}
private abstract class SimpleAnimationListener : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator?) {}
override fun onAnimationEnd(animation: Animator?) {}
override fun onAnimationCancel(animation: Animator?) {}
override fun onAnimationStart(animation: Animator?) {}
}