Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / Switcher.kt
Created November 22, 2018 20:12
For Medium article "Android Dynamic Custom View is Easy"
class Switcher @JvmOverloads constructor(
context: Context,
attrs: AttributeSet ? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
init {
attrs?.let { retrieveAttributes(attrs, defStyleAttr) }
}
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) {
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 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)
open class BaseViewHolder<T>(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
open fun bind(item: T) {
binding.setVariable(BR.obj, item)
}
}
override fun onClick(view: View) {
val fragmentTransaction = initFragmentTransaction(view)
val copy = createCopyView(view)
root.addView(copy)
view.visibility = View.INVISIBLE
startAnimation(copy, fragmentTransaction)
}