[英]Stretch video to full screen in a SurfaceView extension
我創建了一個小部件,它是SurfaceView
的擴展(非常類似於VideoView ),我正在開發一項功能,可以在采取某些操作時在設備屏幕上完全展開視頻。 我已經看了onMeasure
的功能VideoView
並重新寫的是這樣的:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mStretchVideo) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if (mVideoWidth * height > width * mVideoHeight) {
height = width * mVideoHeight / mVideoWidth;
} else if (mVideoWidth * height < width * mVideoHeight) {
width = height * mVideoWidth / mVideoHeight;
}
}
setMeasuredDimension(width, height);
}
}
如果我完全停止視頻並再次開始播放,這似乎工作正常。 現在我試圖在設置拉伸標志后強制刷新此SurfaceView
,以便視頻在播放時得到拉伸,但我無法弄清楚如何在SurfaceView
上強制刷新。 我嘗試了android.view.View.invalidate()
, android.view.View.refreshDrawableState()
並直接用不同的組合調用android.view.View.measure(int, int)
但沒有取得任何成功。 有任何想法嗎?
無需代碼即可在全屏模式下播放視頻
<?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/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
</VideoView>
</RelativeLayout>
您可以從Activity中調用SurfaceView
的measure
方法,如下所示:
Display display = getWindowManager().getDefaultDisplay();
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(display.getWidth(),
MeasureSpec.UNSPECIFIED);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(display.getHeight(),
MeasureSpec.UNSPECIFIED);
surfaceView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Javanator是正確的答案。 無需額外的代碼。 確保您的視頻觀看次序如下
<VideoView android:id="@+id/myvideoview"
android:layout_width="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.