在android中,是不允许给VideoView自行设置高度的,宽度是可以的,所以只能通过自定VideoView来设置高度,具体步骤如下:
1.在项目的values文件夹下建立文件attrs.xml,在里面添加如下内容:
<span style="font-size:24px;"><strong><resources>
<!-- VideoView的相关属性 -->
<declare-styleable name="MyVideoView">
<attr name="videoView_width" format="dimension"/>
<attr name="videoView_height" format="dimension"/>
</declare-styleable>
</resources></strong></span>
2.在布局文件中添加改自定义组件:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.view.MyVideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vv_camera_player"
app:videoView_width="120dp"
app:videoView_height="180dp"/>
</RelativeLayout>
3.定义一个类继承自VideoView,在onMeasure中进行高度设置:
<strong style="font-size:24px;">public class MyVideoView extends VideoView {
TypedArray typedArray ;
private int viewWidth ;
private int viewHeight ;
public HoloVideoView(Context context) {
this(context,null);
}
public HoloVideoView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public HoloVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyVideoView);
viewWidth = typedArray.getDimensionPixelSize(R.styleable.</strong><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">MyVideoView</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">_videoView_width, 0) ;</span><strong style="font-size:18px;">
<span style="font-size:18px;"> </span><span style="font-size:24px;">viewHeight = typedArray.getDimensionPixelSize(R.styleable.</span></strong><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:24px;">MyVideoView</span></span><strong><span style="font-size:24px;">_videoView_height,0) ;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(viewWidth, viewHeight);
}
}</span></strong>
4.最后在Activity中使用正常的VideoView一样使用即可,视频文件放在raw目录下,播放视频文件代码如下:
String path = "android.resource://"+ getPackageName() + "/raw/guide_video" ;
if (path!=null) {
Uri uri = Uri.parse(path);
vvCamera.setMediaController(new MediaController(this));
vvCamera.setVideoURI(uri);
vvCamera.start();
}