Android: Draw a view on canvas

前端 未结 4 800
栀梦
栀梦 2021-01-13 02:14

I have a view inflated, I can draw it on canvas, but can\'t seem to position it properly.

LayoutInflater li = (LayoutInflater)context.getSystemService(Contex         


        
相关标签:
4条回答
  • 2021-01-13 02:41

    use drawChild(canvas,child,getDrawingTime()); instead of child.draw(canvas)

    drawChild will calc child's top and left margin

    0 讨论(0)
  • 2021-01-13 02:52

    Each View draws its contents within its own coordinate system, with (0, 0) at the top-left. If you want it to appear elsewhere, you can set a new transformation matrix on your Canvas.

    0 讨论(0)
  • 2021-01-13 03:01

    because the coordinate system used in the Canvas starts from the top left corner instead of doing that define your Custom View and override the onDraw() method and inside it position your view as you like

    0 讨论(0)
  • 2021-01-13 03:06

    Solution is to translate canvas:

    canvas.save();
    canvas.translate(left, top);            
    view.draw(canvas);
    canvas.restore();
    
    0 讨论(0)
提交回复
热议问题