Kotlin 自定义View

class PointView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : View(context, attrs, defStyleAttr) 
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.support.v4.app.ActivityCompat
import android.util.AttributeSet
import android.util.Log
import android.view.View

class PointView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : View(context, attrs, defStyleAttr) {

    private val mContext: Context = context

    var radius : Float = 70f

    var currPoint : Point ? = null

    var paint:Paint ? = null

    init {

        paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint?.color = ActivityCompat.getColor(context,android.R.color.holo_blue_bright)

    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        if (currPoint == null){
            currPoint = Point(radius,radius)
            canvas?.drawCircle(currPoint!!.x,currPoint!!.y,radius,paint)

            var startPoint =  Point(radius,radius)
            var endPoint = Point(700f,700f)


            var anim = ValueAnimator.ofObject(PointEvaluator(),startPoint,endPoint)

            anim.duration = 3000

            anim.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
                override fun onAnimationUpdate(animation: ValueAnimator?) {
                    currPoint = animation?.getAnimatedValue() as Point
                    Log.i("info","currPoint!!.x  = "+currPoint!!.x)
                    invalidate()
                }
            })

            anim.start()


        }else{
            canvas?.drawCircle(currPoint!!.x,currPoint!!.y,radius,paint)
        }


    }

}

你可能感兴趣的:(andriod)