androidsurfaceviewaspect-ratioscaletype

Changing Aspect Ratio of the video on Button Click using Surface View like MX player


I am making a video player android application like MX player. what i want to do is same like in MX player.For example:If the user clicks on the aspect ratio button it should set the video size to 100%. if the user again clicks the button it should set (aspect ratio or scale type to be crop.on next click it should set to stretch. again it should set it to fit to screen etc.


i have developed other logic of the player except this button. i will handle the clicks logic on the aspect ratio button too.


just tell me what code i will need to write to change the video size not the surface view size when the user selects the button.


Note: I am not using video view at all. i am assigning surface view a media player through surface holder. and by default my video plays in full screen


Solution

  • I have done it finally after some research. if anyone is looking for an answer then it is here for you. it is not the correct size though but it is for an example. the set Text is not according to the width and height but it is just to explain you the functionality


    you can get the screen width and height and +/- the amount you want to increase or decrease in width and height whatever you want.


    above your activity, after imports you write this to initialize:

    android.view.ViewGroup.LayoutParams lp;
    

    This piece of code will get the device width and height.

      public void getDeviceWidthAndHeight(){
        lp = surfaceView.getLayoutParams();
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        screenHeight = getWindowManager().getDefaultDisplay().getHeight();
    }
    

    And this piece of code will set the height and width you give to it.

        @Override
    public void onClick(View v) {
    
        int id=v.getId();
        if (id==R.id.resize_video){
            if (clickCount==0){
                getDeviceWidthAndHeight();
                lp.width = screenWidth-50;
                lp.height = screenHeight-50;
                surfaceView.setLayoutParams(lp);
                resizeVideo.setText("100%");
                clickCount=1;
            }
        else if (clickCount==1){
                getDeviceWidthAndHeight();
                lp.width = screenWidth-300;
                lp.height = screenHeight-100;
                surfaceView.setLayoutParams(lp);
                resizeVideo.setText("Full Screen");
                clickCount=2;
        }
        else if(clickCount==2){
                getDeviceWidthAndHeight();
                lp.width =screenWidth;
                lp.height = screenHeight;
                surfaceView.setLayoutParams(lp);
                resizeVideo.setText("100%");
                clickCount=3;
            }
            else if(clickCount==3){
                getDeviceWidthAndHeight();
                lp.width =screenWidth;
                lp.height = screenHeight-500;
                surfaceView.setLayoutParams(lp);
                resizeVideo.setText("Fit to Screen");
                clickCount=0;
            }
        }
    
    }