videoview全屏播放

一、VideoView与视频比例缩放:

以前在论坛上也看到有人问过如何实现视频按比例缩放的问题。的确,如果仅仅使用VideoView可能达不到我们想要达到的效果。这就需要我们对VideoView做一些改动,简单的说就是另外写一个类似VideoView的类出来(庆幸Android是开源的)。

我们可以很方便的获得VideoView的源代码,最简单的方法是直接在GoogleCodeSearch上找“VideoView.java”。所以重写VideoView的过程其实只是在原来的基础上进行一些修改而已,并非一个很麻烦的工作。为什么Android自带的VideoView会保持视频的长宽比而不能让我们很方便的自定义比例呢?我猜想可能GoogleAndroid也是一个很仓促的工程,许多代码并没有考虑得太成熟。

VideoView的源码中有这样一段代码:

1 @Override 2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 3 //Log.i("@@@@", "onMeasure"); 4 int width = getDefaultSize(mVideoWidth, widthMeasureSpec); 5 int height = getDefaultSize(mVideoHeight, heightMeasureSpec); 6 if (mVideoWidth > 0 && mVideoHeight > 0) { 7 if ( mVideoWidth height > width mVideoHeight ) { 8 //Log.i("@@@", "image too tall, correcting"); 9 height = widthmVideoHeight / mVideoWidth; 10 } else if ( mVideoWidth height < width mVideoHeight ) { 11 //Log.i("@@@", "image too wide, correcting"); 12 width = height mVideoWidth / mVideoHeight; 13 } else { 14 //Log.i("@@@", "aspect ratio is correct: " + 15 //width+"/"+height+"="+ 16 //mVideoWidth+"/"+mVideoHeight); 17 } 18 } 19 //Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height); 20 setMeasuredDimension(width, height); 21 } 22

这就是为什么长宽比不能改变的原因了。因为在OnMeasure的时候,就对这个长宽比进行了处理。

我们把其中处理的代码屏蔽掉,视频大小就可以随着VideoView的长宽改变而改变了。

1 @Override 2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 3 //Log.i("@@@@", "onMeasure"); 4 int width = getDefaultSize(mVideoWidth, widthMeasureSpec); 5 int height = getDefaultSize(mVideoHeight, heightMeasureSpec); 6 /*//if (mVideoWidth > 0 && mVideoHeight > 0) { 7 if ( mVideoWidth height > width mVideoHeight ) { 8 //Log.i("@@@", "image too tall, correcting"); 9 height = widthmVideoHeight / mVideoWidth; 10 } else if ( mVideoWidth height < width mVideoHeight ) { 11 //Log.i("@@@", "image too wide, correcting"); 12 width = height mVideoWidth / mVideoHeight; 13 } else { 14 //Log.i("@@@", "aspect ratio is correct: " + 15 //width+"/"+height+"="+ 16 //mVideoWidth+"/"+mVideoHeight); 17 } 18 }*/ 19 //Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height); 20 setMeasuredDimension(width,height); 21 }

自定义VideoView public class CustomVideoView extends VideoView {

    private int mVideoWidth;
    private int mVideoHeight;

    public CustomVideoView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
    }

    public CustomVideoView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
    }

    public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            // Log.i("@@@@", "onMeasure");

           //下面的代码是让视频的播放的长宽是根据你设置的参数来决定

            int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
            int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
            setMeasuredDimension(width, height);
    }

}

MediaController controller = new MediaController(this); mVideoView = (CustomVideoView)findViewById(R.id.videoView1); mVideoView.setMediaController(controller); mVideoView.setVideoPath(mVideoPath);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值