简体   繁体   中英

Android SurfaceView to full screen

I have a media player with SurfaceView. I try to find a solution to switch between full screen and embedded mode. Custom media controller was developed for MediaPlayer using this popular article http://www.brightec.co.uk/ideas/custom-android-media-controller . So now I have button on custom media controller to switch mode.

XML of activity (not full)

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

I have a found a solution to change size of SurfaceView, size is changed but doesn't go to full screen, picture is in their frame:

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

It isn't clear what it is you are looking for, so I'll just offer some ideas:

-I think SurfaceView cannot be resized on the fly. You have to invalidate/recreate the view. TextureView offers better view handling with a small performance hit.

-If you just want to switch to fullscreen activity (no actinobars etc): see here for android > 4.4 otherwise you need to do it on the onCreate (thus requiring activity refresh to take effect):

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

I decided to make all other ViewGroups and ListView invisible, in this case SurfaceView is as in the full screen mode

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM