开发者

Does Android support scaling Video?

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 th开发者_JS百科e Video. Can I force Android to render a video into a smaller or larger rect?


(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);
 }
}


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);
    }
}


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!


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.


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();


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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