安卓 SurfaceView 画图

SurfaceView和View的区别:

SurfaceView是View的子类,两者的区别在于SurfaceView是在单独的线程中绘制画面,而后者是在主UI线程中更新。

对于频繁更新UI的操作,如游戏中移动的人物、摄像头实时预览等,前者就比较适合,否则会影响主UI的交互。

SurfaceView中封装了一个用于绘图的Surface,而非Canvas,它可以在单独的线程中更新UI。但是和View相比,

它会增加内存的消耗。

SurfaceView的使用方法:

集成SurfaceView并实现SurfaceHolder.callBack,需要重写的方法:

surfaceCreated

//一般在这里启动绘图的线程

surfaceChanged,

surfaceDestroyed

//结束时调用,用于结束线程等

后续流程:
创建SurfaceHolder对象holder,用于添加回调函数holder.addCallback(this),启动线程函数,在线程函数中绘图(获取Canvas,用Canvas画图;或者对摄像头进行操作)

好的,下面是一个画图板app的SurfaceView的xml文件的示例代码: ``` <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <SurfaceView android:id="@+id/drawing_surface_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/colorPrimary" android:gravity="center_vertical" android:paddingStart="16dp" android:paddingEnd="16dp"> <ImageButton android:id="@+id/btn_undo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_undo" android:contentDescription="@string/undo" /> <ImageButton android:id="@+id/btn_redo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_redo" android:contentDescription="@string/redo" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageButton android:id="@+id/btn_color_palette" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_color" android:contentDescription="@string/color_palette" /> <ImageButton android:id="@+id/btn_eraser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_eraser" android:contentDescription="@string/eraser" /> <ImageButton android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_save" android:contentDescription="@string/save" /> <ImageButton android:id="@+id/btn_share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/ic_share" android:contentDescription="@string/share" /> </LinearLayout> </FrameLayout> ``` 这个xml文件包含一个SurfaceView和一个工具栏LinearLayout。SurfaceView用于绘制图形,工具栏LinearLayout包含撤销、恢复、颜色选择、橡皮擦、保存和分享等功能按钮。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值