Android之圓形預覽的SurfaceView

使用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)圓角的效果了。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容