开发者

VideoView crop to square

开发者 https://www.devze.com 2023-01-30 03:20 出处:网络
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.

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开发者_如何学Go:

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

Any ideas would be much appreciated.


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!


Source implies that it isn't natively supported in the VideoView view. You could copy the source and make it work though. :) Most likely by adding an else at line 255, modifying onMeasure to prevent it from expanding to meet the size, etc.

Edit: Don't be surprised that the Reference documentation is slightly off or just plain wrong.

0

精彩评论

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

关注公众号