如何将视频转换为圆形?

时间:2018-02-12 21:34:08

标签: android android-custom-view crop android-videoview

我想在videoview全屏播放视频,过了一段时间我想cropcircular view

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

circuler transprent

另一种方法是将这种类型的图像叠加放在videoview Relative或FrameLayout上(循环器是transprent,因此videoview只能在圆圈中看到)

默认情况下,可视化GONE此图像,并在需要时更改其运行时。

可能对您有帮助

答案 1 :(得分:0)

有一个更好的方法。

您可以创建一个自定义的SurfaceView。实际将视图裁剪为圆形。并且可以从此自定义视图将显示设置为MediaPlayer对象。

public class VideoSurfaceView extends SurfaceView {

    private final static String TAG = "VideoSurfaceView";
    private Path clipPath;
    private boolean isCircular;

    public VideoSurfaceView(Context context) {
        super(context);
        init();
    }

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        clipPath = new Path();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        if (this.isCircular)
            canvas.clipPath(clipPath);
        super.dispatchDraw(canvas);
    }

    /**
     * Crops the view in circular shape
     * @param centerX
     * @param centerY
     * @param radius
     */
    public void cropCircle(float centerX, float centerY, int radius) {
        Log.i(TAG, "cropCircle: x=" + centerX + " ,y= " + centerY + ", radius=" + radius);
        clipPath.addCircle(centerX, centerY, radius, Path.Direction.CW);
    }

    /**
     * Sets the flag for cropping the view in circular shape
     * @param isCircular
     */
    public void setCircular(boolean isCircular) {
        this.isCircular = isCircular;
        invalidate();
    }
}

在活动中,您可以实现SurfaceHolder.Callback并以重写方法设置MediaPlayer的显示。

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener, SurfaceHolder.Callback {
    private final static String TAG = "MainActivity";

    @BindView(R.id.activity_main_video_surface_view)
    protected VideoSurfaceView videoView;


    private Handler handler;
    private boolean inCircleMode;

    private final static int CIRCULAR_INTERVAL = 5000;
    private final static int MINIMUM_CARD_HEIGHT = 300;
    private final static int MAXIMUM_CARD_HEIGHT = 500;

    //Parameters for video view.
    private int cropCenterX;
    private int cropCenterY;
    private int cropRadius;
    private int croppedLayoutWidth;
    private int croppedLayoutHeight;
    private int fullLayoutWidth;
    private int fullLayoutHeight;

    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initParameters();
    }

    /**
     * Initialise the parameters used.
     */
    private void initParameters() {

        SurfaceHolder holder = videoView.getHolder();
        holder.addCallback(this);

        player = MediaPlayer.create(this, R.raw.bird_s);
        player.setOnCompletionListener(this);

        //Setting the video with proper aspect ratio.
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int height = displayMetrics.heightPixels;
        int width = displayMetrics.widthPixels;
        int dimenFull[] = Utility.getVideoDimensions(player, width, height);
        fullLayoutWidth = dimenFull[0];
        fullLayoutHeight = dimenFull[1];

        setVideoLayout();
    }

    //Runnable for switching the views from circular video to full screen video.
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            inCircleMode = !inCircleMode;

            setVideoLayout();
            handler.postDelayed(runnable, CIRCULAR_INTERVAL);
        }
    };

    /**
     * Calculates the dimensions required for cropped video view.
     */
    private void calculateCroppedParams() {
        int dimen[] = Utility.getVideoDimensions(player, 100, 100); 
        croppedLayoutWidth = dimen[0];
        croppedLayoutHeight = dimen[1];

        cropRadius = croppedLayoutWidth / 2;
        cropCenterX = cropRadius;
        cropCenterY = cropRadius;
    }

    /**
     * Change the layout dimensions for video view.
     */
    private void setVideoLayout() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) videoView.getLayoutParams();

        //Changing the margin, width & height of videoView.
        layoutParams.setMargins(0, inCircleMode ? cardView.getVideoMargin() : 0, 0, 0);
        layoutParams.width = inCircleMode ? croppedLayoutWidth : fullLayoutWidth;
        layoutParams.height = inCircleMode ? croppedLayoutHeight : fullLayoutHeight;
        layoutParams.addRule(inCircleMode ? RelativeLayout.CENTER_HORIZONTAL : RelativeLayout.CENTER_IN_PARENT);

        videoView.cropCircle(cropCenterX, cropCenterY, cropRadius);
        videoView.setCircular(inCircleMode);
        videoView.setLayoutParams(layoutParams);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {

    }

    @Override
    public void onLayout() {
        Log.i(TAG, "onLayout: starting runnable");
        calculateCroppedParams();
        player.start();
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        player.setDisplay(surfaceHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }

    @Override
    protected void onStop() {
        Log.i(TAG, "onStop");
        super.onStop();
     }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null && player.isPlaying())
            player.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null && !player.isPlaying())
            player.start();
    }
}

有关如何以不同形状裁剪视频的更多详细信息,请查看此博客文章VideoInShapes

希望这会有所帮助。

快乐编码