Skip to content

Instantly share code, notes, and snippets.

override fun onClick(view: View) {
val fragmentTransaction = initFragmentTransaction(view)
val copy = createCopyView(view)
root.addView(copy)
view.visibility = View.INVISIBLE
startAnimation(copy, fragmentTransaction)
}
open class BaseViewHolder<T>(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
open fun bind(item: T) {
binding.setVariable(BR.obj, item)
}
}
private fun createCopyView(view: View): View {
val copy = copyViewImage(view)
// On preLollipop when we create a copy of card view's content its shadow is copied too
// and we do not need additional card view.
return (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CardView(view.context).apply {
cardElevation = resources.getDimension(R.dimen.card_elevation)
radius = resources.getDimension(R.dimen.card_corner_radius)
abstract class BaseAdapter<T, VH : BaseViewHolder<T>> : RecyclerView.Adapter<VH>() {
abstract var dataSet: ArrayList<T>
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val view = parent.inflate(viewType)
return createViewHolder(view)
}
override fun onBindViewHolder(viewHolder: VH, position: Int) {
abstract class BaseRecyclerAdapter<T, VH : BaseViewHolder<T>> : RecyclerView.Adapter<VH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val inflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding = DataBindingUtil.inflate(inflater, viewType, parent, false)
return createViewHolder(binding)
}
override fun onBindViewHolder(holder: VH, position: Int) {
val obj = getObjectForPosition(position)
@bitvale
bitvale / SwitcherAnimator.kt
Created November 22, 2018 20:13
For Medium article "Android Dynamic Custom View is Easy"
// ...
var amplitude = BOUNCE_ANIM_AMPLITUDE_IN
var frequency = BOUNCE_ANIM_FREQUENCY_IN
var newProgress = 1f
if (!checked) {
amplitude = BOUNCE_ANIM_AMPLITUDE_OUT
frequency = BOUNCE_ANIM_FREQUENCY_OUT
newProgress = 0f
}
@bitvale
bitvale / IconProgress.kt
Created November 22, 2018 20:15
For Medium article "Android Dynamic Custom View is Easy"
private var iconProgress = 0f
set(value) {
if (field != value) {
field = value
val iconOffset = lerp(0f, iconRadius - iconCollapsedWidth / 2, value)
iconRect.left = width - switcherCornerRadius - iconCollapsedWidth / 2 - iconOffset
iconRect.right = width - switcherCornerRadius + iconCollapsedWidth / 2 + iconOffset
postInvalidateOnAnimation()
@bitvale
bitvale / onDraw.kt
Created November 22, 2018 20:17
For Medium article "Android Dynamic Custom View is Easy"
override fun onDraw(canvas: Canvas ?) {
// draw switcher (green rect)
canvas?.drawRoundRect(switcherRect, switcherCornerRadius, switcherCornerRadius, switcherPaint)
// draw icon (white rect)
canvas?.withTranslation(x = iconTranslateX) {
drawRoundRect(iconRect, switcherCornerRadius, switcherCornerRadius, iconPaint)
}
}
@bitvale
bitvale / onDraw.kt
Created January 7, 2019 07:54
For Medium article "The power of Android Porter/Duff Mode"
override fun onDraw(canvas: Canvas ?) {
canvas?.withRotation(angle, lightPivotX, lightPivotY) {
drawPath(lightPath, lightPaint)
}
canvas?.drawBitmap(textBitmap, 0f, 0f, textPaint)
}
@bitvale
bitvale / LightAnimator.kt
Created January 7, 2019 08:27
For Medium article "The power of Android Porter/Duff Mode"
private var animator = ValueAnimator.ofFloat(0f, 1f).apply {
addUpdateListener {
val value = it.animatedValue as Float
angle = lerp(0f, FULL_CIRCLE, value)
}
interpolator = CustomSpringInterpolator(INTERPOLATOR_FACTOR)
repeatMode = ValueAnimator.RESTART
repeatCount = ValueAnimator.INFINITE
duration = ANIMATION_DURATION
}