使用ViewOutlineProvider來實現(xiàn),ViewOutlineProvider專門用于陰影投射和剪切。
相關方法:
1.?setOutlineProvider()
生成定義其投射陰影形狀的輪廓,并且啟用輪廓裁剪。
2.?setClipToOutline()
設置是否應使用視圖輪廓來剪切視圖的內容
定義一個圓形的ViewOutlineProvider:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
? ? public static class CircleViewOutlineProvider extends ViewOutlineProvider {
? ? ? ? public CircleViewOutlineProvider() {
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void getOutline(View view, Outline outline) {
????????????//864,648? 橫屏情況下,寬>高
? ? ? ? ? ? Log.d("Circle===","width:"+view.getWidth()+"==height:"+view.getHeight());
? ? ? ? ? ? //裁剪成一個圓形
? ? ? ? ? ? int left0 = (view.getWidth() - view.getHeight()) / 2;
? ? ? ? ? ? int top0 = 0;
? ? ? ? ? ? int right0 = left0 + view.getHeight() ;
? ? ? ? ? ? int bottom0 =? view.getHeight() ;
? ? ? ? ? ? outline.setOval(left0, top0, right0, bottom0);
? ? ? ? }
? ? }
設置到SurfaceView上:
surfaceView.setOutlineProvider(new CircleViewOutlineProvider(dp1));
surfaceView.setClipToOutline(true);
這樣就可以實現(xiàn)圓形預覽形狀的SurfaceView了。
延伸,設置圓角的方式:
public class TextureVideoViewOutlineProviderextends ViewOutlineProvider {
????private float mRadius;
? ? public TextureVideoViewOutlineProvider(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);
? ? ? ? outline.setRoundRect(selfRect, mRadius);
? ? }
}
然后設置:
surfaceView.setOutlineProvider(new TextureVideoViewOutlineProvider(10));
surfaceView.setClipToOutline(true);
就可以實現(xiàn)圓角的效果了。