-
-
Save dreampowder/fdf993d5a23df4176f3d5af5b433a605 to your computer and use it in GitHub Desktop.
Android : How to stretch video to use whole area of VideoView (sode snippets)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.raulete.dev.stretchvideoview.utils; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.VideoView; | |
public class StretchVideoView extends VideoView { | |
public StretchVideoView(Context context) { | |
super(context); | |
} | |
public StretchVideoView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public StretchVideoView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
//Log.i("@@@@", "onMeasure"); | |
int width = getDefaultSize(mVideoWidth, widthMeasureSpec); | |
int height = getDefaultSize(mVideoHeight, heightMeasureSpec); | |
if (mVideoWidth > 0 && mVideoHeight > 0) { | |
if ( mVideoWidth * height > width * mVideoHeight ) { | |
//Log.i("@@@", "image too tall, correcting"); | |
height = width * mVideoHeight / mVideoWidth; | |
} else if ( mVideoWidth * height < width * mVideoHeight ) { | |
//Log.i("@@@", "image too wide, correcting"); | |
width = height * mVideoWidth / mVideoHeight; | |
} else { | |
//Log.i("@@@", "aspect ratio is correct: " + | |
//width+"/"+height+"="+ | |
//mVideoWidth+"/"+mVideoHeight); | |
} | |
} | |
//Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height); | |
setMeasuredDimension(width, height); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<VideoView android:id="@+id/videoViewRelative" | |
android:layout_alignParentTop="true" | |
android:layout_alignParentBottom="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentRight="true" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
</VideoView> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment