Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VideoView crop to square

Using an ImageView, I can set a square height and width (say 100dip x 100dip). Then using android:scaleType="centerCrop" gives me an image which is cropped to square regardless of aspect ratio.

Can we do this with a VideoView?

I've tried just setting a square height and width, but it just re-sizes to fill the square as best as it can while maintaining the aspect ratio, which I guess is completely expected.

It doesn't seem to have any scale or crop properties / methods unlike ImageView, but this in the VideoView documentation makes me think I'm missing something:

[VideoView] ...provides various display options such as scaling and tinting.

Any ideas would be much appreciated.

like image 230
Andy Avatar asked Dec 10 '10 14:12

Andy


People also ask

Can you crop a video to a square?

You can crop the video to any standard shapes, like vertical rectangle, horizontal rectangle, or square.

How do I crop a video to 16 9?

Step 1: Open the video you wish to crop and click on the Edit option. Step 2: Click on the Crop Button. Step 3: Click on the “Aspect Ratio” button on the right top corner of your screen. Step 4: Select the aspect ratio you wish to crop your video to.

Does cropping a video reduce quality?

The answer is yes. The process of cropping video involves re-encoding which will unavoidably cause the downgrade of original quality.


1 Answers

You achive video crop effect using TextureView which require Android API 14 and hardware acceleration. I described it in my article here.

You can also use my TextureVideoView - custom view based on android TextureView which gives you ability to easy play and crop video. This very similar to ImageView#setScaleType

Layout

<com.dd.crop.TextureVideoView
        android:id="@+id/cropTextureView"
        android:layout_width="fill_parent"
        android:layout_height="100dp"/>

Code

TextureVideoView cropTextureView = (TextureVideoView) findViewById(R.id.cropTextureView);
// Use `setScaleType` method to crop video
cropTextureView.setScaleType(TextureVideoView.ScaleType.TOP);
// Use `setDataSource` method to set data source, this could be url, assets folder or path
cropTextureView.setDataSource("http://www.w3schools.com/html/mov_bbb.mp4");
cropTextureView.play();

Hope this will help someone!

like image 143
Dmytro Danylyk Avatar answered Sep 28 '22 08:09

Dmytro Danylyk