Skip to content

Instantly share code, notes, and snippets.

View AshuTyagi16's full-sized avatar

Ashu Tyagi AshuTyagi16

View GitHub Profile
var currentVelocity = 0f
/**
* A [SpringAnimation] for this RecyclerView item. This animation rotates the view with a bouncy
* spring configuration, resulting in the oscillation effect.
*
* The animation is started in [Recyclerview.onScrollListener].
*/
val rotation: SpringAnimation = SpringAnimation(itemView, SpringAnimation.ROTATION)
.setSpring(
companion object {
/** The magnitude of rotation while the list is scrolled. */
private const val SCROLL_ROTATION_MAGNITUDE = 0.25f
/** The magnitude of rotation while the list is over-scrolled. */
private const val OVERSCROLL_ROTATION_MAGNITUDE = -10
/** The magnitude of translation distance while the list is over-scrolled. */
private const val OVERSCROLL_TRANSLATION_MAGNITUDE = 0.2f
rvApps.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
recyclerView.forEachVisibleHolder { holder: AppViewHolder ->
holder.rotation
// Update the velocity.
// The velocity is calculated by the vertical scroll offset.
.setStartVelocity(holder.currentVelocity - dx * SCROLL_ROTATION_MAGNITUDE)
// Start the animation. This does nothing if the animation is already running.
.start()
}
inline fun <reified T : RecyclerView.ViewHolder> RecyclerView.forEachVisibleHolder(
action: (T) -> Unit
) {
for (i in 0 until childCount) {
action(getChildViewHolder(getChildAt(i)) as T)
}
}
import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import androidx.appcompat.app.AppCompatActivity
class ExampleActivity : AppCompatActivity() {
private lateinit var wakeLock: PowerManager.WakeLock
@SystemService(Context.POWER_SERVICE)
public final class PowerManager {
final IPowerManager mService;
public final class WakeLock {
@UnsupportedAppUsage
private int mFlags;
@UnsupportedAppUsage
private String mTag;
rvChat.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
recyclerView.forEachVisibleHolder { holder: RecyclerView.ViewHolder ->
if (holder is OutgoingChatViewHolder) {
changeDrawableColor(holder)
}
}
}
})
private fun getFloatRange(view: View): Float {
return 1f - (view.absY() / resources.displayMetrics.heightPixels.toFloat())
}
//This function returns the y-cord of the view..
fun View.absY(): Float {
val location = IntArray(2)
getLocationOnScreen(location)
return (location[1].toFloat() - height.toFloat())
}
@AshuTyagi16
AshuTyagi16 / AnimatedColor.kt
Created November 16, 2020 09:22
Class to animate between two colors based on progress
import android.graphics.Color
class AnimatedColor(private val startColor: Int, private val endColor: Int) {
private val startHSV: FloatArray
private val endHSV: FloatArray
private val move = FloatArray(3)
init {
private fun changeDrawableColor(holder: OutgoingChatViewHolder) {
holder.itemView.post {
ContextCompat.getDrawable(this, R.drawable.bg_round_corner_outgoing)
?.let { incomingBgDrawable ->
val color = animatedColor.with(getFloatRange(holder.itemView))
incomingBgDrawable.updateTint(color)
holder.itemView.tvOutgoingMessage.background = incomingBgDrawable
}
}
}