This repository was archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathViewExtensions.kt
More file actions
274 lines (260 loc) · 9.27 KB
/
ViewExtensions.kt
File metadata and controls
274 lines (260 loc) · 9.27 KB
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
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.materialstudies.owl.util
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Bitmap.Config.ARGB_8888
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.view.View.GONE
import android.view.View.MeasureSpec
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.animation.AnimationUtils
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.IdRes
import androidx.annotation.Px
import androidx.core.animation.doOnEnd
import androidx.core.content.res.use
import androidx.core.graphics.applyCanvas
import androidx.core.view.ViewCompat
import androidx.core.view.drawToBitmap
import androidx.core.view.forEach
import androidx.dynamicanimation.animation.DynamicAnimation.ViewProperty
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.materialstudies.owl.R
/**
* An extension function which creates/retrieves a [SpringAnimation] and stores it in the [View]s
* tag.
*/
fun View.spring(
property: ViewProperty,
stiffness: Float = 200f,
damping: Float = 0.3f,
startVelocity: Float? = null
): SpringAnimation {
val key = getKey(property)
var springAnim = getTag(key) as? SpringAnimation?
if (springAnim == null) {
springAnim = SpringAnimation(this, property).apply {
spring = SpringForce().apply {
this.dampingRatio = damping
this.stiffness = stiffness
startVelocity?.let { setStartVelocity(it) }
}
}
setTag(key, springAnim)
}
return springAnim
}
/**
* Map from a [ViewProperty] to an `id` suitable to use as a [View] tag.
*/
@IdRes
private fun getKey(property: ViewProperty): Int {
return when (property) {
SpringAnimation.TRANSLATION_X -> R.id.translation_x
SpringAnimation.TRANSLATION_Y -> R.id.translation_y
SpringAnimation.TRANSLATION_Z -> R.id.translation_z
SpringAnimation.SCALE_X -> R.id.scale_x
SpringAnimation.SCALE_Y -> R.id.scale_y
SpringAnimation.ROTATION -> R.id.rotation
SpringAnimation.ROTATION_X -> R.id.rotation_x
SpringAnimation.ROTATION_Y -> R.id.rotation_y
SpringAnimation.X -> R.id.x
SpringAnimation.Y -> R.id.y
SpringAnimation.Z -> R.id.z
SpringAnimation.ALPHA -> R.id.alpha
SpringAnimation.SCROLL_X -> R.id.scroll_x
SpringAnimation.SCROLL_Y -> R.id.scroll_y
else -> throw IllegalAccessException("Unknown ViewProperty: $property")
}
}
/**
* Retrieve a color from the current [android.content.res.Resources.Theme].
*/
@ColorInt
fun Context.themeColor(
@AttrRes themeAttrId: Int
): Int {
return obtainStyledAttributes(
intArrayOf(themeAttrId)
).use {
it.getColor(0, Color.MAGENTA)
}
}
/**
* Search this view and any children for a [ColorDrawable] `background` and return it's `color`,
* else return `colorSurface`.
*/
@ColorInt
fun View.descendantBackgroundColor(): Int {
val bg = backgroundColor()
if (bg != null) {
return bg
} else if (this is ViewGroup) {
forEach {
val childBg = descendantBackgroundColorOrNull()
if (childBg != null) {
return childBg
}
}
}
return context.themeColor(android.R.attr.colorBackground)
}
@ColorInt
private fun View.descendantBackgroundColorOrNull(): Int? {
val bg = backgroundColor()
if (bg != null) {
return bg
} else if (this is ViewGroup) {
forEach {
val childBg = backgroundColor()
if (childBg != null) {
return childBg
}
}
}
return null
}
/**
* Check if this [View]'s `background` is a [ColorDrawable] and if so, return it's `color`,
* otherwise `null`.
*/
@ColorInt
fun View.backgroundColor(): Int? {
val bg = background
if (bg is ColorDrawable) {
return bg.color
}
return null
}
/**
* Walk up from a [View] looking for an ancestor with a given `id`.
*/
fun View.findAncestorById(@IdRes ancestorId: Int): View {
return when {
id == ancestorId -> this
parent is View -> (parent as View).findAncestorById(ancestorId)
else -> throw IllegalArgumentException("$ancestorId not a valid ancestor")
}
}
/**
* Potentially animate showing a [BottomNavigationView].
*
* Abruptly changing the visibility leads to a re-layout of main content, animating
* `translationY` leaves a gap where the view was that content does not fill.
*
* Instead, take a snapshot of the view, and animate this in, only changing the visibility (and
* thus layout) when the animation completes.
*/
fun BottomNavigationView.show() {
if (visibility == VISIBLE) return
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
val parent = parent as ViewGroup
// View needs to be laid out to create a snapshot & know position to animate. If view isn't
// laid out yet, need to do this manually.
if (!isLaidOut) {
measure(
MeasureSpec.makeMeasureSpec(parent.width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(parent.height, MeasureSpec.AT_MOST)
)
layout(parent.left, parent.height - measuredHeight, parent.right, parent.height)
}
val drawable = BitmapDrawable(context.resources, drawToBitmap())
drawable.setBounds(left, parent.height, right, parent.height + height)
parent.overlay.add(drawable)
ValueAnimator.ofInt(parent.height, top).apply {
startDelay = 100L
duration = 300L
interpolator = AnimationUtils.loadInterpolator(
context,
android.R.interpolator.linear_out_slow_in
)
addUpdateListener {
val newTop = it.animatedValue as Int
drawable.setBounds(left, newTop, right, newTop + height)
}
doOnEnd {
parent.overlay.remove(drawable)
visibility = VISIBLE
}
start()
}
}
})
}
/**
* Potentially animate hiding a [BottomNavigationView].
*
* Abruptly changing the visibility leads to a re-layout of main content, animating
* `translationY` leaves a gap where the view was that content does not fill.
*
* Instead, take a snapshot, instantly hide the view (so content lays out to fill), then animate
* out the snapshot.
*/
fun BottomNavigationView.hide() {
if (visibility == GONE) return
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
val drawable = BitmapDrawable(context.resources, drawToBitmap())
val parent = parent as ViewGroup
drawable.setBounds(left, top, right, bottom)
parent.overlay.add(drawable)
visibility = GONE
ValueAnimator.ofInt(top, parent.height).apply {
startDelay = 100L
duration = 200L
interpolator = AnimationUtils.loadInterpolator(
context,
android.R.interpolator.fast_out_linear_in
)
addUpdateListener {
val newTop = it.animatedValue as Int
drawable.setBounds(left, newTop, right, newTop + height)
}
doOnEnd {
parent.overlay.remove(drawable)
}
start()
}
}
})
}
/**
* A copy of the KTX method, adding the ability to add extra padding the bottom of the [Bitmap];
* useful when it will be used in a [android.graphics.BitmapShader][BitmapShader] with
* a [android.graphics.Shader.TileMode.CLAMP][CLAMP tile mode].
*/
fun View.drawToBitmap(@Px extraPaddingBottom: Int = 0): Bitmap {
if (!ViewCompat.isLaidOut(this)) {
throw IllegalStateException("View needs to be laid out before calling drawToBitmap()")
}
return Bitmap.createBitmap(width, height + extraPaddingBottom, ARGB_8888).applyCanvas {
translate(-scrollX.toFloat(), -scrollY.toFloat())
draw(this)
}
}