2014-08-28 30 views
0

我有一些代碼,運行時orientation changesAndroid上運行的方法是完全

@Override 
public void onConfigurationChanged(Configuration newConfig){ 
    super.onConfigurationChanged(newConfig); 
    setVideoSize(); 
    _videoSurfaceView.changeCameraDisplaySize(); 
} 

private void setVideoSize() { 
    //Get the dimensions of the video 
    int videoWidth = _videoSurfaceView.getCamera().getParameters().getPreferredPreviewSizeForVideo().width; 
    int videoHeight = _videoSurfaceView.getCamera().getParameters().getPreferredPreviewSizeForVideo().height; 
    float videoProportion = (float) videoWidth/(float) videoHeight; 

    // Get the width of the screen 
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); 
    int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); 
    float screenProportion = (float) screenWidth/(float) screenHeight; 

    // Get the SurfaceView layout parameters 
    android.view.ViewGroup.LayoutParams lp = _videoSurfaceView.getLayoutParams(); 
    if (videoProportion > screenProportion) { 
     lp.width = screenWidth; 
     lp.height = (int) ((float) screenWidth/videoProportion); 
    } else { 
     lp.width = (int) (videoProportion * (float) screenHeight); 
     lp.height = screenHeight; 
    } 
    // Commit the layout parameters 
    _videoSurfaceView.setLayoutParams(lp); 
} 

當發生旋轉時這就是所謂的,但我需要運行setVideoSize()轉動完成後,。有沒有辦法做到這一點?

+0

看看[這個](http://developer.android.com/reference/android/view/OrientationEventListener.html) – aProperFox 2014-08-28 16:48:02

+0

這有效。謝謝! – 2014-08-28 18:17:16

+0

很高興聽到它! – aProperFox 2014-08-28 19:13:39

回答

0

請嘗試在onResume()中運行您的方法。從暫停狀態恢復活動時調用。

請注意,當你改變你的方向活動重新創建因此onResume()也將被調用。

相關問題