项目中的自定义控件比较多,今天应该是最后一个了,看下UI效果git
UI分析github
- 内侧一个白色的虚线弧
- 外侧有两条弧,一个是灰色的实线弧,一个是白色的虚线弧,实线弧尾部有一个小圆点
- 中间是文字,大小不一致,颜色是白色
- 加载的时候须要一个加载动画,实线椭圆进度条跟可用额度数字须要同时从小到达变化
下面根据UI的分析,来分享一下实现的过程canvas
代码bash
//定义属性
<declare-styleable name="CircleIndicatorView">
<attr name="largeSize" format="dimension"/>
<attr name="smallSize" format="dimension"/>
<attr name="grayColor" format="color"/>
<attr name="whiteColor" format="color"/>
<attr name="lineSpace" format="dimension"/>
</declare-styleable>
//获取属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleIndicatorView);
mLargeSize = (int) ta.getDimension(R.styleable.CircleIndicatorView_largeSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, context.getResources().getDisplayMetrics()));
mSmallSize = (int) ta.getDimension(R.styleable.CircleIndicatorView_smallSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13, context.getResources().getDisplayMetrics()));
mGrayColor = ta.getColor(R.styleable.CircleIndicatorView_grayColor, Color.GRAY);
mWhiteColor = ta.getColor(R.styleable.CircleIndicatorView_whiteColor, Color.WHITE);
ta.recycle();复制代码
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureWidth(int widthMeasureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
switch (specMode) {
case MeasureSpec.EXACTLY:
result = specSize;
break;
case MeasureSpec.UNSPECIFIED:
case MeasureSpec.AT_MOST:
break;
}
return result;
}复制代码
这里其实不须要考虑MeasureSpec.AT_MOST,由于这个圆的半径是通常是固定的,因此没有处理MeasureSpec.AT_MOST,若是需求须要处理的话其实也很简单,跟自定义View之IndexView进度条(一)中的同样,把相应的宽高进行累加便可,不是分析的重点,因此一笔带过。app
//不加的话在高版本上虚线不显示
setLayerType(View.LAYER_TYPE_SOFTWARE,dashPaint);
//设置虚线间隔
PathEffect effects = new DashPathEffect(new float[]{20, 6}, 0);
dashPaint.setPathEffect(effects);
RectF dashedRectF = new RectF(mCenter - mRadius + 20 + getPaddingLeft(), mCenter - mRadius + 20 + getPaddingTop(), mCenter + mRadius - 20 - getPaddingRight(), mCenter + mRadius - 20 - getPaddingBottom());
float startAngle = 150;
canvas.drawArc(dashedRectF, startAngle, sweepAngle, false, dashPaint);复制代码
canvas.drawArc(rectF, startAngle, sweepAngle, false, outPaint);复制代码
canvas.drawArc(rectF, startAngle, getInSweepAngle(), false, inPaint);复制代码
4.绘制外侧进度弧度的小圆点,其实是一个Bitmap,注释比较详细,也比较简单,就是画布的平移跟旋转这个须要好好理解一下ide
//绘制发光的小圆点
Paint paintCircle = new Paint();
paintCircle.setStyle(Paint.Style.FILL);
paintCircle.setAntiAlias(true);//抗锯齿功能
canvas.translate(getWidth() / 2, getHeight() / 2);//画布平移到圆心
canvas.rotate(getInSweepAngle() + 60);//旋转画布使得画布的Y轴通过小圆点
canvas.translate(0, getHeight() / 2 - getPaddingLeft());//再次平移画布至小圆点绘制的位置
//此处省略了Bitmap的处理过程bmp
Bitmap dotBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
canvas.drawBitmap(dotBitmap, -15, -15, paintCircle);
canvas.rotate(-(getInSweepAngle() + 60));//恢复canvas复制代码
5.绘制文字
这个在绘制数字的时候须要注意一下,由于传过来的是一个浮点数,须要拆分红整数跟小数两部分,可是当你讲float转化为String而后切割的时候,注意下须要将小数点进行转义,否则会分割失败动画
DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里若是小数不足2位,会以0补足.
String value = decimalFormat.format(indexValue);//format 返回的是字符串
Log.d("value---->", value);
String[] split = value.split("\\.");
String text = split[0];//整数部分复制代码
这个其实就是两个属性动画同时播放而已,经过计算扇形扫过的角度与数字增加的幅度
,而后在属性动画的update里面进行从新绘制整个View,就能够搞定ui
float inSweepAngle = sweepAngle * value / 100;
ValueAnimator angleAnim = ValueAnimator.ofFloat(0f, inSweepAngle);//角度的ValueAnimator
float inValue = value * 8888 / 100;
ValueAnimator valueAnim = ValueAnimator.ofFloat(0, inValue);//数字变化的ValueAnimator
//一块儿播放动画
animatorSet.playTogether(angleAnim, valueAnim);复制代码
public void goToPoint(float value) {
//在方法中进行播放动画
}复制代码
使用方法spa
mCircleIndicatorView.goToPoint(value);code