canvas教程

Android中的SurfaceView详解(2)

字号+ 作者:H5之家 来源:H5之家 2017-02-28 17:01 我要评论( )

1 . 绘制正弦曲线 大体的框架都是上面给的那个代码模板,区别只在于初始化画笔,和具体的绘图逻辑,所以这里不再赘述,直接上代码: public class SurfaceViewSinFun extends SurfaceView implements SurfaceHolder



1 . 绘制正弦曲线

大体的框架都是上面给的那个代码模板,区别只在于初始化画笔,和具体的绘图逻辑,所以这里不再赘述,直接上代码:

public class SurfaceViewSinFun extends SurfaceView implements SurfaceHolder.Callback, Runnable { private SurfaceHolder mSurfaceHolder; //绘图的Canvas private Canvas mCanvas; //子线程标志位 private boolean mIsDrawing; private int x = 0, y = 0; private Paint mPaint; private Path mPath; public SurfaceViewSinFun(Context context) { this(context, null); } public SurfaceViewSinFun(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SurfaceViewSinFun(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(5); mPath = new Path(); //路径起始点(0, 100) mPath.moveTo(0, 100); initView(); } @Override public void surfaceCreated(SurfaceHolder holder) { mIsDrawing = true; new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { mIsDrawing = false; } @Override public void run() { while (mIsDrawing){ drawSomething(); x += 1; y = (int)(100 * Math.sin(2 * x * Math.PI / 180) + 400); //加入新的坐标点 mPath.lineTo(x, y); } } private void drawSomething() { try { //获得canvas对象 mCanvas = mSurfaceHolder.lockCanvas(); //绘制背景 mCanvas.drawColor(Color.WHITE); //绘制路径 mCanvas.drawPath(mPath, mPaint); }catch (Exception e){ }finally { if (mCanvas != null){ //释放canvas对象并提交画布 mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } } /** * 初始化View */ private void initView(){ mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); setFocusable(true); setKeepScreenOn(true); setFocusableInTouchMode(true); }}

2 . 手写板(随手指绘制轨迹)

这个主要是涉及到触摸事件,在手指按下时将Path的起始点移动到按下的坐标点,手指移动时将移动的坐标点加入Path中,其他的代码是相同的。代码如下:

public class SurfaceViewHandWriting extends SurfaceView implements SurfaceHolder.Callback, Runnable { private SurfaceHolder mSurfaceHolder; //绘图的Canvas private Canvas mCanvas; //子线程标志位 private boolean mIsDrawing; //画笔 private Paint mPaint; //路径 private Path mPath; private static final String TAG = "pyh"; public SurfaceViewHandWriting(Context context) { this(context, null); } public SurfaceViewHandWriting(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SurfaceViewHandWriting(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(5); mPaint.setAntiAlias(true); mPath = new Path(); mPath.moveTo(0, 100); initView(); } @Override public void surfaceCreated(SurfaceHolder holder) { mIsDrawing = true; new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { mIsDrawing = false; } @Override public void run() { while (mIsDrawing) { long start = System.currentTimeMillis(); drawSomething(); long end = System.currentTimeMillis(); if (end - start < 100) { try { Thread.sleep(100 - (end - start)); } catch (InterruptedException e) { e.printStackTrace(); } } } } @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; } return true; } /** * 初始化View */ private void initView(){ mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); setFocusable(true); setKeepScreenOn(true); setFocusableInTouchMode(true); } private void drawSomething() { try { //获得canvas对象 mCanvas = mSurfaceHolder.lockCanvas(); //绘制背景 mCanvas.drawColor(Color.WHITE); //绘制路径 mCanvas.drawPath(mPath, mPaint); }catch (Exception e){ }finally { if (mCanvas != null){ //释放canvas对象并提交画布 mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } }}

上面还有一个细节,在绘制的时候,我们并没有让线程一直运行,而是让它休眠一会,从而节约系统资源,一般建议判断的阈值为50-100之间即可保证用户体验同时节约系统资源。

以上就是SurfaceView的使用介绍,希望可以帮助到需要的人。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android编程实现支持拖动改变位置的图片中叠加文字功能示例

    Android编程实现支持拖动改变位置的图片中叠加文字功能示例

    2017-02-28 15:02

  • Android倾斜、描边、自定义字体的TextView

    Android倾斜、描边、自定义字体的TextView

    2017-02-26 11:03

  • 用H5中的Canvas等技术制作海报

    用H5中的Canvas等技术制作海报

    2017-02-24 14:00

  • Android中圆角矩形和圆形的多种实现方式

    Android中圆角矩形和圆形的多种实现方式

    2017-02-24 12:03

网友点评