您好,欢迎访问三七文档
Android绘图机制(二)——自定义View绘制形,圆形,三角形,扇形,椭圆,曲线,文字和图片的坐标讲解我们要想画好一些炫酷的View,首先我们得知道怎么去画一些基础的图案,比如矩形,圆形,三角形,多边形等….新建一个项目然后我们创建一个listview,每个图案一个Activity,这样看起来是不是很顺眼ListViewandroid:id=@+id/listviewandroid:layout_width=wrap_contentandroid:layout_height=wrap_content/编写ListViewprivateListViewlistview;//item上的数据源privateString[]name={矩形,圆形,三角形,扇形,椭圆,曲线,文字和图片};//listview的adapterprivateArrayAdapterStringadapter;privatevoidinitView(){//实例化listviewlistview=(ListView)findViewById(R.id.listview);//实例化数据源adapter=newArrayAdapterString(this,android.R.layout.simple_list_item_1,name);//listview设置adapterlistview.setAdapter(adapter);//listview设置点击事件listview.setOnItemClickListener(newAdapterView.OnItemClickListener(){@OverridepublicvoidonItemClick(AdapterView?parent,Viewview,intposition,longid){//判断点击了第几个if(id==0){//矩形startActivity(newIntent(MainActivity.this,RectActivity.class));}elseif(id==1){//圆形startActivity(newIntent(MainActivity.this,CircleActivity.class));}elseif(id==2){//三角形startActivity(newIntent(MainActivity.this,TrigonActivity.class));}elseif(id==3){//扇形startActivity(newIntent(MainActivity.this,SectorActivity.class));}elseif(id==4){//椭圆startActivity(newIntent(MainActivity.this,OvalActivity.class));}elseif(id==5){//曲线startActivity(newIntent(MainActivity.this,PathActivity.class));}elseif(id==6){//曲线startActivity(newIntent(MainActivity.this,TvIvActivity.class));}}});}效果是这样的后续可以添加1.矩形——RectActivity好的,上面写ListView的代码是不是很简单?拿我们新建一个RectActivity去画点了,首先我们要新建一个RectView继承Viewpackagecom.lgl.view.view;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.view.View;/***矩形*CreatedbyLGLon2016/1/7.*/publicclassRectViewextendsView{//无参publicRectView(Contextcontext){super(context);}//有参publicRectView(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//创建画笔Paintp=newPaint();//设置实心p.setStyle(Paint.Style.FILL);//设置红色p.setColor(Color.BLACK);//设置画笔的锯齿效果p.setAntiAlias(true);//绘制canvas.drawRect(50,100,300,300,p);}}截图1.圆形——CircleActivity我们还是新建一个CircleActivity去画点了,首先我们要新建一个CircleView继承Viewpackagecom.lgl.view.view;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.util.AttributeSet;importandroid.view.View;importandroid.view.WindowManager;/***圆*CreatedbyLGLon2016/1/7.*/publicclassCircleViewextendsView{intwidth;intheight;//无参publicCircleView(Contextcontext){super(context);init();}//有参publicCircleView(Contextcontext,AttributeSetattrs){super(context,attrs);init();}privatevoidinit(){//获取屏幕的宽高//Android绘图机制(一)——自定义View的基础属性和方法里面有讲WindowManagerwm=(WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);width=wm.getDefaultDisplay().getWidth();height=wm.getDefaultDisplay().getHeight();}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);Paintp=newPaint();p.setColor(Color.BLACK);//设置画笔的锯齿效果p.setAntiAlias(true);canvas.drawCircle(width/2,height/2,width/2,p);}}截图3.三角形——TrigonActivity我们还是新建一个TrigonActivity去画点了,首先我们要新建一个trigonView继承Viewpackagecom.lgl.view.view;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Path;importandroid.util.AttributeSet;importandroid.view.View;/***三角形*CreatedbyLGLon2016/1/7.*/publicclassTrigonViewextendsView{//无参publicTrigonView(Contextcontext){super(context);}//有参publicTrigonView(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);Paintp=newPaint();p.setColor(Color.BLACK);//实例化路径Pathpath=newPath();path.moveTo(80,200);//此点为多边形的起点path.lineTo(120,250);path.lineTo(80,250);path.close();//使这些点构成封闭的多边形canvas.drawPath(path,p);}}4.扇形——SectorActivitypackagecom.lgl.view.view;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.RectF;importandroid.util.AttributeSet;importandroid.view.View;/***扇形*CreatedbyLGLon2016/1/8.*/publicclassSectorViewextendsView{publicSectorView(Contextcontext){super(con);}publicSectorView(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//创建画笔Paintp=newPaint();p.setColor(Color.BLACK);RectFrectF=newRectF(60,100,200,240);canvas.drawArc(rectF,200,130,true,p);}}5.椭圆——OvalActivitypackagecom.lgl.view.view;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.RectF;importandroid.util.AttributeSet;importandroid.view.View;/***椭圆*CreatedbyLGLon2016/1/8.*/publicclassOvalViewextendsView{publicOvalView(Contextcontext){super(context);}publicOvalView(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//创建画笔Paintp=newPaint();p.setColor(Color.BLACK);RectFrectF=newRectF(60,1
本文标题:Android绘图机制(二)自定义View绘制形,圆形,三角形,扇形,椭圆,曲线,文字和图片的坐标讲
链接地址:https://www.777doc.com/doc-2897379 .html