Android 支持缩放视频吗?

发布于 2024-08-17 13:09:03 字数 95 浏览 2 评论 0原文

使用VideoView可以为Android设置比例因子吗?默认情况下,视频视图会自行调整大小以适应视频的编码分辨率。我可以强制 Android 将视频渲染为更小或更大的矩形吗?

Using a VideoView is it possible to set a scale factor for Android? By Default the video view resizes itself to fit the encoded resolution of the Video. Can I force Android to render a video into a smaller or larger rect?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟─花易冷 2024-08-24 13:09:03

(我知道这是一个非常老的问题,但是还有另一种控制尺寸的方法,这里没有描述,也许有人会发现它很有帮助。)

在布局中声明您自己的 MyVideoView 类并编写您的自己的 onMeasure() 方法。以下是如何运行拉伸到原始视图尺寸的视频:


public class MyVideoView extends VideoView {
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int width = getDefaultSize(0, widthMeasureSpec);
  int height = getDefaultSize(0, heightMeasureSpec);

  setMeasuredDimension(width, height);
 }
}

(I know it's very old question, but there is another way to control dimensions, which isn't described here, maybe someone will find it helpful.)

Declare your own MyVideoView class in your layout and write your own onMeasure() method. Here is how to run video stretched to original View's dimensions:


public class MyVideoView extends VideoView {
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int width = getDefaultSize(0, widthMeasureSpec);
  int height = getDefaultSize(0, heightMeasureSpec);

  setMeasuredDimension(width, height);
 }
}
夏の忆 2024-08-24 13:09:03

要为 VideoView 设置 "centerCrop" 缩放类型,您的 onMeasure()layout() 方法可能如下所示:


public class CenterCropVideoView extends VideoView {
    private int leftAdjustment;
    private int topAdjustment;

 ...
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int videoWidth = getMeasuredWidth();
        int videoHeight = getMeasuredHeight();

        int viewWidth = getDefaultSize(0, widthMeasureSpec);
        int viewHeight = getDefaultSize(0, heightMeasureSpec);

        leftAdjustment = 0;
        topAdjustment = 0;
        if (videoWidth == viewWidth) {
            int newWidth = (int) ((float) videoWidth / videoHeight * viewHeight);
            setMeasuredDimension(newWidth, viewHeight);
            leftAdjustment = -(newWidth - viewWidth) / 2;
        } else {
            int newHeight = (int) ((float) videoHeight / videoWidth * viewWidth);
            setMeasuredDimension(viewWidth, newHeight);
            topAdjustment = -(newHeight - viewHeight) / 2;

        }
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
    }
}

To set "centerCrop" scale type for VideoView your onMeasure() and layout() methods may look like this:


public class CenterCropVideoView extends VideoView {
    private int leftAdjustment;
    private int topAdjustment;

 ...
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int videoWidth = getMeasuredWidth();
        int videoHeight = getMeasuredHeight();

        int viewWidth = getDefaultSize(0, widthMeasureSpec);
        int viewHeight = getDefaultSize(0, heightMeasureSpec);

        leftAdjustment = 0;
        topAdjustment = 0;
        if (videoWidth == viewWidth) {
            int newWidth = (int) ((float) videoWidth / videoHeight * viewHeight);
            setMeasuredDimension(newWidth, viewHeight);
            leftAdjustment = -(newWidth - viewWidth) / 2;
        } else {
            int newHeight = (int) ((float) videoHeight / videoWidth * viewWidth);
            setMeasuredDimension(viewWidth, newHeight);
            topAdjustment = -(newHeight - viewHeight) / 2;

        }
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
    }
}
晚风撩人 2024-08-24 13:09:03

默认情况下,视频视图会调整大小
本身适合编码分辨率
视频的内容。

VideoView(或与 MediaPlayer 一起使用的 SurfaceView)将是您在布局中指定的大小。 在该空间内,视频将尽可能大地播放,同时保持宽高比。

我可以强制 Android 渲染视频吗
进入更小或更大的矩形?

是的:将您的 VideoView 设置为您想要的大小,Android 将缩放以适合 VideoView 的大小。

By Default the video view resizes
itself to fit the encoded resolution
of the Video.

