Android中VideoView视频的全屏和半屏的切换
- private boolean fullscreen=false;
if(!fullscreen){//设置RelativeLayout的全屏模式 - RelativeLayout.LayoutParams layoutParams=
- new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- mVideoView01.setLayoutParams(layoutParams);
- fullscreen = true;//改变全屏/窗口的标记
- }else{//设置RelativeLayout的窗口模式
- RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(320,240);
- lp.addRule(RelativeLayout.CENTER_IN_PARENT);
- mVideoView01.setLayoutParams(lp);
- fullscreen = false;//改变全屏/窗口的标记
- }
在oncreate方法之前调用
//去掉头信息
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
//判断手机屏幕的方向
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width=dm.widthPixels;
heigh=dm.heightPixels;
if(width/heigh>0)
{
//横屏
path = bundle.getString("widthurl");
- RelativeLayout.LayoutParams layoutParams=
- new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- mVideoView01.setLayoutParams(layoutParams);
- fullscreen = true;//改变全屏/窗口的标记
}
if(width/heigh==0)
{
//竖屏
path = bundle.getString("heighturl");
- RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(320,240);
- lp.addRule(RelativeLayout.CENTER_IN_PARENT);
- mVideoView01.setLayoutParams(lp);
- fullscreen = false;//改变全屏/窗口的标记
}
第二种方法
- import android.content.Context;
- import android.util.AttributeSet;
- import android.widget.VideoView;
- public class MyVideoView extends VideoView {
- public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public MyVideoView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyVideoView(Context context) {
- super(context);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- // TODO Auto-generated method stub
- int width = getDefaultSize(0, widthMeasureSpec);
- int height = getDefaultSize(0, heightMeasureSpec);
- setMeasuredDimension(width, height);
- }
- }
- --------------------
布局文件 - <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <com.jone.jonevideo.widget.MyVideoView
- android:id="@+id/audtoView"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </RelativeLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom" >
- <Button
- android:id="@+id/audio_start"
- style="@style/layout_wrap_content"
- android:text="start_audio" />
- <Button
- android:id="@+id/audio_2_video"
- style="@style/layout_wrap_content"
- android:text="@string/switch_audio_2_vidwo" />
- <Button
- android:id="@+id/entry_list_audio"
- style="@style/layout_wrap_content"
- android:text="@string/entry_audio_list" />
- </LinearLayout>
- </FrameLayout>
- ------------
全屏的设置 - if(!fullscreen){//设置RelativeLayout的全屏模式
- RelativeLayout.LayoutParams layoutParams=
- new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- mVideoView01.setLayoutParams(layoutParams);
- fullscreen = true;//改变全屏/窗口的标记
- }else{//设置RelativeLayout的窗口模式
- RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(320,240);
- lp.addRule(RelativeLayout.CENTER_IN_PARENT);
- mVideoView01.setLayoutParams(lp);
- fullscreen = false;//改变全屏/窗口的标记
- }