0

我有问题。调用 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() 之后它的宽度比它应该的要小得多。你能看出什么问题吗?

4

1 回答 1

0

纵横比搞砸了,因为当表面改变时,它又被创建了。现在我对其进行了更改,以便在 onSurfaceChanged 中也设置纵横比。它目前可以正常工作。

于 2012-06-15T16:37:05.703 回答