调整VideoView的大小

时间:2012-06-14 19:41:06

标签: android android-widget android-videoview

我正在尝试动态设置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模拟器和Motorola Droid X上正确调整大小;但在摩托罗拉Droid上,VideoView会调整大小,但VideoView中播放的视频不会调整大小。在摩托罗拉Droid上,如果VideoView设置为比视频播放更大的尺寸,VideoView中会出现黑色背景,视频在VideoView的左上角播放在黑色背景的顶部。

如何在Android中正确调整VideoView的大小?

谢谢, 万斯

1 个答案:

答案 0 :(得分: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();

通过使视频视频无效,您将强制它使用新的LayoutParams(以及新尺寸)重绘整个视频视图。