简体   繁体   English

在SurfaceView扩展中将视频拉伸到全屏

[英]Stretch video to full screen in a SurfaceView extension

I have created a widget that is an extension of SurfaceView (very similar to VideoView ) and I am working on a feature to stretch the video fully across the device screen when certain action is taken. 我创建了一个小部件,它是SurfaceView的扩展(非常类似于VideoView ),我正在开发一项功能,可以在采取某些操作时在设备屏幕上完全展开视频。 I've looked at onMeasure function of VideoView and re-wrote it this way: 我已经看了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);
    }
}

This seems to work fine if I completely stop the video and start playing again. 如果我完全停止视频并再次开始播放,这似乎工作正常。 Now I am trying to force the refresh of this SurfaceView after setting the stretch flag so that the video gets stretched while it is playing, but I couldn't figure out how to force a refresh on SurfaceView . 现在我试图在设置拉伸标志后强制刷新此SurfaceView ,以便视频在播放时得到拉伸,但我无法弄清楚如何在SurfaceView上强制刷新。 I tried android.view.View.invalidate() , android.view.View.refreshDrawableState() and calling android.view.View.measure(int, int) directly with different combinations but did not get any success. 我尝试了android.view.View.invalidate()android.view.View.refreshDrawableState()并直接用不同的组合调用android.view.View.measure(int, int)但没有取得任何成功。 Any ideas? 有任何想法吗?

No need of code to play video in full screen mode 无需代码即可在全屏模式下播放视频

Apply the following layout format over the xml containing the videoview it will for sure will play the video in full screen mode. 在包含videoview的xml上应用以下布局格式,它肯定会以全屏模式播放视频。 as it is running mine :) Hope it helps 因为它正在运行我的:)希望它有所帮助

 <?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>

You can call the measure method of the SurfaceView from the Activity like this: 您可以从Activity中调用SurfaceViewmeasure方法,如下所示:

        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 is the correct answer. Javanator是正确的答案。 There is no need for additional code. 无需额外的代码。 Make sure your video view looks like this 确保您的视频观看次序如下

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM