Feb 9, 2012

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).


3 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