思路:
自定义的view类继承于view类,在xml中赋予它布局属性,在MainActivity中调用xml定义的自己写的view;使用ValueAnimator改变onDraw中绘画的控件的属性,当属性的值改变时,会触发invalidate()函数,系统自己去调用onDraw方法,就形成了动画。
代码:
自定义view的类:
class animator_View : View {
constructor(context: Context,attributeSet: AttributeSet): super(context,attributeSet)
// 高阶函数
var callBack:((Int) -> Boolean)? = null
var pregress = 0
set(value){
field = value
Log.v("yk","我是set + $field")
}
private val mPaint1 by lazy {
Paint().also {
it.color = Color.GRAY
it.style = Paint.Style.FILL
}
}
private val mPaint2 by lazy {
Paint().also {
it.color = Color.BLUE
it.style = Paint.Style.FILL
}
}
private val mPaint3 by lazy {
Paint().also {
it.color = Color.BLUE
it.style = Paint.Style.FILL
}
}
// 画√
private val mPaint4 by lazy {
Paint().also {
it.color = Color.WHITE
it.style = Paint.Style.STROKE
it.strokeWidth = 5f
it.strokeCap = Paint.Cap.ROUND;
}
}
// 线
private var line1X:Float = 0f
private var line1Y:Float = 0f
private var line2X:Float = 0f
private var line2Y:Float = 0f
// 保存图片的宽高
private var ykWidth:Float = 0f
private var ykHeight:Float = 0f
// 第一次图层的y坐标
private var firstX:Float = 0f
// 第二次图片的x 坐标
private var seondX:Float = 0f
private var seconY:Float = 0f
// 第二个图层两端缩进