Android's VideoView doesn't always stretch video.

Creating a full screen simple video player on Android 2.4.x(GingerBread), I've been annoyed by the problem that VideoView doesn't always stretch videos to full screen(keeping aspect ratio). Reading VideoView's code, I finally found a solution to this. Point is to call getHolder().setFixedSize in onMeasure.

class StretchVideoView(context:Context, attr:AttributeSet)
extends VideoView(context, attrib) {
    def measure(measureSpec:Int):Int = {
        val specMode = View.MeasureSpec.getMode(measureSpec)
        View.MeasureSpec.getSize(measureSpec)
    }

    override def onMeasure(widthMeasureSpec:Int, heightMeasureSpec:Int) {
        val (w,h) = (measure(widthMeasureSpec), measure(heightMeasureSpec))
        getHolder().setFixedSize(w,h)
       super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }
}

By the way, the above code is written in Scala, so please write in Java if you're not Scala guy (Usually not).


Comments

Anonymous said…
Plz Java sample
Anonymous said…
Can you translate this:

val specMode = View.MeasureSpec.getMode(measureSpec)
View.MeasureSpec.getSize(measureSpec)

to Java please?

int specMode = View.MeasureSpec.getMode(measureSpec) +
View.MeasureSpec.getSize(measureSpec)

Is that right?
Anonymous said…
Your scala code is wrong?
http://stackoverflow.com/questions/15646896/scalas-class-to-java

Popular posts from this blog

Subclassing and Signal connect on a same widget crashes PySide application on exit.

Calling OpenCV functions via Cython from Python 3.X.

Showing CPU/Memory usage on tmux status bar(tmuxのステータスバーにCPUとMemoryの使用状況を表示する)