代码之家  ›  专栏  ›  技术社区  ›  haseman

Android是否支持缩放视频?

  •  26
  • haseman  · 技术社区  · 15 年前

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

    7 回复  |  直到 8 年前
        1
  •  9
  •   CommonsWare    15 年前

    默认情况下,视频视图会调整大小 自身以适应编码的分辨率 视频中的。

    VideoView (或 SurfaceView 供使用 MediaPlayer )将是你告诉它在你的布局大小。 在那个空间里 ,视频将在保持纵横比的同时尽可能大的播放。

    我能强制android呈现视频吗 变小还是变大?

    是的:让你的 播放视频 根据您的需要调整大小,android将根据 播放视频 .

        2
  •  56
  •   Michał Kowalczuk    14 年前

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

    声明你自己的 MyVIEW视图 在你的布局中分类并编写你自己的 度量() 方法。以下是如何将视频拉伸到原始视图的尺寸:

    
    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);
     }
    }
    
        3
  •  10
  •   Adil Hussain    12 年前

    我发现当一个视频视图被放置在一个相对延迟中时,视频会延伸 二者都 适合VideoView指定高度和宽度的高度和宽度(与视频纵横比无关)。但是,当我将videoview放置在framelayout中时,视频会拉伸高度和宽度,直到匹配为止 什么之中的一个 视频视图的指定高度或宽度(即它不会破坏纵横比)。奇怪,我知道,但这就是我发现的!

        4
  •  10
  •   FeelGood    10 年前

    设置 "centerCrop" 刻度类型 VideoView 你的 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);
        }
    }
    
        5
  •  3
  •   silver_mx    13 年前

    我也在努力实现这一点,到目前为止,这一切都很顺利。其思想是使用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();
    
        6
  •  2
  •   Joao Ferreira    8 年前

    情感物品 回答。

    如果没有layout()方法, 中心作物 缩放类型更好,但VideoView必须在FrameLayout中。

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l + leftAdjustment, t + topAdjustment, r + leftAdjustment, b + topAdjustment);
    }
    
        7
  •  0
  •   Murali    11 年前

    伙计们,我有另外一个主意,就是通过扩展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和类文件中。

    希望有帮助。