android在点击画圆,Android自定义View画圆功能

本文实例为大家分享了android自定义view画圆的具体代码,供大家参考,具体内容如下

引入布局

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">

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

自定义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);

}

}

效果图:

f1ff69d04f761e788a7f23686e31ead9.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值