The draggablepointviews in pathview arent showing up. I did addView in PathView but the pointviews are still not being rendered. Am I missing something? I thought addView was enough and would make the childs render.
PathView.kt
class PathView(context: Context) : ViewGroup(context)
{
private val pointA = DraggablePointView(context)
private val pointB = DraggablePointView(context)
private val paint = Paint().apply {
strokeWidth = 15f
color = Color.WHITE
}
init
{
pointA.x = 50f
pointA.y = 50f
pointB.x = 330f
pointB.y = 330f
addView(pointA, 100, 100)
addView(pointB, 100, 100)
setWillNotDraw(false)
}
override fun onDraw(canvas: Canvas?)
{
if (canvas == null)
return
// Draw a line between pointA and pointB
canvas.drawLine(pointA.x, pointA.y, pointB.x, pointB.y, paint)
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)
{
}
}
DraggablePointView.kt
class DraggablePointView(context: Context) : ImageView(context)
{
init
{
setImageResource(R.drawable.point)
setWillNotDraw(false)
}
override fun onDragEvent(event: DragEvent?): Boolean
{
println("DRAG EVENT")
if (event == null)
return false
x = event.x
y = event.y
return true
}
}
Then:
val pathView = PathView(context)
frameLayout.addView(pathView)
To force a view to draw, call invalidate()
.
Invalidate the whole view. If the view is visible,
onDraw(android.graphics.Canvas)
will be called at some point in the future.
Mark the area defined by the rect (l, t, r, b) as needing to be drawn. The coordinates of the dirty rect are relative to the view. If the view is visible,
onDraw(android.graphics.Canvas)
will be called at some point in the future.
Since you extend ViewGroup
you're responsible for measuring and laying out the child views.
Perhaps you should extend AbsoluteLayout
instead, which already has the measurement and layout logic programmed. Then you can set the coordinates like so:
val lp = layoutParams as AbsoluteLayout.LayoutParams
lp.x = event.x
lp.y = event.y
requestLayout()
Or perhaps you should extend View
and draw the points in the same fashion as the line. That should be most efficient. You don't need child views to draw, you need just canvas.
onTouchEvent
to listen for and react to touch. setBounds
(tell it where to draw) and draw
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.