Android SurfaceView画板操作
程序员文章站
2023-11-06 17:36:46
本文实例为大家分享了android surfaceview画板操作的具体代码,供大家参考,具体内容如下画板——画路径package com.example.review.view;import and...
本文实例为大家分享了android surfaceview画板操作的具体代码,供大家参考,具体内容如下
画板——画路径
package com.example.review.view; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.porterduff; import android.util.attributeset; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; /** * 画板画路径 */ public class huabanview extends surfaceview implements surfaceholder.callback { private surfaceholder surfaceholder; private path path = new path(); public huabanview(context context) { super(context); } public huabanview(context context, attributeset attrs) { super(context, attrs); surfaceholder = getholder(); surfaceholder.addcallback(this);//获得surfaceview的生命周期 } public huabanview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } public huabanview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); } @override public void surfacecreated(surfaceholder holder) { new huabanthread().start(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { } @override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); int action = event.getaction(); if (action == motionevent.action_down) {//按下 path.moveto(x, y); } else if (action == motionevent.action_move) {//移动 path.lineto(x, y); } return true; } class huabanthread extends thread { @override public void run() { super.run(); //todo:画笔 paint paint = new paint(); paint.setcolor(color.black); paint.setstrokewidth(20); paint.setstyle(paint.style.stroke); paint.setantialias(true); //todo:画布 while (true) { canvas canvas = surfaceholder.lockcanvas(); //避免空指针 if (canvas == null){ return; } canvas.drawcolor(color.white, porterduff.mode.clear); canvas.drawcolor(color.white); canvas.drawpath(path,paint); surfaceholder.unlockcanvasandpost(canvas); } } } public void close(){ path.reset(); } }
画板——画动态直线
package com.example.review.view; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.graphics.porterduff; import android.util.attributeset; import android.view.surfaceholder; import android.view.surfaceview; /** * 画板画路径 * 画动态直线 */ public class lineview extends surfaceview implements surfaceholder.callback { private surfaceholder surfaceholder; private path path = new path(); private int x = 0; public lineview(context context) { super(context); } public lineview(context context, attributeset attrs) { super(context, attrs); surfaceholder = getholder(); surfaceholder.addcallback(this);//获得surfaceview的生命周期 } public lineview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } public lineview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); } @override public void surfacecreated(surfaceholder holder) { new huabanthread().start(); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { } class huabanthread extends thread { @override public void run() { super.run(); //todo:画笔 paint paint = new paint(); paint.setcolor(color.black); paint.setstrokewidth(20); paint.setstyle(paint.style.stroke); paint.setantialias(true); //todo:画布 while (true) { canvas canvas = surfaceholder.lockcanvas(); //避免空指针 if (canvas == null){ return; } canvas.drawcolor(color.white, porterduff.mode.clear); canvas.drawcolor(color.white); canvas.drawline(0,100,x++,100,paint); surfaceholder.unlockcanvasandpost(canvas); } } } public void close(){ path.reset(); } }
基本图形
//圆 canvas.drawoval(50,100,150,200,paint); //半圆 canvas.drawarc(500,500,700,700,20,180,true,paint); //矩形 canvas.drawrect(100,300,250,400,paint); //三角形 canvas.drawline(100,450,0,600,paint); canvas.drawline(0,600,400,600,paint); canvas.drawline(100,450,400,600,paint); //梯形 canvas.drawline(100,700,200,700,paint); canvas.drawline(100,700,0,900,paint); canvas.drawline(0,900,400,900,paint); canvas.drawline(200,700,400,900,paint); //文字 canvas.drawtext("截图",100,1000,paint);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Python 统计字数的思路详解
下一篇: 前女友不按套路出牌啊
推荐阅读
-
Android gradle配置抽取合并的操作步骤
-
Android操作存放在assets文件夹下SQLite数据库的方法
-
android 限制某个操作每天只能操作指定的次数(示例代码详解)
-
Android SurfaceView画板操作
-
android surfaceView实现播放视频功能
-
Android 文件读写操作方法总结
-
Android中实现地址栏输入网址能浏览该地址网页源码并操作访问网络
-
Android studio 自动换行和取消自动换行操作
-
修改Android Studio 的 Logcat 缓冲区大小操作
-
android使用surfaceview+MediaPlayer播放视频