1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 自定义: class myView: View{ private var paint = Paint() private var H = 54F private var W = 54F constructor(context: Context):super(context) constructor(context:Context,set:AttributeSet):super(context,set) var context2 = context override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) paint.color = Color.RED canvas!!.drawCircle(W,H,15F,paint) }
override fun onTouchEvent(event: MotionEvent?): Boolean { H = event!!.y W = event.x Toast.makeText(context2,"X:${event.x},Y:${event.y}",Toast.LENGTH_SHORT).show() invalidate() return true } } 实例化: val myView = myView(this) layout.addView(myView)
|