android 获取坐标[整理]



int[] location = new  int[2] ;
view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
location [0]--->x坐标,location [1]--->y坐标

如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。

    private ViewFlipper flipper;
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
            Log.i("demo", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        flipper = (ViewFlipper)findViewById(R.id.flipper);
        Log.d("demo", "left:" + flipper.getLeft());
        Log.d("demo", "right:" + flipper.getRight());
        Log.d("demo", "Top:" + flipper.getTop());
        Log.d("demo", "Bottom:" + flipper.getBottom());
       
        Button btn = (Button)findViewById(R.id.button2);
        btn.setOnClickListener(new OnClickListener(){
                        public void onClick(View v) {
                                Log.i("demo", "onClick");
                                Log.d("demo", "left:" + flipper.getLeft());
                                Log.d("demo", "right:" + flipper.getRight());
                                Log.d("demo", "Top:" + flipper.getTop());
                                Log.d("demo", "Bottom:" + flipper.getBottom());
                        }
        });
    }

你可能感兴趣的:(android)