Android Surfaceview Content Center Display

Source: Internet
Author: User

Cause of the problem:

Recently prepared to encapsulate a video player, I used the Surfaceview + mediaplayer approach.

Video loading display screen after encountering problems: " video distortion, proportional deformation ." ”

Problem:

Solution Ideas:

The video size is automatically populated with Surfaceview, so it's good to adjust the video size Surfaceview.

By listening to the MediaPlayer callback function "Onvideosizechanged" in the inside to modify the width of the surfaceview.

Changevideosize
 Public voidchangevideosize () {intVideowidth =mediaplayer.getvideowidth (); intVideoheight =mediaplayer.getvideoheight (); //calculates the maximum multiples of video that can be magnified in sufaceview based on the video size.         floatMax; if(Getresources (). GetConfiguration (). orientation==activityinfo.screen_orientation_portrait) {            //magnification by video width in portrait modemax = Math.max ((float) Videowidth/(float) Surfacewidth, (float) Videoheight/(float) surfaceheight); } Else{            //zoom in by video height in landscape modemax = Math.max ((float) videowidth/(float) (Surfaceheight), (float) videoheight/(float) surfacewidth); }                //Video Width-height difference/max-times numerical calculation of enlarged video sizeVideowidth = (int) Math.ceil (float) Videowidth/max); Videoheight= (int) Math.ceil (float) Videoheight/max); //The video size cannot be set directly, and the computed video size is set to Surfaceview for the video to be automatically populated. Surfaceview.setlayoutparams (Newrelativelayout.layoutparams (Videowidth, videoheight)); }

Note: The size of the Surfaceview requires a record to be saved as a fixed value , or the size changes after the screen is rotated.

Effect:

Full code:

 Public classMainactivityextendsAppcompatactivityImplementsSurfaceholder.callback, Mediaplayer.onvideosizechangedlistener {PrivateSurfaceview Surfaceview; PrivateMediaPlayer MediaPlayer; Private intSurfacewidth; Private intSurfaceheight; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Initialize page/object/variable/listener eventsSetcontentview (R.layout.activity_main); Surfaceview=(Surfaceview) Findviewbyid (R.id.surfaceview); Surfaceview.getholder (). Addcallback ( This); }    Private voidplayvideo (String url) {Try{Mediaplayer.setdatasource ( This, Uri.parse (URL));            Mediaplayer.prepare (); Mediaplayer.start ();} Catch(IOException e) {e.printstacktrace ();} } @Override Public voidsurfacecreated (Surfaceholder surfaceholder) {MediaPlayer=NewMediaPlayer (); Mediaplayer.setonvideosizechangedlistener ( This); Mediaplayer.setdisplay (Surfaceholder);
if(Getresources (). GetConfiguration (). orientation==activityinfo.screen_orientation_portrait) {Surfacewidth=surfaceview.getwidth (); Surfaceheight=surfaceview.getheight (); }Else{surfacewidth=surfaceview.getheight (); Surfaceheight=surfaceview.getwidth (); } playvideo ("http://pgccdn.v.baidu.com/258500786_2854944371_20171201122626.mp4?authorization=bce-auth-v1% 2fc308a72e7b874edd9115e4614e1d62f6%2f2017-12-01t04%3a26%3a31z%2f-1%2f% 2fcbe73c8f603a65a23a019236ad32c090ba08f587acae4011d388f153d912724f&responsecachecontrol=max-age%3d8640000 &responseexpires=sun%2c+11+mar+2018+12%3a26%3a31+gmt&xcode= 0d70b526aeacd2423ca650defc16c18d96d564747874148f&time=1512191597&_=1512108201312 "); } @Override Public voidSurfacechanged (Surfaceholder Surfaceholder,intIintI1,intI2) {} @Override Public voidsurfacedestroyed (Surfaceholder surfaceholder) {mediaplayer.release (); MediaPlayer=NULL; }     Public voidchangevideosize () {intVideowidth =mediaplayer.getvideowidth (); intVideoheight =mediaplayer.getvideoheight (); //calculates the maximum multiples of video that can be magnified in sufaceview based on the video size.         floatMax; if(Getresources (). GetConfiguration (). orientation==activityinfo.screen_orientation_portrait) {            //magnification by video width in portrait modemax = Math.max ((float) Videowidth/(float) Surfacewidth, (float) Videoheight/(float) surfaceheight); } Else{            //zoom in by video height in landscape modemax = Math.max ((float) videowidth/(float) (Surfaceheight), (float) videoheight/(float) surfacewidth); }        //Video Width-height difference/max-times numerical calculation of enlarged video sizeVideowidth = (int) Math.ceil (float) Videowidth/max); Videoheight= (int) Math.ceil (float) Videoheight/max); //The video size cannot be set directly, and the computed video size is set to Surfaceview for the video to be automatically populated. Surfaceview.setlayoutparams (Newrelativelayout.layoutparams (Videowidth, videoheight)); } @Override Public voidOnvideosizechanged (MediaPlayer MP,intWidthintheight)    {changevideosize (); } @Override Public voidonconfigurationchanged (Configuration newconfig) {Super. onconfigurationchanged (Newconfig);    Changevideosize (); }}

Finally, we hope that we can provide other algorithms, there should be better.

Android Surfaceview Content Center Display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.