Android自定义SurfaceView——实现画板功能
来源:互联网 发布:新型网络诈骗手段莆田 编辑:程序博客网 时间:2024/06/05 09:46
接触了这么久的View,总不能一直停留在View里,现在开始呢,就要学习一个新的知识点:SurfaceView,实际上SurfaceView与View的原理都差不多,只是效率和渲染方式上,SurfaceView要优于View,这也是我们写这个的原因。今天就看看这个SurfaceView,好了,下面就是今天要说的效果。
界面很简单,就是一个按钮以及一个画板,先看看界面的代码吧
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.xinxindemo.MainActivity" > <com.example.xinxindemo.view.SecondSurfaceView android:id="@+id/surfaceview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="20" /> <LinearLayout android:layout_width="match_parent" android:layout_height="55dip" android:orientation="horizontal" android:padding="5dip" > <Button android:onClick="onClick" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="clean" /></LinearLayout></LinearLayout>
对吧,界面不是很复杂,下面再看看这个SecondSurfaceView是怎么实现的;
/** * 2016年7月26日17:20:13 * @author 小瓶盖 blog http://blog.csdn.net/qq_25193681/article/details/52005375 * */public class SecondSurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{ /** * 是否处于绘制状态 */ private boolean mIsDrawing; /** * 帮助类 */ private SurfaceHolder mHolder; /** * 画布 */ private Canvas mCanvas; /** * 路径 */ private Path mPath; /** * 画笔 */ private Paint mPaint; public SecondSurfaceView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } public SecondSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public SecondSurfaceView(Context context) { super(context); initView(); } @Override public boolean onTouchEvent(MotionEvent event) { int x=(int) event.getX(); int y=(int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPath.moveTo(x, y); break; case MotionEvent.ACTION_MOVE: mPath.lineTo(x, y); break; case MotionEvent.ACTION_UP: break; default: break; } return true; } private void initView() { mHolder=getHolder(); mHolder.addCallback(this); setFocusable(true); setFocusableInTouchMode(true); this.setKeepScreenOn(true); mPath=new Path(); mPaint=new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.BLACK); mPaint.setStyle(Style.STROKE); mPaint.setStrokeWidth(15); } @Override public void run() { long start =System.currentTimeMillis(); while(mIsDrawing){ draw(); } long end =System.currentTimeMillis(); if (end-start<100) { try { Thread.sleep(100-(end-start)); } catch (Exception e) { e.printStackTrace(); } } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder arg0) { mIsDrawing=true; new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { mIsDrawing=false; } private void draw(){ try { mCanvas=mHolder.lockCanvas(); mCanvas.drawColor(Color.WHITE); mCanvas.drawPath(mPath, mPaint); } catch (Exception e) { e.printStackTrace(); }finally{ if (mCanvas!=null) { mHolder.unlockCanvasAndPost(mCanvas); } } } /** * 清除内容 */ public void clean(){ initView(); }}
然后就是MainActivity.java
/** * 2016年7月26日17:20:13 * @author 小瓶盖 blog http://blog.csdn.net/qq_25193681/article/details/52005375 * */public class MainActivity extends Activity{ SecondSurfaceView surfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); surfaceView=(SecondSurfaceView)findViewById(R.id.surfaceview); } public void onClick(View v){ surfaceView.clean(); }}
今天项目要上线,所以博客先写到这里,后期有空的话,再补充了
源码
5 2
- Android自定义SurfaceView——实现画板功能
- android自定义画板,实现简单绘图功能
- android实现画板功能
- android基础画板的实现(surfaceView)
- 自定义view实现涂鸦(画板)功能
- 自定义view实现涂鸦(画板)功能(二)
- Android简易基于SurfaceView画板
- Android手摸手实现一个画板功能(一)——View的拖拽
- canvas实现画板功能
- surfaceview如何实现可以滚动的画板
- android 自定义控件 -- 画板
- Android实现拍照功能,使用SurfaceView+camera
- Android实现画板功能的多种实现方式
- Android Camera+SurfaceView实现自定义拍照
- android自定义SurfaceView实现跑男动画
- Android自定义控件--波浪球SurfaceView实现
- Android 自定义SurfaceView实现加载GIF动画
- Android视频播放02—SurfaceView实现
- 图片间有空隙的解决方法
- 在 Laravel 5 中集成 Pjax 实现无刷新加载页面的扩展包 —— Laravel Pjax
- ViewPager实现图片自动轮播无限循环(完美流畅版)
- 合并(序列)流
- 糍粑大叔的独游之旅-u3d中2D轮廓的生成(中)
- Android自定义SurfaceView——实现画板功能
- Spark源码解读(3)——从集群启动到Job提交
- Emag eht htiw Em Pleh
- C#的枚举转换、应用例子
- Python机器视觉编程常用数据结构与示例
- SpringBoot的常用系统变量
- Less使用手册
- Java 加密解密技术学习之DES(对称加密学习)
- C# WinCE中DataGrid列设置(列宽 列标题等)