androidcanvas

目录:

1、使用SurfaceView画一张图2、自定义view绘制一张图

我开始认真研究Android多媒体的开发,拓宽多媒体的技术层面,在Android中画一张图有三种方式:SurfaceView、ImageView和Custom View。

1、使用SurfaceView画一张图

包com . example . tongjiangsong . surface view demo;导入Android . content . context;导入Android . graphics . canvas;导入Android . graphics . color;导入Android . graphics . paint;导入Android . graphics . path;导入Android . util . attributeset;导入Android . view . motion event;导入Android . view . surface holder;导入Android . view . surface view;公共类CommonSurfaceView扩展SurfaceView实现SurfaceHolder。回调,Runnable { private surface holder m holder;私人画布mCanvas私有路径mPath私漆mPaint//线程私有线程mThread进行绘制;//线程状态标志(线程控制开关)私有布尔isRunning私有int drawXprivate int drawYpublic common surface view(Context Context){ this(Context,null);} public common surface view(Context Context,AttributeSet attrs){ this(Context,attrs,0);} public common surface view(Context Context,AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr);//初始化m holder = get holder();mholder . add callback(this);//管理生命周期mPath = new Path();//初始化画笔MP aint = new Paint();MP int . set style(Paint。Style .笔画);mpaint . setstrokewidth(6);MP aint . setantialias(true);mPaint.setColor(Color。红色);setFocusable(true);setFocusableInTouchMode(true);this . setkeepscreenon(true);} @ Override public void surface created(surface holder holder){ is running = true;mThread =新线程(this);mthread . start();//Start thread } @ override public void surface已更改(surface holder,int format,int width,int height){ } @ override public void surface已销毁(surface holder){ is running = false;}/* * *注意这是在子线程*/@ override public void run(){ while(正在运行){ draw x++;drawY =(int)(100 * Math . sin(drawX * 2 * Math。PI/180)+400);mPath.lineTo(drawX,drawY);draw();}}/* * *注意这是在子线程中绘制的,表面支持子线程更新ui,所以*/private void draw(){ canvas canvas = null;//锁定画布,防止线程安全,防止内存区域被其他线程共享。canvas = mholder . lock canvas();if (null!= canvas) {//清除屏幕或者设置背景canvas.drawColor(Color。白色);canvas.drawPath(mPath,mPaint);//提交显示视图并解锁它,以防止它占用此内存mholder。长时间解锁canvasandPost(画布);}}}输出结果:

androidcanvas

2、使用自定义view绘制一张图

包com . example . tongjiangsong . view demo;导入Android . content . context;导入Android . graphics . canvas;导入Android . graphics . color;导入Android . graphics . paint;导入Android . util . attributeset;导入Android . view . motion event;导入Android . view . view;导入Ja . util . random;公共类BallView扩展了View { private Context mContext私有属性集mAttrs私漆油漆;//定义画笔公共球视图(context上下文){ super(context);this.mContext = context//初始化画笔init paint();} public BallView(Context context,AttributeSet attrs){ super(Context,attrs);this.mContext = contextthis.mAttrs = attrsinit paint();} public BallView(Context context,AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr);this.mContext = contextthis.mAttrs = attrsinit paint();}私有void init Paint(){ Paint = new Paint();}//重写onDraw方法实现画图操作@ override protected void onDraw(canvas canvas){ super。ondraw(帆布);paint . setantialias(false);//将画笔设置为无锯齿paint.setColor(Color。黑色);//设置画笔颜色canvas.drawColor(Color。白色);//白色背景paint . setstrokewidth((float)3.0);//线宽paint . set style(paint . style . stroke);/空心形效果画布。画圆(50,50,10,画);//画一个圆形canvas.drawcircle (100,100,20,paint);//绘制圆形画布。画圆(150,150,30,画);//画一个圆形canvas.drawcircle (200,200,40,paint);//画一个圆形canvas.drawcircle (250,250,50,paint);//画一个圆形canvas.drawcircle (300,300,60,paint);//画一个圆形canvas.drawcircle (350,350,70,paint);//画一个圆}}输出结果:

我的CSDN博客地址:https://blog.csdn.net/tong5956.

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

发表回复

登录后才能评论