如何实现TextureView或者SurfaceView 预览框为圆角

项目中,视频预览界面框为圆角;但发现是使用的Renderer渲染的方法进行的,的确可以有圆角效果。
但有个问题是:我的视频源与要显示视频的预览框的长宽比率不同,预览框需要满屏看到视频源,如果不做处理,视频源只能通过压缩来显示。造成了视频的变形,如果不拉伸处理,那么就进行裁剪处理,需要对视频进行Matrix操作,如移动,伸缩处理。进行这些处理后,渲染的圆角效果就不再出现了。所以通过Renderer来进行圆角效果,不但复杂而且并不可靠。
通过网上的搜索可以找到Android5.0后,一个类ViewOutlineProvider;基于View自身的setClipToOutline(boolean clipToOutline)和setOutlineProvider(ViewOutlineProvider provider)方法实现了该需求。
    public class TextureVideoViewOutlineProvider extends 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);
        }
    }

    mView.setOutlineProvider(new TextureVideoViewOutlineProvider(radius));
    mView.setClipToOutline(true);

任何View都可通过此方法达到圆角效果。
  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,我会给您讲解使用 SurfaceTexture 和 SurfaceView 实现相机预览的方法。 首先,在布局文件中添加一个 SurfaceView: ```xml <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后在 Activity 或 Fragment 中获取 SurfaceView,并设置相机预览: ```java // 获取 SurfaceView SurfaceView surfaceView = findViewById(R.id.surfaceView); // 获取相机实例 Camera camera = Camera.open(); try { // 设置相机预览显示的 Surface camera.setPreviewDisplay(surfaceView.getHolder()); // 开始预览 camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } ``` 接下来,我们来使用 SurfaceTexture 来实现相机预览。 首先,在布局文件中添加一个 TextureView: ```xml <TextureView android:id="@+id/textureView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后在 Activity 或 Fragment 中获取 TextureView,并设置相机预览: ```java // 获取 TextureView TextureView textureView = findViewById(R.id.textureView); // 获取相机实例 Camera camera = Camera.open(); try { // 创建 SurfaceTexture SurfaceTexture surfaceTexture = new SurfaceTexture(0); // 设置相机预览显示的 Surface camera.setPreviewTexture(surfaceTexture); // 开始预览 camera.startPreview(); } catch (IOException e) { e.printStackTrace(); } ``` 使用 SurfaceTexture 来实现相机预览的好处在于,可以通过 TextureView 的 setSurfaceTextureListener() 方法来监听 SurfaceTexture 的状态,例如当 SurfaceTexture 准备好后,可以获取它的宽高等信息,从而进行一些额外的操作。 希望这些代码可以帮助到您。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值