Recently, my project needed a preview of the SurfaceView with rounded corners, so I searched the Internet and found a class called ViewOutLineProvider that can achieve this effect. I’ve never used it before, so I’ll record it here.
This class is available on Android5.0 or above (i.e. API>=21) : 1. Override getOutLine method 3. Set setOutlineProvider() 3. Set the View's setClipToOutLine(True) to start clippingCopy the code

Example code:

public class SurfaceViewOutlineProvider extends ViewOutlineProvider { private float mRadius; public SurfaceViewOutlineProvider(float radius) { this.mRadius = radius; } @Override public void getOutline(View view, Outline outline) { Rect rect = new Rect(); view.getGlobalVisibleRect(rect); int leftMargin = 0; int topMargin = 0; Rect selfRect = new Rect(leftMargin, topMargin, rect.right - rect.left - leftMargin, rect.bottom - rect.top - topMargin); SetRect () rectangle setOval() circle outline. SetRoundRect (selfRect, mRadius); }}Copy the code
Then set the View you want to crop:
  1.setOutLineProvider(new SurfaceViewOutlineProvider(radius))
  2.setClipOutLine(true)
Copy the code

Basically any VIew can be set this way