最近做一个app要用到视频的播放,毫无疑问的选择了VieoView,但是在使用的时候发现VideoView全屏的时候还是有些学问的。。。先不管那么多了,把可以实现全屏与指定大小页面的效果的解决方案记录下来好了。。。
步骤1:
自定义一个VideoView,重写里面的onMeasure
- package com.jone.jonevideo.widget;
- 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);
- }
- }
package com.jone.jonevideo.widget;
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);
}
}
步骤2: 用一个布局来包裹你的VideoView [以后我们会在代码中控制VideoView的父布局来实现切换大小]
- <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>
<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>
步骤3: 通过代码来设置全屏与否的切换
- 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;//改变全屏/窗口的标记
- }