使 surfaceView 进入全屏
Making a surfaceView go fullscreen
我有一个播放直播视频的表面视图。我需要它在横向时全屏显示。我有一个 activity,其中有一个片段,其中包含 surfaceView 和一个显示其他实时流的 gridview。这是片段的布局-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<RelativeLayout
android:id="@+id/guiLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:layout_weight="0.6">
<FrameLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp">
<SurfaceView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="visible">
</SurfaceView>
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@android:color/white">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
android:background="@color/transparent"
android:layout_gravity="center_horizontal|bottom" />
</RelativeLayout>
所以我的 onConfigChanged 在 activity 调用超级 onConfigChanged 然后调用片段 onRotate 方法并将它传递给 newConfig -
public void onRotate(Configuration newConfig)
{
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.e(TAG, "CHANGING");
getView().findViewById(R.id.guiLayout).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
if(gridView != null)
gridView.setVisibility(View.GONE);
getView().findViewById(R.id.layoutGrid).setVisibility(View.GONE);
}
else
{
Log.e(TAG, "CHANGING PORTRAIT");
getView().findViewById(R.id.guiLayout).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ,0.6f));
if(gridView != null)
gridView.setVisibility(View.VISIBLE);
else
gridView = (GridView) getView().findViewById(R.id.gridView);
}
我的想法是,如果我将我的 guiLayout 调整为全屏,它的 children 应该改变以适合,因为它们被设置为 MATCH_PARENT。但事实并非如此,guilayout 调整为全屏,但 children 仍然按照原始重量占据屏幕的 40%。我已经尝试了很多不同的方法来让 children 匹配他们的 parent 但它没有用。任何关于我做错了什么的建议将不胜感激。
全屏:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
videoLayout.setBackgroundColor(Color.BLACK);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
ViewGroup.LayoutParams videoLayoutParams = videoLayout.getLayoutParams();
videoLayoutParams.width = displayMetrics.widthPixels;
videoLayoutParams.height = displayMetrics.heightPixels;
ViewGroup.LayoutParams videoParams = videoView.getLayoutParams();
videoParams.width = displayMetrics.widthPixels;
videoParams.height = displayMetrics.heightPixels;
我有一个播放直播视频的表面视图。我需要它在横向时全屏显示。我有一个 activity,其中有一个片段,其中包含 surfaceView 和一个显示其他实时流的 gridview。这是片段的布局-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<RelativeLayout
android:id="@+id/guiLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:layout_weight="0.6">
<FrameLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="0dp">
<SurfaceView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="visible">
</SurfaceView>
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layoutGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="@android:color/white">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp"
android:background="@color/transparent"
android:layout_gravity="center_horizontal|bottom" />
</RelativeLayout>
所以我的 onConfigChanged 在 activity 调用超级 onConfigChanged 然后调用片段 onRotate 方法并将它传递给 newConfig -
public void onRotate(Configuration newConfig)
{
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.e(TAG, "CHANGING");
getView().findViewById(R.id.guiLayout).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
if(gridView != null)
gridView.setVisibility(View.GONE);
getView().findViewById(R.id.layoutGrid).setVisibility(View.GONE);
}
else
{
Log.e(TAG, "CHANGING PORTRAIT");
getView().findViewById(R.id.guiLayout).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ,0.6f));
if(gridView != null)
gridView.setVisibility(View.VISIBLE);
else
gridView = (GridView) getView().findViewById(R.id.gridView);
}
我的想法是,如果我将我的 guiLayout 调整为全屏,它的 children 应该改变以适合,因为它们被设置为 MATCH_PARENT。但事实并非如此,guilayout 调整为全屏,但 children 仍然按照原始重量占据屏幕的 40%。我已经尝试了很多不同的方法来让 children 匹配他们的 parent 但它没有用。任何关于我做错了什么的建议将不胜感激。
全屏:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
videoLayout.setBackgroundColor(Color.BLACK);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
ViewGroup.LayoutParams videoLayoutParams = videoLayout.getLayoutParams();
videoLayoutParams.width = displayMetrics.widthPixels;
videoLayoutParams.height = displayMetrics.heightPixels;
ViewGroup.LayoutParams videoParams = videoView.getLayoutParams();
videoParams.width = displayMetrics.widthPixels;
videoParams.height = displayMetrics.heightPixels;