2012-06-14 67 views
8

我正在嘗試動態設置Android VideoView的大小。我看過StackOverflow以及互聯網;我發現最好的解決方案是從here。我已經把我的執行如下:調整視頻查看器

public class ResizableVideoView extends VideoView { 

    public ResizableVideoView(Context c) { 
     super(c); 
    } 

    private int mVideoWidth = 100; 
    private int mVideoHeight = 100; 

    public void changeVideoSize(int width, int height) { 
     mVideoWidth = width;  
     mVideoHeight = height; 

     // not sure whether it is useful or not but safe to do so 
     getHolder().setFixedSize(width, height); 

     forceLayout(); 
     invalidate(); // very important, so that onMeasure will be triggered 
    } 

    public void onMeasure(int specwidth, int specheight) { 
     Log.i("onMeasure","On Measure has been called"); 
     setMeasuredDimension(mVideoWidth, mVideoHeight); 
    } 

    public void onDraw(Canvas c) { 
     super.onDraw(c); 
     Log.i("onDraw","Drawing..."); 
    } 
} 

該視頻在Android模擬器以及摩托羅拉Droid X上正確調整大小;但在Motorola Droid上,VideoView會調整大小,但VideoView中播放的視頻不會調整大小。在Motorola Droid上,如果將VideoView設置爲比視頻播放更大的尺寸,則VideoView中會出現黑色背景,VideoView的左上角會在黑色背景頂部播放。

如何在Android中正確調整VideoView的大小?

感謝, 萬斯

回答

3

我的實現是這樣的:

RelativeLayout.LayoutParams videoviewlp = new RelativeLayout.LayoutParams(newwidth, newheight); 
    videoviewlp.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    videoviewlp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
    videoview.setLayoutParams(videoviewlp); 
    videoview.invalidate(); 

由videoview無效,你會強迫它重繪使用新的LayoutParams(和新的層面)的全videoview。

+0

我測試它。但它裁剪的視頻,而不是調整大小!你有什麼主意嗎? – boomz

+1

@boomz我終於在去年夏天創建了一個實現。查看[VideoPlayer](https://github.com/Vance-Turner/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/VideoPlayer.java)課程來自App Inventor項目(調整代碼,截至去年夏天,是我的工作)。 –