下面请先看效果图:
看上去是不很炫的样子,它的实现上也不是很复杂,重点在与onDraw()
方法的绘制。
首先是我们的attrs文件:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
接下来是我们重写View
类的自定义View
类:
public class MySelfCircleView extends View { /* * 第一圈颜色 */ int firstColor; /* * 第二圈颜色 */ int secondColor; /* * 圆的宽度 */ int circleWidth; /* * 速率 */ int speed; /* * 画笔 */ Paint mPaint; /* * 进度 */ int mProgress; /* * 是否切换标志 */ boolean isNext; public MySelfCircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0); int n = typedArray.getIndexCount(); for(int i=0; i
最后是我们的布局文件:
总结
好了,到这里我们的效果就算大工告成,感兴趣的朋友可以写写看,个人感觉自定义View需要大量的练习,才能为我所用。希望本文对大家开发Android能有所帮助。