Android SurfaceView 到全屏

Android SurfaceView to full screen

我有一个带 SurfaceView 的媒体播放器。我试图找到一种在全屏和嵌入式模式之间切换的解决方案。自定义媒体控制器是使用这篇热门文章 http://www.brightec.co.uk/ideas/custom-android-media-controller 为 MediaPlayer 开发的。所以现在我在自定义媒体控制器上有按钮来切换模式。

XML 个 activity(未满)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
        <ListView
            android:id="@+id/lvMain"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:background="#20FFFFFF" >
        </ListView>
        <FrameLayout
            android:id="@+id/videoSurfaceContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <SurfaceView
                android:id="@+id/videoSurface"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>
    </LinearLayout>

我找到了一个更改 SurfaceView 大小的解决方案,大小已更改但没有进入全屏,图片在他们的框架中:

 @Override
    public void toggleFullScreen() {
        setFullScreen(isFullScreen());
    }

    @Override
    public boolean isFullScreen() {

        if(mFullScreen){
            return false;
        }else{
            return true;
        }
    }

    public void setFullScreen(boolean fullScreen){
        fullScreen = false;

        if (mFullScreen) {
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
            params.width = displaymetrics.widthPixels;
            params.height = displaymetrics.heightPixels;
            params.setMargins(0, 0, 0, 0);
            mFullScreen = fullScreen;
        }
        else{
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            final FrameLayout mFrame = (FrameLayout) videoSurfaceContainer;
            int height = mFrame.getHeight();
            int width = mFrame.getWidth();
            android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
            params.width = width;
            params.height= height;
            params.setMargins(0, 0, 0, 0);
            mFullScreen = !fullScreen;
        }
    }

不清楚你在找什么,所以我只是提供一些想法:

-我认为 SurfaceView 不能即时调整大小。您必须 invalidate/recreate 视图。 TextureView 以较小的性能损失提供更好的视图处理。

-如果你只是想切换到全屏 activity(没有 actinobars 等):请参阅 here for android > 4.4 否则你需要在 onCreate 上进行(因此需要 activity 刷新才能生效):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(YOURFULLSCREENCHECKHERE) {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
          WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    setContentView(R.layout.main);
}

我决定让所有其他 ViewGroups 和 ListView 不可见,在这种情况下 SurfaceView 就像在全屏模式下一样

     if (mFullScreen) {
                paneltop.setVisibility(View.GONE);
                panelbottom.setVisibility(View.GONE);
                lvMain.setVisibility(View.GONE);
                mFullScreen = fullScreen;
            }
            else{

                paneltop.setVisibility(View.VISIBLE);
                panelbottom.setVisibility(View.VISIBLE);
                lvMain.setVisibility(View.VISIBLE);
                mFullScreen = !fullScreen;
            }