videoview全屏播放

真TMD的费劲 解决了java

1、VideoView与视频比例缩放:ide

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

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

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 = width * mVideoHeight / 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 } 22this

这就是为何长宽比不能改变的缘由了。由于在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 = width * mVideoHeight / 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 }code

自定义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);
    }

}get

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

相关文章
相关标签/搜索