lebonprix.apk/java/io/xdrm/lebonprix/anim/UnderlineAnimation.kt

81 lines
2.2 KiB
Kotlin

package io.xdrm.lebonprix.anim
import android.content.res.Resources
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RectShape
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.view.animation.Transformation
import android.widget.ImageView
class UnderlineAnimation(val target: ImageView?) : Animation() {
private val width : Int = 1000
private var cursor : Float = 0F
private var start : Float = 0F
private var end : Float = 0F
private var bitmap : Bitmap = Bitmap.createBitmap(width, 5, Bitmap.Config.ARGB_8888)
private var canvas : Canvas = Canvas(this.bitmap)
init {
draw()
}
fun during(durationMillis: Long) : UnderlineAnimation{
super.setDuration(durationMillis)
return this
}
// sets the animation range
fun animate(_start: Float, _end : Float) : UnderlineAnimation{
// ignore animation if already at (or going for) the end state
if( _end == cursor || !hasEnded() && _end == end )
return UnderlineAnimation(null)
start = _start
end = _end
return this
}
override fun start() {
if( target == null )
return
target.startAnimation(this)
}
// sets the animation range
fun animateContinue(_end : Float) : UnderlineAnimation{
start = cursor
end = _end
return this
}
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
super.applyTransformation(interpolatedTime, t)
cursor = start + (end - start) * interpolatedTime
draw()
}
private fun draw(){
// clear canvas
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
// set up taint (WHITE)
val taint = Paint()
taint.color = Color.WHITE
// draw rect
val size = cursor*width
val offset = (width-size)/2
canvas.drawRect(0F, 0F, size, 5F, taint)
// display on target
if( target != null )
target.setImageBitmap(bitmap)
}
}