Created
January 16, 2014 13:40
-
-
Save anry200/8455113 to your computer and use it in GitHub Desktop.
Android : How to stretch video to use whole area of VideoView (code 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
<?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"> | |
<pakage.path.ScalableVideoView | |
android:id="@+id/scalableVideoView" | |
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" /> | |
</RelativeLayout> |
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
http://clseto.mysinablog.com/index.php?op=ViewArticle&articleId=2992625 | |
https://code.google.com/p/googletv-android-samples/source/browse/aspect-ratio-video-library/src/com/google/android/tv/library/aspectratio/video/AspectRatioVideoView.java |
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
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.VideoView; | |
public class ScalableVideoView extends VideoView { | |
private int mVideoWidth; | |
private int mVideoHeight; | |
private DisplayMode displayMode = DisplayMode.ORIGINAL; | |
public enum DisplayMode { | |
ORIGINAL, // original aspect ratio | |
FULL_SCREEN, // fit to screen | |
ZOOM // zoom in | |
}; | |
public ScalableVideoView(Context context) { | |
super(context); | |
} | |
public ScalableVideoView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ScalableVideoView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
mVideoWidth = 0; | |
mVideoHeight = 0; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int width = getDefaultSize(0, widthMeasureSpec); | |
int height = getDefaultSize(mVideoHeight, heightMeasureSpec); | |
if (displayMode == DisplayMode.ORIGINAL) { | |
if (mVideoWidth > 0 && mVideoHeight > 0) { | |
if ( mVideoWidth * height > width * mVideoHeight ) { | |
// video height exceeds screen, shrink it | |
height = width * mVideoHeight / mVideoWidth; | |
} else if ( mVideoWidth * height < width * mVideoHeight ) { | |
// video width exceeds screen, shrink it | |
width = height * mVideoWidth / mVideoHeight; | |
} else { | |
// aspect ratio is correct | |
} | |
} | |
} | |
else if (displayMode == DisplayMode.FULL_SCREEN) { | |
// just use the default screen width and screen height | |
} | |
else if (displayMode == DisplayMode.ZOOM) { | |
// zoom video | |
if (mVideoWidth > 0 && mVideoHeight > 0 && mVideoWidth < width) { | |
height = mVideoHeight * width / mVideoWidth; | |
} | |
} | |
setMeasuredDimension(width, height); | |
} | |
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); | |
requestLayout(); | |
invalidate(); // very important, so that onMeasure will be triggered | |
} | |
public void setDisplayMode(DisplayMode mode) { | |
displayMode = mode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment