Does Android support scaling Video?

2020-05-14 08:05发布

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?

7条回答
何必那么认真
2楼-- · 2020-05-14 08:23

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);
    }
}
查看更多
我想做一个坏孩纸
3楼-- · 2020-05-14 08:28

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();
查看更多
成全新的幸福
4楼-- · 2020-05-14 08:33

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!

查看更多
欢心
5楼-- · 2020-05-14 08:35

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);
}
查看更多
Bombasti
6楼-- · 2020-05-14 08:42

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.

查看更多
小情绪 Triste *
7楼-- · 2020-05-14 08:48

(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);
 }
}
查看更多
登录 后发表回答