|
| 1 | +/* |
| 2 | + * Copyright 2019 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.android.motion.demo.oscillation |
| 18 | + |
| 19 | +import android.view.LayoutInflater |
| 20 | +import android.view.ViewGroup |
| 21 | +import android.widget.EdgeEffect |
| 22 | +import android.widget.ImageView |
| 23 | +import android.widget.TextView |
| 24 | +import androidx.core.view.doOnLayout |
| 25 | +import androidx.core.view.doOnNextLayout |
| 26 | +import androidx.dynamicanimation.animation.SpringAnimation |
| 27 | +import androidx.dynamicanimation.animation.SpringForce |
| 28 | +import androidx.recyclerview.widget.ListAdapter |
| 29 | +import androidx.recyclerview.widget.RecyclerView |
| 30 | +import com.bumptech.glide.Glide |
| 31 | +import com.example.android.motion.R |
| 32 | +import com.example.android.motion.model.Cheese |
| 33 | + |
| 34 | +internal class CheeseAdapter : ListAdapter<Cheese, CheeseViewHolder>(Cheese.DIFF_CALLBACK) { |
| 35 | + |
| 36 | + /** |
| 37 | + * A [RecyclerView.OnScrollListener] to be set to the RecyclerView. This tilts the visible |
| 38 | + * items while the list is scrolled. |
| 39 | + * |
| 40 | + * @see RecyclerView.addOnScrollListener |
| 41 | + */ |
| 42 | + val onScrollListener = object : RecyclerView.OnScrollListener() { |
| 43 | + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { |
| 44 | + recyclerView.forEachVisibleHolder { holder: CheeseViewHolder -> |
| 45 | + holder.rotation |
| 46 | + // Update the velocity. |
| 47 | + // The velocity is calculated by the horizontal scroll offset. |
| 48 | + .setStartVelocity(holder.currentVelocity - dx * SCROLL_ROTATION_MAGNITUDE) |
| 49 | + // Start the animation. This does nothing if the animation is already running. |
| 50 | + .start() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * A [RecyclerView.EdgeEffectFactory] to be set to the RecyclerView. This adds bounce effect |
| 57 | + * when the list is over-scrolled. |
| 58 | + * |
| 59 | + * @see RecyclerView.setEdgeEffectFactory |
| 60 | + */ |
| 61 | + val edgeEffectFactory = object : RecyclerView.EdgeEffectFactory() { |
| 62 | + override fun createEdgeEffect(recyclerView: RecyclerView, direction: Int): EdgeEffect { |
| 63 | + return object : EdgeEffect(recyclerView.context) { |
| 64 | + |
| 65 | + override fun onPull(deltaDistance: Float) { |
| 66 | + super.onPull(deltaDistance) |
| 67 | + handlePull(deltaDistance) |
| 68 | + } |
| 69 | + |
| 70 | + override fun onPull(deltaDistance: Float, displacement: Float) { |
| 71 | + super.onPull(deltaDistance, displacement) |
| 72 | + handlePull(deltaDistance) |
| 73 | + } |
| 74 | + |
| 75 | + private fun handlePull(deltaDistance: Float) { |
| 76 | + // This is called on every touch event while the list is scrolled with a finger. |
| 77 | + // We simply update the view properties without animation. |
| 78 | + val sign = if (direction == DIRECTION_RIGHT) -1 else 1 |
| 79 | + val rotationDelta = sign * deltaDistance * OVERSCROLL_ROTATION_MAGNITUDE |
| 80 | + val translationXDelta = |
| 81 | + sign * recyclerView.width * deltaDistance * OVERSCROLL_TRANSLATION_MAGNITUDE |
| 82 | + recyclerView.forEachVisibleHolder { holder: CheeseViewHolder -> |
| 83 | + holder.rotation.cancel() |
| 84 | + holder.translationX.cancel() |
| 85 | + holder.itemView.rotation += rotationDelta |
| 86 | + holder.itemView.translationX += translationXDelta |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + override fun onRelease() { |
| 91 | + super.onRelease() |
| 92 | + // The finger is lifted. This is when we should start the animations to bring |
| 93 | + // the view property values back to their resting states. |
| 94 | + recyclerView.forEachVisibleHolder { holder: CheeseViewHolder -> |
| 95 | + holder.rotation.start() |
| 96 | + holder.translationX.start() |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + override fun onAbsorb(velocity: Int) { |
| 101 | + super.onAbsorb(velocity) |
| 102 | + val sign = if (direction == DIRECTION_RIGHT) -1 else 1 |
| 103 | + // The list has reached the edge on fling. |
| 104 | + val translationVelocity = sign * velocity * FLING_TRANSLATION_MAGNITUDE |
| 105 | + recyclerView.forEachVisibleHolder { holder: CheeseViewHolder -> |
| 106 | + holder.translationX |
| 107 | + .setStartVelocity(translationVelocity) |
| 108 | + .start() |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheeseViewHolder { |
| 116 | + return CheeseViewHolder(parent).apply { |
| 117 | + // The rotation pivot should be at the center of the top edge. |
| 118 | + itemView.doOnLayout { v -> v.pivotX = v.width / 2f } |
| 119 | + itemView.pivotY = 0f |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + override fun onBindViewHolder(holder: CheeseViewHolder, position: Int) { |
| 124 | + val cheese = getItem(position) |
| 125 | + Glide.with(holder.image).load(cheese.image).into(holder.image) |
| 126 | + holder.name.text = cheese.name |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +internal class CheeseViewHolder( |
| 132 | + val parent: ViewGroup |
| 133 | +) : RecyclerView.ViewHolder( |
| 134 | + LayoutInflater.from(parent.context) |
| 135 | + .inflate(R.layout.cheese_board_item, parent, false) |
| 136 | +) { |
| 137 | + val image: ImageView = itemView.findViewById(R.id.image) |
| 138 | + val name: TextView = itemView.findViewById(R.id.name) |
| 139 | + |
| 140 | + var currentVelocity = 0f |
| 141 | + |
| 142 | + /** |
| 143 | + * A [SpringAnimation] for this RecyclerView item. This animation rotates the view with a bouncy |
| 144 | + * spring configuration, resulting in the oscillation effect. |
| 145 | + * |
| 146 | + * The animation is started in [CheeseAdapter.onScrollListener]. |
| 147 | + */ |
| 148 | + val rotation: SpringAnimation = SpringAnimation(itemView, SpringAnimation.ROTATION) |
| 149 | + .setSpring( |
| 150 | + SpringForce() |
| 151 | + .setFinalPosition(0f) |
| 152 | + .setDampingRatio(SpringForce.DAMPING_RATIO_HIGH_BOUNCY) |
| 153 | + .setStiffness(SpringForce.STIFFNESS_LOW) |
| 154 | + ) |
| 155 | + .addUpdateListener { _, _, velocity -> |
| 156 | + currentVelocity = velocity |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * A [SpringAnimation] for this RecyclerView item. This animation is used to bring the item back |
| 161 | + * after the over-scroll effect. |
| 162 | + */ |
| 163 | + val translationX: SpringAnimation = SpringAnimation(itemView, SpringAnimation.TRANSLATION_X) |
| 164 | + .setSpring( |
| 165 | + SpringForce() |
| 166 | + .setFinalPosition(0f) |
| 167 | + .setDampingRatio(SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY) |
| 168 | + .setStiffness(SpringForce.STIFFNESS_LOW) |
| 169 | + ) |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Runs [action] on every visible [RecyclerView.ViewHolder] in this [RecyclerView]. |
| 174 | + */ |
| 175 | +private inline fun <reified T : RecyclerView.ViewHolder> RecyclerView.forEachVisibleHolder( |
| 176 | + action: (T) -> Unit |
| 177 | +) { |
| 178 | + for (i in 0 until childCount) { |
| 179 | + action(getChildViewHolder(getChildAt(i)) as T) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// The constants below are used to calculate the animation magnitude from values taken from UI |
| 184 | +// events. Their values are determined empirically and can be modified to change the animation |
| 185 | +// flavor. |
| 186 | + |
| 187 | +/** The magnitude of rotation while the list is scrolled. */ |
| 188 | +private const val SCROLL_ROTATION_MAGNITUDE = 0.25f |
| 189 | +/** The magnitude of rotation while the list is over-scrolled. */ |
| 190 | +private const val OVERSCROLL_ROTATION_MAGNITUDE = -10 |
| 191 | +/** The magnitude of translation distance while the list is over-scrolled. */ |
| 192 | +private const val OVERSCROLL_TRANSLATION_MAGNITUDE = 0.2f |
| 193 | +/** The magnitude of translation distance when the list reaches the edge on fling. */ |
| 194 | +private const val FLING_TRANSLATION_MAGNITUDE = 0.5f |
0 commit comments