Reputation: 1245
I have a video in one size and would like from it to display full size all the time no matter the size of the screen. At the same time I would like to keep aspect ratio of that video so it doesn't stretch i create a bad look.
I can accomplish that stretch effect like this but the video looks bad then:
<?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/videoViewRelative"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</VideoView>
</RelativeLayout>
In my this case it is not important to show whole video to user so I need something like how it's done for ImageView:
android:scaleType="centerCrop"
Is there any crop alternative for VideoView
? Thank you :)
Upvotes: 6
Views: 13897
Reputation: 2104
I need to play vertical video in splash screen. Due to aspect ratio issue, it is not playing well in all screens and not fit to the full screens.
By use of the code posted by @free_style, here is the custom video view which fits the video in full screen. It gives the solution of my problem. May be help to others also.
Use this class instead of default video view.
public class KSplashScreenVideoView extends VideoView {
public KSplashScreenVideoView(Context context) {
super(context);
}
public KSplashScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KSplashScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = (screenHeight * 9) / 16;
setMeasuredDimension(screenWidth, screenHeight);
}
}
Upvotes: 1
Reputation: 2992
It is easier to solve with the latest ConstraintLayout. Convert your layout into ConstraintLayout. Then add the line for the videoView.
app:layout_constraintDimensionRatio="16:9"
Upvotes: 1
Reputation: 3608
I know its an old question.But Just in case this helps someone. In my case i was using a CustomTextureVideoView which extends TextureView
And i was making the view full screen in the
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
((Activity)getContext()).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = (screenHeight*9)/16;
setMeasuredDimension(screenWidth, screenHeight);
}
I am getting the height of the display with DisplayMetrics and adjusting with width of the view so that the video vill keep the aspect ratio. i wanted to keep the aspect ratio of my video to be 16:9
and in layout make the view
android:layout_centerHorizontal="true"
so that the sides will be cut off equally from both sides.
I Wanted to make my video adjust itself for the new fancy aspect ratio 18.5:9 of samsung s8 thats why the aspect ratio is 16:9, in case you are wondering.. ;)
Upvotes: 3
Reputation: 4021
Like this you can set the properties of the video by yourself. Use a SurfaceView (gives you more control on the view), set it to fill_parent to match the whole screen.
Try this code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/surfaceViewFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
</SurfaceView>
</Linearlayout>
then on your java code get the surface view and add your media player to it
surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
player = new MediaPlayer();
player.setDisplay(holder);
Upvotes: 0
Reputation: 2547
You can try to use this library
https://github.com/dmytrodanylyk/video-crop
Upvotes: 4