onWindowFocusChanged-屏幕焦点函数回调情况

1.这个函数的具体作用不太清楚,但网上有人说是 ,当activity得到或者失去焦点的时候,就会调用这个方法

先看如下代码

 

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        android.util.Log.e("zhangshuli", "win");
        mHistoryDisplayButton.getLocationOnScreen(mHistoryDisplayButtonLocation);
        mHistoryDisplayButtonLocationX = mHistoryDisplayButtonLocation[0];
        mHistoryDisplayButtonLocationY = mHistoryDisplayButtonLocation[1];
        mHistoryHideButton.getLocationOnScreen(mHistoryHideButtonLocation);
        mHistoryHideButtonLocationX = mHistoryHideButtonLocation[0];
        mHistoryHideButtonLocationY = mHistoryHideButtonLocation[1];
        mHistoryDisplayListWidth = mHistoryDisplayList.getWidth();
        mHistoryHideLastLocationX =  mHistoryHideButtonLocationX - mHistoryDisplayListWidth ;
        mRotationAngle = (135f)/((float)mHistoryDisplayButtonLocationX -(float)mHistoryHideLastLocationX);
        android.util.Log.e("zhangshuli", "angle"+mRotationAngle);
        android.util.Log.e("zhangshuli", "mHistoryDisplayListWidth"+mHistoryDisplayListWidth);
        android.util.Log.e("zhangshuli", "mHistoryHideLastLocationX"+mHistoryHideLastLocationX);
        android.util.Log.e("zhangshuli", "mHistoryHideButtonLocationX"+mHistoryHideButtonLocationX);
        android.util.Log.e("zhangshuli", "mHistoryDisplayButtonLocationX"+mHistoryDisplayButtonLocationX);

        super.onWindowFocusChanged(hasFocus);
}

当我进入和退出这个activity的时候,我们可以看到打印的log如下

 

1)进入

03-23 10:57:30.801 E/zhangshuli( 1762): nihao
03-23 10:57:31.241 E/zhangshuli( 1762): win
03-23 10:57:31.241 E/zhangshuli( 1762): angle0.5672269
03-23 10:57:31.241 E/zhangshuli( 1762): mHistoryDisplayListWidth362
03-23 10:57:31.241 E/zhangshuli( 1762): mHistoryHideLastLocationX378
03-23 10:57:31.241 E/zhangshuli( 1762): mHistoryHideButtonLocationX740
03-23 10:57:31.241 E/zhangshuli( 1762): mHistoryDisplayButtonLocationX616

2)退出

03-23 10:57:35.736 E/zhangshuli( 1762): win
03-23 10:57:35.736 E/zhangshuli( 1762): angle0.5672269
03-23 10:57:35.736 E/zhangshuli( 1762): mHistoryDisplayListWidth362
03-23 10:57:35.736 E/zhangshuli( 1762): mHistoryHideLastLocationX378
03-23 10:57:35.736 E/zhangshuli( 1762): mHistoryHideButtonLocationX740
03-23 10:57:35.736 E/zhangshuli( 1762): mHistoryDisplayButtonLocationX616

所以我们知道,这个方法是在activity加载或者退出的时候都会调用。对它唯一的认识就是,如果你想要得到一个控件的位置坐标,那么你必须在这个方法中执行相应的代码,在oncreate中是得不到的

 

我们现在再把它跟Oncreate做一下比较

log打印如下

 @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        android.util.Log.e("zhangshuli", "nihao");
}

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        android.util.Log.e("zhangshuli", "win");

        super.onWindowFocusChanged(hasFocus);
}

1)当我们启动应用的时候

03-23 12:56:37.577 E/zhangshuli( 6860): nihao
03-23 12:56:38.178 E/zhangshuli( 6860): win

2)按下HOME建

03-23 12:57:19.584 E/zhangshuli( 6860): win

3)按下HOME建再次进入

03-23 12:58:25.664 E/zhangshuli( 6860): win

4)直接退出

03-23 12:59:30.899 E/zhangshuli( 6860): win

可以得到一个结论,就是oncreate比focus先执行。但是,如论程序怎么走,都会执行focus

 

posted @ 2015-03-23 11:10  小白屋  阅读(1660)  评论(0编辑  收藏  举报