This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : RecyclerView.ViewHolder> RecyclerView.forEachVisibleHolder( | |
action: (T) -> Unit | |
) { | |
for (i in 0 until childCount) { | |
action(getChildViewHolder(getChildAt(i)) as T) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SystemService(Context.POWER_SERVICE) | |
public final class PowerManager { | |
final IPowerManager mService; | |
public final class WakeLock { | |
@UnsupportedAppUsage | |
private int mFlags; | |
@UnsupportedAppUsage | |
private String mTag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
OlderNewer