Monday, 15 April 2013

android - Dynamic VideoView Height Based on Parent Width -



android - Dynamic VideoView Height Based on Parent Width -

i'm trying size videoview parent container width , set height maintain 4:3 aspect ratio. i've seen answers recommend extending videoview class , overriding onmeasure, don't understand parameters i'm getting or how utilize them:

package com.example; import android.content.context; import android.util.attributeset; import android.util.log; import android.widget.videoview; public class myvideoview extends videoview { public myvideoview(context context) { super(context); } public myvideoview (context context, attributeset attrs) { super(context, attrs); } public myvideoview (context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) { log.i("myvideoview", "width="+widthmeasurespec); log.i("myvideoview", "height="+heightmeasurespec); super.onmeasure(widthmeasurespec, heightmeasurespec); } }

result (on nexus 7 tablet):

02-13 21:33:42.515: i/myvideoview(12667): width=1073742463 02-13 21:33:42.515: i/myvideoview(12667): height=1073742303

i'm trying accomplish next layout:

tablet (portrait):

videoview width - total or total screen. videoview height - maintain 4:3 aspect ratio given width listview - appears below videoview select video play.

tablet (landscape):

listview - appears on left of screen, used select video play. videoview - appears on right of screen , should fill remaining width , set height maintain 4:3 aspect ratio.

try this:

@override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int width = getdefaultsize(mvideowidth, widthmeasurespec); int height = getdefaultsize(mvideoheight, heightmeasurespec); /**adjust according desired ratio*/ if (mvideowidth > 0 && mvideoheight > 0) { if (mvideowidth * height > width * mvideoheight) { // log.i("@@@", "image tall, correcting"); height = (width * mvideoheight / mvideowidth); } else if (mvideowidth * height < width * mvideoheight) { // log.i("@@@", "image wide, correcting"); width = (height * mvideowidth / mvideoheight); } else { // log.i("@@@", "aspect ratio correct: " + // width+"/"+height+"="+ // mvideowidth+"/"+mvideoheight); } } setmeasureddimension(width, height); }

where mvideowidth , mvideoheight current dimension of video. hope helps. :)

android android-layout android-widget

No comments:

Post a Comment