How to make Circular Mask and Clip GLSurfaceView?

2020-04-05 09:14发布

问题:

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

  • It's showing Circle shape when I overlay GLSurfaceView over ImageView

GLSurfaceView over ImageView

  • It's showing Square shape when I overlay GLSurfaceView over VideoView.

GLSurfaceView over VideoView

I have tried below code for making circle GLSurfaceView

public class SampleGLView extends GLSurfaceView implements View.OnTouchListener {

    private Path clippingPath;

    public SampleGLView(Context context) {
        this(context, null);
    }

    public SampleGLView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnTouchListener(this);
    }

    private TouchListener touchListener;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int actionMasked = event.getActionMasked();
        if (actionMasked != MotionEvent.ACTION_DOWN) {
            return false;
        }

        if (touchListener != null) {
            touchListener.onTouch(event, v.getWidth(), v.getHeight());
        }
        return false;
    }

    public interface TouchListener {
        void onTouch(MotionEvent event, int width, int height);
    }

    public void setTouchListener(TouchListener touchListener) {
        this.touchListener = touchListener;
    }

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

Question:

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)

Is any one have idea how to solve please comment. Thanks in advance.