Android自定义View画圆功能
作者:xcjean 时间:2023-05-18 10:47:24
本文实例为大家分享了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);
}
}
效果图:
来源:http://blog.csdn.net/xcjean/article/details/78137200
标签:Android,View,画圆
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用stream实现一个简单的http下载器
2023-12-14 09:36:36
java实现文件的断点续传
2023-11-23 09:11:00
![](https://img.aspxhome.com/file/2023/4/59544_0s.jpg)
Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例
2022-08-19 20:58:06
![](https://img.aspxhome.com/file/2023/3/76323_0s.png)
C#语音识别用法实例
2021-09-11 01:16:47
C语言数据结构之二叉树详解
2021-08-18 20:56:41
![](https://img.aspxhome.com/file/2023/3/120783_0s.png)
spring boot中配置hikari连接池属性方式
2022-11-13 06:06:44
Android实现页面短信验证功能
2022-02-13 01:36:56
![](https://img.aspxhome.com/file/2023/6/139396_0s.jpg)
java selenium教程之selenium详细介绍
2023-07-30 13:53:52
Spring与Spring boot的区别介绍
2022-02-12 08:55:07
Spring加载properties文件的两种方式实例详解
2021-11-20 23:36:28
![](https://img.aspxhome.com/file/2023/7/70987_0s.png)
Android 圆角边框的实现方式汇总
2023-03-20 04:27:46
![](https://img.aspxhome.com/file/2023/5/137905_0s.png)
Spring的@Validation和javax包下的@Valid区别以及自定义校验注解
2021-06-20 04:06:35
SpringCloud升级2020.0.x版之OpenFeign简介与使用实现思路
2021-10-02 08:30:56
![](https://img.aspxhome.com/file/2023/2/88592_0s.jpg)
Unity打开淘宝app并跳转到商品页面功能的实现方法
2023-06-17 01:05:18
![](https://img.aspxhome.com/file/2023/5/56135_0s.png)
Android 实例开发基于ArcSoft实现人脸识别
2022-01-01 16:34:06
![](https://img.aspxhome.com/file/2023/1/137321_0s.jpg)
android中NFC读写功能的实现方法
2023-10-19 12:30:07
如何基于java语言实现八皇后问题
2022-08-22 09:05:25
Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
2023-12-01 22:53:40
C#使用SharpZipLib压缩解压文件
2021-10-27 18:56:21
在Kotlin开发中如何使用集合详解
2022-03-06 02:22:30