How get Button coordinate?

泪湿孤枕 提交于 2019-12-25 07:17:11

问题


I have a layout activity_main.xml:

    <Button
        android:id="@+id/button"
        android:layout_marginTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:text="@string/hover"/>

</FrameLayout>

How can i get Button's coordinate (x, y) on screen. for now I use:

button.getX()
button.getY()

but it return 0.0, How can i achieve this.
Thanks


回答1:


This happens because you are getting the coordinates of your view before it's actually shown.

You can use the View.post() method, which executes some code after your View is properly initalized.

mButton.post(
    new Runnable(){
        @Override
        public void run(){
            x = mButton.getX();
            y = mButton.getY();
        }
    }
);



回答2:


Inside OnCreate() method define this

Button button = (Button) findViewById(R.id.button);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");

and create this to get location of button.

private Point getPointOfView(View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);
    return new Point(location[0], location[1]);
}



回答3:


A few things to consider when you deal with view coordinates:

  1. getLocationInWindow() returns the position within a window, which usually has a greater height than your root layout, because the former includes the notifications bar / appbar / tabs.

  2. getX(), getY(), getTop(), getLeft() all return the position within your view's parent, which may be irrelevant, if your view is not a direct child of the root layout.

  3. If your view's position depends on the height of another view (e.g. it is located below another view, who's height if set as wrap_content), then you will be able to know the final position only after the global layout is finished (use OnGlobalLayoutListener).




回答4:


public class MainActivity extends Activity {

    Button b1, b2, b3, b4;

    int b1x1, b1x2, b1y1, b1y2;

    private TextView xcordview;
    private TextView ycordview;
    private TextView buttonIndicator;
    private RelativeLayout touchview;
    private static int defaultStates[];
    private Button mLastButton;
    private final static int[] STATE_PRESSED = {
            android.R.attr.state_pressed,
            android.R.attr.state_focused  
                    | android.R.attr.state_enabled };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xcordview = (TextView) findViewById(R.id.textView4);
        ycordview = (TextView) findViewById(R.id.textView3);
        buttonIndicator = (TextView) findViewById(R.id.button_indicator);
        touchview = (RelativeLayout) findViewById(R.id.relativelayout);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        defaultStates = b1.getBackground().getState();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        touchview.setOnTouchListener(new View.OnTouchListener() {

            private boolean isInside = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int x = (int) event.getX();
                int y = (int) event.getY();

                xcordview.setText(String.valueOf(x));
                ycordview.setText(String.valueOf(y));

                for (int i = 0; i < touchview.getChildCount(); i++) {
                    View current = touchview.getChildAt(i);
                    if (current instanceof Button) {
                        Button b = (Button) current;

                        if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(defaultStates);
                        }

                        if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(STATE_PRESSED);
                            if (b != mLastButton) {
                                mLastButton = b;
                                buttonIndicator.setText(mLastButton.getText());
                            }
                        }

                    }
                }
                return true;
            }

        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
    }

    static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
        return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
    }
}


来源:https://stackoverflow.com/questions/37906693/how-get-button-coordinate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!