Android自定义View画圆功能
程序员文章站
2023-12-18 09:01:28
本文实例为大家分享了android自定义view画圆的具体代码,供大家参考,具体内容如下
引入布局
本文实例为大家分享了android自定义view画圆的具体代码,供大家参考,具体内容如下
引入布局
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.bwie.test.xuejian1508a20170928.mainactivity"> <com.bwie.test.xuejian1508a20170928.viview android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
自定义view的java类,继承view
public class viview extends view{ paint paint; context context; //构造方法 public viview(context context) { super(context); } public viview(context context, attributeset attrs) { super(context, attrs); } public viview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } public viview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); this.context=context; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); } @override protected void onlayout(boolean changed, int left, int top, int right, int bottom) { super.onlayout(changed, left, top, right, bottom); } /*绘图*/ @override protected void ondraw(canvas canvas) { super.ondraw(canvas); //得到屏幕宽高 int width = getwidth(); int radius = width - 450/2; int height = getheight(); // 创建画笔 paint paint1 = new paint(); paint paint2 = new paint(); paint paint3= new paint(); // 消除锯齿 paint1.setantialias(true); paint2.setantialias(true); paint3.setantialias(true); //画笔颜色 paint1.setcolor(color.red); paint2.setcolor(color.white); paint3.setcolor(color.blue); // 画圆。确定位置 // canvas.drawrect(100,100,width/2,height/2,paint1); // canvas.drawcircle(100,100,100,paint1); // canvas.drawcircle(250,250,200,paint2); // canvas.drawcircle(500,500,300,paint3); //设置圆环形状和大小 rectf oval = new rectf(width-radius,width-radius,width+radius,width+radius); paint1.setstrokewidth(450); canvas.drawarc(oval,-90,90,false,paint1); canvas.drawcircle(width/2,height/2,450,paint1); canvas.drawcircle(width/2,height/2,300,paint2); canvas.drawcircle(width/2,height/2,200,paint3); } @override public boolean ontouchevent(motionevent event) { return super.ontouchevent(event); } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。