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

蹲街弑〆低调 提交于 2019-12-22 05:27:04

问题


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?


回答1:


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




回答2:


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);
        }

    }
}



回答3:


Use this method

video_player_view.getRotation();

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



来源:https://stackoverflow.com/questions/20468232/how-to-perform-zoom-in-out-on-videoview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!