Arun Badole
Arun Badole

Reputation: 11097

How to perform zoom in/out on VideoView in android?

I am using VideoView & running videos from resources.

I want to know, is there any way by which I can perform zoom in/out functionality on running video?

Upvotes: 8

Views: 13792

Answers (4)

Found a tricky method to implement the same zooming methods used by all of the video player applications(mx player, vlc)

You can change the Videoview Layout params on the basics of screen width and height.

zoom++;
if (zoom == 1) {
    videoview1.setLayoutParams(new LinearLayout.LayoutParams(getDisplayWidthPixels(getApplicationContext()) + 500, getDisplayHeightPixels(getApplicationContext()) + 200));
    Toast.showMessage(getApplicationContext(), "1");
}

Upvotes: 0

Prashant Patil
Prashant Patil

Reputation: 11

Use this method

video_player_view.getRotation();

It is get at horizontal mode and Video shows on full screen

Upvotes: 0

Ritesh
Ritesh

Reputation: 1036

You can use the solution provided in this link https://github.com/father2sisters/scale_videoview

I have updated the above solution to handle orientation changes.

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.FrameLayout;


public class VideoZoomActivity extends AppCompatActivity {

    int MIN_WIDTH;
    private FrameLayout frameLayout;
    private FrameLayout.LayoutParams mRootParam;
    private VodView mVodView;
    private ScaleGestureDetector mScaleGestureDetector;
    private GestureDetector mGestureDetector;
    DisplayMetrics dm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_zoom);

        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        MIN_WIDTH = dm.widthPixels;
        Log.d("WIDTH==",MIN_WIDTH+"");
        frameLayout = (FrameLayout)findViewById(R.id.root_view);
        mRootParam = (FrameLayout.LayoutParams)frameLayout.getLayoutParams();
        mVodView = (VodView) findViewById(R.id.vodView1);
        mVodView.setVideoPath("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");

        // set up gesture listeners
        mScaleGestureDetector = new ScaleGestureDetector(this, new MyScaleGestureListener());
        mGestureDetector = new GestureDetector(this, new MySimpleOnGestureListener());
        mVodView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mGestureDetector.onTouchEvent(event);
                mScaleGestureDetector.onTouchEvent(event);
                return true;
            }
        });
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen

        getWindowManager().getDefaultDisplay().getMetrics(dm);
        MIN_WIDTH = dm.widthPixels;
        mVodView.setFixedVideoSize(dm.widthPixels,dm.heightPixels);
        mRootParam.width = dm.widthPixels;
        mRootParam.height = dm.heightPixels;


        getWindowManager().getDefaultDisplay().getMetrics(dm);
        MIN_WIDTH = dm.widthPixels;
        mVodView.setFixedVideoSize(dm.widthPixels,dm.heightPixels);
        mRootParam.width = dm.widthPixels;

    }

    @Override
    protected void onResume() {
        mVodView.start();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mVodView.pause();
        super.onPause();
    }

    private class MySimpleOnGestureListener extends SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if (mVodView == null)
                return false;
            if (mVodView.isPlaying())
                mVodView.pause();
            else
                mVodView.start();
            return true;
        }
    }

    private class MyScaleGestureListener implements OnScaleGestureListener {
        private int mW, mH;
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // scale our video view
            mW *= detector.getScaleFactor();
            mH *= detector.getScaleFactor();
            if (mW < MIN_WIDTH) { // limits width
                mW = mVodView.getWidth();
                mH = mVodView.getHeight();
            }
            Log.d("onScale", "scale=" + detector.getScaleFactor() + ", w=" + mW + ", h=" + mH);
            mVodView.setFixedVideoSize(mW, mH); // important
            mRootParam.width = mW;
            mRootParam.height = mH;
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            mW = mVodView.getWidth();
            mH = mVodView.getHeight();
            Log.d("onScaleBegin", "scale=" + detector.getScaleFactor() + ", w=" + mW + ", h=" + mH);
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            Log.d("onScaleEnd", "scale=" + detector.getScaleFactor() + ", w=" + mW + ", h=" + mH);
        }

    }
}

Upvotes: 0

Graham Smith
Graham Smith

Reputation: 25757

OK I had this issue and solved it by removing the VideoView and replaced it with a TextureView. You can then apply a Matrix transformation which includes lots of options including zooming.

The method for the Matrix I would use is the postScale() method. You can apply multiple effects pre and post, which you can view in the documentation.

Edit

Here is a custom VideoView from a running project that we used. You can decalre it in XML Layouts and it has a function called setMatrix() which takes a Matrix argument. The original code was written by Alex Ross, we then modified it to deal with the Matrix functionality.

http://pastebin.com/KwQvBWs1

Upvotes: 7

Related Questions