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);源碼

相關文章
相關標籤/搜索