Mask and Clip GLSurfaceView

2019-05-30 13:22发布

问题:

I work with an SDK that provides a rectangular glsurfaceview through a callback.

I want to be able to render this view in a circular layout. (i.e.) I would like to display the view on a circular view

I have tried using masking layout such as using a maskable layout https://github.com/christophesmet/android_maskable_layout (works great for images, not so much for videos)

How do I go about clipping and rendering this as a circle?

(The background is constantly changing, so i cant overlay a transparent view on top of this video view. The aim is to have a rectangular glsurface view on top of which there is a circular glsurface view)

**UI Objective : **

回答1:

I have pretty much the same requirement with a project right now, which involves masking a GLSurfaceView provided by PexKit. The solution that works for me is making a subclass of FrameLayout (or any other ViewGroup) and placing the GLSurfaceView inside it.

Then in the FrameLayout subclass use canvas.clipPath:

private Path clippingPath;

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != oldw || h != oldh) {
        int radius = Math.min(w, h)/2;
        clippingPath = new Path();
        clippingPath.addCircle(w/2, h/2, radius, Path.Direction.CW);
    }
}

@Override
protected void dispatchDraw(Canvas canvas) {
    int count = canvas.save();
    canvas.clipPath(clippingPath);
    super.dispatchDraw(canvas);
    canvas.restoreToCount(count);
}