[英]Android: Draw a view on canvas
我有一個充氣的視圖,我可以在畫布上繪制它,但似乎無法正確定位它。
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.fix_this_recommendation, null);
v.measure(MeasureSpec.getSize(v.getMeasuredWidth()), MeasureSpec.getSize(v.getMeasuredHeight()));
v.layout(400, 400, 400, 400);
v.draw(canvas);
但視圖始終位於左上角。 誰知道為什么?
解決方法是翻譯畫布:
canvas.save();
canvas.translate(left, top);
view.draw(canvas);
canvas.restore();
每個View在其自己的坐標系中繪制其內容,左上角為(0,0)。 如果您希望它出現在其他位置,您可以在Canvas上設置新的變換矩陣 。
因為Canvas
使用的坐標系從左上角開始而不是定義您的自定義視圖並覆蓋onDraw()
方法,並在其中定位視圖,如您所願
使用drawChild(canvas,child,getDrawingTime()); 而不是child.draw(畫布)
drawChild將計算孩子的左上邊距和左邊距
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.