Android和SurfaceView。错误的大小

时间:2014-02-25 18:06:29

标签: android resize surfaceview android-2.3-gingerbread

我在Android 2.3中使用SurfaceView调整大小时遇到​​了一些问题。版本。我正在使用视频播放器,当视频帧尺寸与表面尺寸不匹配时,我需要调整SurfaceView的大小。我在里面有SurfaceView Fragment。片段不匹配全屏,只是它的一部分。对于Android 4版本,一切正常,但在Android 2.3中。调整大小后,我的SurfaceView大于其FrameLayout父级。我不知道为什么会这样。

这是允许视频大小为init的界面

/**
 * This method is called by native vout to request a surface resize when frame size doesn't match surface size.
 * @param width Frame width
 * @param height Frame height
 * @param visible_width Visible frame width
 * @param visible_height Visible frame height
 * @param sar_num Surface aspect ratio numerator
 * @param sar_den Surface aspect ratio denominator
 */
void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);

//This method is an implementation of Interface
@Override
public void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
    if (width * height == 0)
        return;

    // store video size
    mVideoHeight = height;
    mVideoWidth = width;
    mVideoVisibleHeight = visible_height;
    mVideoVisibleWidth  = visible_width;
    mSarNum = sar_num;
    mSarDen = sar_den;
    Message msg = mHandler.obtainMessage(SURFACE_SIZE);
    mHandler.sendMessage(msg);
}

第二个 - 改变我的SurfaceView大小。

 private void changeSurfaceSize() {
    int sw;
    int sh;

    // get screen size
    if (mPresentation == null) {
        Log.d("MyLog", "Presentation is null");
        sw = ((Activity)mainActivity).getWindow().getDecorView().getWidth();
        sh = ((Activity)mainActivity).getWindow().getDecorView().getHeight();
    } else {
        Log.d("MyLog", "Presentation is not null");
        sw = mPresentation.getWindow().getDecorView().getWidth();
        sh = mPresentation.getWindow().getDecorView().getHeight();
    }

    double dw = sw, dh = sh;
    boolean isPortrait;

    if (mPresentation == null) {
        // getWindow().getDecorView() doesn't always take orientation into account, we have to correct the values
        isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    } else {
        isPortrait = false;
    }

    if (sw > sh && isPortrait || sw < sh && !isPortrait) {
        dw = sh;
        dh = sw;
    }

    // sanity check
    if (dw * dh == 0 || mVideoWidth * mVideoHeight == 0) {
        Log.e(TAG, "Invalid surface size");
        return;
    }

    // compute the aspect ratio
    double ar, vw;
    double density = (double)mSarNum / (double)mSarDen;
    if (density == 1.0) {
        /* No indication about the density, assuming 1:1 */
        vw = mVideoVisibleWidth;
        ar = (double)mVideoVisibleWidth / (double)mVideoVisibleHeight;
    } else {
        /* Use the specified aspect ratio */
        vw = mVideoVisibleWidth * density;
        ar = vw / mVideoVisibleHeight;
    }

    // compute the display aspect ratio
    double dar = dw / dh;

    switch (mCurrentSize) {
        case SURFACE_BEST_FIT:
            if (dar < ar)
                dh = dw / ar;
            else
                dw = dh * ar;
            break;
        case SURFACE_FIT_HORIZONTAL:
            dh = dw / ar;
            break;
        case SURFACE_FIT_VERTICAL:
            dw = dh * ar;
            break;
        case SURFACE_FILL:
            break;
        case SURFACE_16_9:
            ar = 16.0 / 9.0;
            if (dar < ar)
                dh = dw / ar;
            else
                dw = dh * ar;
            break;
        case SURFACE_4_3:
            ar = 4.0 / 3.0;
            if (dar < ar)
                dh = dw / ar;
            else
                dw = dh * ar;
            break;
        case SURFACE_ORIGINAL:
            dh = mVideoVisibleHeight;
            dw = vw;
            break;
    }

    SurfaceView surface;
    SurfaceView subtitlesSurface;
    SurfaceHolder surfaceHolder;
    SurfaceHolder subtitlesSurfaceHolder;
    FrameLayout surfaceFrame;

    if (mPresentation == null) {
        surface = mSurface;
        subtitlesSurface = mSubtitlesSurface;
        surfaceHolder = mSurfaceHolder;
        subtitlesSurfaceHolder = mSubtitlesSurfaceHolder;
        surfaceFrame = mSurfaceFrame;
    } else {
        surface = mPresentation.mSurface;
        subtitlesSurface = mPresentation.mSubtitlesSurface;
        surfaceHolder = mPresentation.mSurfaceHolder;
        subtitlesSurfaceHolder = mPresentation.mSubtitlesSurfaceHolder;
        surfaceFrame = mPresentation.mSurfaceFrame;
    }

    // force surface buffer size
    surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
    subtitlesSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);

    // set display size
    LayoutParams lp = surface.getLayoutParams();
    lp.width  = (int) Math.ceil(dw * mVideoWidth / mVideoVisibleWidth);
    lp.height = (int) Math.ceil(dh * mVideoHeight / mVideoVisibleHeight);
    surface.setLayoutParams(lp);
    subtitlesSurface.setLayoutParams(lp);

    // set frame size (crop if necessary)
    lp = surfaceFrame.getLayoutParams();
    lp.width = (int) Math.floor(dw);
    lp.height = (int) Math.floor(dh);
    surfaceFrame.setLayoutParams(lp);

    surface.invalidate();
    subtitlesSurface.invalidate();
}

有没有人有任何想法,为什么会这样呢?我会很高兴任何帮助!谢谢!

0 个答案:

没有答案