android 圆形相机RoundSurfaceView

编程入门 行业动态 更新时间:2024-10-07 19:25:32

android <a href=https://www.elefans.com/category/jswz/34/1769013.html style=圆形相机RoundSurfaceView"/>

android 圆形相机RoundSurfaceView

最近做了圆形相机,使用surfaceView来承载。

public final class CircleSurfaceView extends SurfaceView implements SurfaceHolder.Callback{public CircleSurfaceView(Context context) {super(context);init();}public CircleSurfaceView(Context context, AttributeSet attrs) {super(context, attrs);init();}public CircleSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}private void init() {this.setFocusable(true);this.setFocusableInTouchMode(true);}@Overridepublic void draw(Canvas canvas) {Path path = new Path();// 坐标转换为实际像素int w = getWidth();int h = getHeight();int radius = Math.min(w >> 1, h >> 1);ALog.d(String.format(Locale.getDefault(),"w %d h %d radius %d", w, h, radius));path.addCircle(w >> 1, h >> 1, radius, Path.Direction.CW);canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));if (Build.VERSION.SDK_INT >= 26) {canvas.clipPath(path);} else {canvas.clipPath(path, Region.Op.REPLACE);}super.draw(canvas);}@Overridepublic void surfaceCreated(SurfaceHolder holder) {ALog.d("surface Created");setWillNotDraw(false);//do camera action.... todo....}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {ALog.d("surface Changed " + width + ", " + height);}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if(mSurfaceHolderChanged != null) mSurfaceHolderChanged.invoke(null);}
}

主要就是修改draw函数。
来源自,参考和实践的。
主要是评论下方,提点。
ImageView则修改onDraw(Canvas).

但是,重要的你会发现你的界面,摄像头还是拉伸的。

这个时候,我们需要关注下xml的画面比例即可。


<RelativeLayoutandroid:id="@+id/fr_recording_rl"android:layout_marginTop="40dp"android:layout_width="351dp"android:layout_centerHorizontal="true"android:layout_height="351dp"><!-- surfaceView camera --><CircleSurfaceViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginTop="33dp"android:layout_marginStart="0dp"android:layout_marginEnd="0dp"android:layout_marginBottom="33dp"android:layout_centerInParent="true"android:id="@+id/fr_camera_surface_view"/></RelativeLayout>

你看,我的原型外层是351dp的正方形。内部,通过marginTop,bottom,而左右不设置值。
来保证他的比例与camera的分辨率达到一个比较好的比例即可。

如果你在xml中设置的比例也是方的。而摄像头一般都是1280/720这种比例,你的画面注定就要拉伸了。

后续有空,补充:
onMesure,onDraw,draw的区别和各自影响。

更多推荐

android 圆形相机RoundSurfaceView

本文发布于:2024-02-14 05:37:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1762042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆形   相机   android   RoundSurfaceView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!