SurfaceView实现圆角矩形预览

android中,文本、按钮实现圆角样式只需要在其背景中定义corners属性设置radius即可。
在SurfaceView中同样可以实现此种效果,以丰富预览样式。
比如用SurfaceView要实现圆角矩形预览的效果,自定义View继承于SurfaceView,然后重写其draw方法,这点不同于ImageView实现圆角效果,ImageView要重写其onDraw方法。具体代码,如下。
	@Override
	public void draw(Canvas canvas) {
		
	    Path path = new Path();  
            //用矩形表示SurfaceView宽高
	    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
            //15.0f即是圆角半径
           path.addRoundRect(rect, 15.0f, 15.0f, Path.Direction.CCW);
	    //裁剪画布,并设置其填充方式  
	    canvas.clipPath(path, Region.Op.REPLACE);  
	    super.draw(canvas); 
	}
同理,要实现其他样式的效果,只需要调用Canvas类中的其他画图方法就好。
不过,要想实现预览,还需要在布局中加入背景,一般设置为透明色就好了。
最后,上图展示实现效果。
附:ImageView实现圆角代码
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,我会给您讲解使用 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 准备好后,可以获取它的宽高等信息,从而进行一些额外的操作。 希望这些代码可以帮助到您。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值