Android SurfaceView全屏

时间:2015-11-09 10:57:38

标签: android surfaceview

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

活动的XML(未完整)

 <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;
        }
    }

2 个答案:

答案 0 :(得分:0)

目前还不清楚你在寻找什么,所以我只是提出一些想法:

- 我认为SurfaceView无法动态调整大小。您必须使视图无效/重新创建。 TextureView提供更好的视图处理效果,但性能很小。

- 如果您只想切换到全屏活动(没有actinobars等):请参阅here for android&gt; 4.4否则你需要在onCreate上执行它(因此需要活动刷新才能生效):

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);
}

答案 1 :(得分:0)

我决定让所有其他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;
            }