VideoView (or the SurfaceView for use with MediaPlayer) will be the size you tell it to be in your layout. Within that space, the video will play back as large as possible while maintaining the aspect ratio.

Can I force Android to render a video
into a smaller or larger rect?

Yes: make your VideoView be the size you want, and Android will scale to fit the size of the VideoView.

梦里南柯 2024-08-24 13:09:03

我发现当 VideoView 放置在relativelayout 中时,视频会拉伸高度和宽度以适合 VideoView 指定的高度和宽度(无论视频宽高比如何)。但是,当我将 VideoView 放入 FrameLayout 中时,视频会拉伸高度和宽度,直到它与 VideoView 的指定高度或宽度之一匹配(即,它不会破坏宽高比) )。奇怪,我知道,但这就是我发现的!

I find that when a VideoView is placed inside a RelativeLayout, the video stretches both height and width to fit the VideoView's specified height and width (irrespective of the video aspect ratio). However, when I place the VideoView in a FrameLayout, the video stretches height and width until it matches one of the VideoView's specified height or width (i.e. it does not break aspect ratio). Strange, I know, but that's what I found!

国粹 2024-08-24 13:09:03

我也在努力实现这一目标,到目前为止效果还不错。这个想法是使用视频视图的 setLayoutParams 并指定大小。这只是一个简单的片段,但它给出了想法。检查 LayoutParams 行。

VideoView mVideoView = new VideoView(this);

//intermediate code

mVideoView.setVideoPath("/sdcard/VIDEO0007.3gp");
MediaController mController = new MediaController(this);
mVideoView.setMediaController(mController);
mController.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
    int width = mVideoView.getMeasuredWidth();
    int height = mVideoView.getMeasuredHeight();
    //we add 10 pixels to the current size of the video view every time you touch     
    //the media controller.
    LayoutParams params = new LinearLayout.LayoutParams(width+10, height+10);
    mVideoView.setLayoutParams(params);

    return true;
}
});

mVideoView.requestFocus();
mVideoView.start();

I am also trying to achieve that and so far this has worked ok. The idea is to use the setLayoutParams for the video view and specify the size. It is just a simple fragment but it gives the idea. Check the LayoutParams lines.

VideoView mVideoView = new VideoView(this);

//intermediate code

mVideoView.setVideoPath("/sdcard/VIDEO0007.3gp");
MediaController mController = new MediaController(this);
mVideoView.setMediaController(mController);
mController.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
    int width = mVideoView.getMeasuredWidth();
    int height = mVideoView.getMeasuredHeight();
    //we add 10 pixels to the current size of the video view every time you touch     
    //the media controller.
    LayoutParams params = new LinearLayout.LayoutParams(width+10, height+10);
    mVideoView.setLayoutParams(params);

    return true;
}
});

mVideoView.requestFocus();
mVideoView.start();
南风起 2024-08-24 13:09:03

FeelGoods 回答。

如果没有layout()方法,“centerCrop”缩放类型更好,但VideoView必须位于FrameLayout中。

@Override
public void layout(int l, int t, int r, int b) {
    super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
}

To FeelGoods answer.

Without the layout() method the "centerCrop" scale type is better, but the VideoView must be in a FrameLayout.

@Override
public void layout(int l, int t, int r, int b) {
    super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
}
薄荷港 2024-08-24 13:09:03

伙计们,我有其他想法通过扩展 VideoView 创建您自己的 VideoView 类,这样您就可以做任何您想做的事情。确切地说,您必须在 VideoView 中创建几个方法。

public void setDimensions(int w, int h) {
    this.mForceHeight = h;
    this.mForceWidth = w;

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(mForceWidth, mForceHeight);
}

为您的 VideoView 类创建您自己的构造函数,并确保您自己的 videoview 类中使用此方法,然后将其像 android 默认 videoview 一样使用到 xml 和类文件中。

希望有帮助。

Guys I had other idea create your own VideoView class by extending VideoView, with that u can do what ever u want,. exactly u have to create couple of methods in your VideoView.

public void setDimensions(int w, int h) {
    this.mForceHeight = h;
    this.mForceWidth = w;

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(mForceWidth, mForceHeight);
}

make your own constructor for ur VideoView class and make sure this methods in ur own videoview class then use it as like androids default videoview into both xml and class files.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文