VideoView大小屏幕切换的总结

 

    最近做一个app要用到视频的播放,毫无疑问的选择了VieoView,但是在使用的时候发现VideoView全屏的时候还是有些学问的。。。先不管那么多了,把可以实现全屏与指定大小页面的效果的解决方案记录下来好了。。。

    步骤1:

      自定义一个VideoView,重写里面的onMeasure

     

  1. package com.jone.jonevideo.widget; 
  2.  
  3. import android.content.Context; 
  4. import android.util.AttributeSet; 
  5. import android.widget.VideoView; 
  6.  
  7. public class MyVideoView extends VideoView { 
  8.  
  9.     public MyVideoView(Context context, AttributeSet attrs, int defStyle) { 
  10.         super(context, attrs, defStyle); 
  11.     } 
  12.  
  13.     public MyVideoView(Context context, AttributeSet attrs) { 
  14.         super(context, attrs); 
  15.     } 
  16.  
  17.     public MyVideoView(Context context) { 
  18.         super(context); 
  19.     } 
  20.  
  21.     @Override 
  22.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  23.         // TODO Auto-generated method stub 
  24.  
  25.         int width = getDefaultSize(0, widthMeasureSpec); 
  26.         int height = getDefaultSize(0, heightMeasureSpec); 
  27.         setMeasuredDimension(width, height); 
  28.     } 
  29.  
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的父布局来实现切换大小]

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical"
  6.  
  7.     <RelativeLayout 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="match_parent"
  10.  
  11.         <com.jone.jonevideo.widget.MyVideoView 
  12.             android:id="@+id/audtoView" 
  13.             android:layout_width="match_parent" 
  14.             android:layout_height="match_parent" /> 
  15.     </RelativeLayout> 
  16.  
  17.     <LinearLayout 
  18.         android:layout_width="match_parent" 
  19.         android:layout_height="wrap_content" 
  20.         android:layout_gravity="bottom"
  21.  
  22.         <Button 
  23.             android:id="@+id/audio_start" 
  24.             style="@style/layout_wrap_content" 
  25.             android:text="start_audio" /> 
  26.  
  27.         <Button 
  28.             android:id="@+id/audio_2_video" 
  29.             style="@style/layout_wrap_content" 
  30.             android:text="@string/switch_audio_2_vidwo" /> 
  31.  
  32.         <Button 
  33.             android:id="@+id/entry_list_audio" 
  34.             style="@style/layout_wrap_content" 
  35.             android:text="@string/entry_audio_list" /> 
  36.     </LinearLayout> 
  37.  
  38. </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: 通过代码来设置全屏与否的切换

  1. if(!fullscreen){//设置RelativeLayout的全屏模式 
  2.        RelativeLayout.LayoutParams layoutParams= 
  3.               new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); 
  4.            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
  5.            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
  6.            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
  7.            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
  8.            mVideoView01.setLayoutParams(layoutParams); 
  9.             
  10.            fullscreen = true;//改变全屏/窗口的标记 
  11.        }else{//设置RelativeLayout的窗口模式 
  12.           RelativeLayout.LayoutParams lp=new  RelativeLayout.LayoutParams(320,240); 
  13.           lp.addRule(RelativeLayout.CENTER_IN_PARENT); 
  14.             mVideoView01.setLayoutParams(lp); 
  15.             fullscreen = false;//改变全屏/窗口的标记 
  16.        }     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值