onResume()后宽高比错误

时间:2012-05-01 19:21:04

标签: android surfaceview aspect-ratio

我遇到了问题。在调用onResume后,我的媒体播放器的宽高比变得混乱

private void aspectRatio(MediaPlayer vp)
{

    Integer videoHeight;
    Integer videoWidth;
    //Obtain current video's dimensions for keeping aspect Ration the same on all devices
    videoHeight = vp.getVideoHeight();
    videoWidth = vp.getVideoWidth();

    //Get width of screen
    Integer screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    Integer screenHeight = getWindowManager().getDefaultDisplay().getHeight();

    Log.i("AspectRatio VP WxH", videoHeight.toString() +" x " + videoWidth.toString());
    Log.i("AspectRatio Screen WxH", screenHeight.toString() + " x " + screenWidth.toString());
    ViewGroup.LayoutParams viewParameters = view.getLayoutParams();

    float ratioWidth = (float)screenWidth/(float)videoWidth;
    float ratioHeight = (float)screenHeight/(float)videoHeight;
    float aspectRatio = (float)videoWidth/(float)videoHeight;

    if(ratioWidth>ratioHeight)
    {
        viewParameters.width = (int)(screenHeight * aspectRatio);
        viewParameters.height= screenHeight;
    }
    else if(ratioWidth < ratioHeight)
    {
        viewParameters.width = screenWidth;
        viewParameters.height = (int) (screenHeight / aspectRatio);
    }

    Integer x = viewParameters.width;
    Integer y = viewParameters.height;
    Log.i("Screen", x.toString() + " " + y.toString());
    view.setLayoutParams(viewParameters);
}

这是获取宽高比并将其放到我的surfaceView的函数。问题是在onResume()之后它的宽度远小于它应该的宽度。你能看到什么问题吗?

1 个答案:

答案 0 :(得分:0)

纵横比变得混乱,因为当表面改变时,它再次被创建。现在我改变它,以便在onSurfaceChanged中设置宽高比。它目前没有问题。