What I want to do is, to draw a line from the edge of a button to a point on the screen...
I'm using a dialog fragment for this... And all functions I tried always return the 0 point...
I tried following:
@Override
protected Dialog createDialog(Bundle savedInstanceState, FragmentActivity activity)
{
Dialog d = builder.create();
View v = LayoutInflater.from(activity).inflate(R.layout.dialog, null);
builder.setView(v);
// custom view by my which draws simple lines between points...
LineDrawView ldv = new LineDrawView(getActivity());
ldv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
((RelativeLayout)v).addView(ldv);
Dialog d = builder.create();
button = ((Button) v.findViewById(R.id.button));
int[] pos = new int[2];
button.getLocationInWindow(pos);
// tried following ways, all always return p(0, 0)
// button.getLocationInWindow(pos);
// Debugger.d("x: " + pos[0] + ", y: " + pos[1], "POS");
// button.getLocationOnScreen(pos);
// Debugger.d("x: " + pos[0] + ", y: " + pos[1], "POS");
// float x = button.getLeft();
// float y = button.getTop();
// Debugger.d("x: " + x + ", y: " + y, "POS");
ldv.addLine(new Point(pos[0], pos[1]), new Point(pos[0] + 30, pos[1]), Color.RED);
ldv.invalidate();
return d;
}
and it ALWAYS draws a line from the p(0, 0) to p(0, 30)...
How can I get the position of a button on the screen or preferable relative to it's parent view? I think, they layout is not finished yet, so I have to draw my lines somewhere later, but when and where?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…