2

我有这个 surfview

<SurfaceViewandroid:id="@+id/videoview"android:layout_width="720px"android:layout_height="480px"/>

我如何管理自动设置大小以显示大小?因为每部安卓手机都有不同的显示尺寸?

谢谢!

4

2 回答 2

1

你必须在 onSurfaceCreated() 中捕捉它并尝试在那里调整它的大小......

  private class surfaceCallback implements SurfaceHolder.Callback
{
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {
        if (holder == videoHolder)
        {
            Log.e(TAG, "video surfaceChanged: f: " + format + ", w: " + width + ", h: " + height);

            //resize it here maybe?
        } else
        {
            Log.e(TAG, "Unknown surface: f: " + format + ", w: " + width + ", h: " + height);
            assert (false);
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceCreated");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceDestroyed");
    }
}

获得显示尺寸的一种可能方法...

    //measured android resolution
    WindowManager win = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
    Display d = win.getDefaultDisplay();
    Point measuredResolution = new Point();
    d.getSize(measuredResolution); //measuredResolution now holds actual size.
于 2012-07-30T09:47:15.353 回答
1

你在 xml 中定义这个?android:layout_width="fill_parent"然后在父容器中使用和相同:)

否则,如果以编程方式执行此操作,请参阅@Shark 的答案

于 2012-07-30T09:49:58.797 回答