为SurfaceView设置效果(圆角、渐变蒙层)

设置SurfaceView圆角

 /**
     * 设置Surfaceviewl圆角
     */
    private void setSurfaceviewCorner(final float radius) {
        mSurfaceView.setOutlineProvider(new ViewOutlineProvider() {
            @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, radius);
            }
        });
        mSurfaceView.setClipToOutline(true);
    }

为SurfaceView设置渐变蒙层,设置foreground属性

    <com.example.myapplication.CustomSurfaceView
        android:id="@+id/sv_test"
        android:layout_width="500dp"
        android:layout_height="500dp"
        android:foreground="@drawable/test_gradient" />

test_gradient.xml为上下往中间的渐变效果代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:startColor="#60000000"
        android:centerColor="#20000000"
        android:endColor="#60000000"
        android:angle="90" />

</shape>```
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容