思路:
实现思路非常简单:当前变化的圆点先从最小半径变大到最大最大半径再变回最小半径的圆,然后再切换到下个圆点,同时颜色会先变浅在变会原来的颜色(可以理解为透明度变化),而且当前圆点的上上一个圆点颜色会不断变浅。
直接上代码:
package com.kincai.testcustomview_pointprogress; import android.content.context; import android.content.res.typedarray; import android.graphics.canvas; import android.graphics.paint; import android.support.annotation.nullable; import android.support.v4.content.contextcompat; import android.util.attributeset; import android.util.log; import android.view.view; /** * 尊龙凯时网址 copyright (c) 2015 the kincai open source project * . * create by kincai * . * time 2017-06-14 10:23 * . * desc 水平圆点进度条 */ public class dotpollingview extends view { private final string tag = this.getclass().getsimplename(); /** * 进度当前圆点画笔和正常圆点画笔 */ private paint mselectedpaint = new paint(), mnormalpaint = new paint(); /** * 正常圆点颜色 */ private int mcolor; /** * 变大圆点的颜色 */ private int mselectedcolor; /** * 圆点总数 */ private int mdottotalcount = 3; /** * 正常圆点半径 */ private int mdotradius; /** * 当前变化的圆点半径变化量 0.0 - (mdotmaxradius - mdotradius)之间 */ private float mdotcurrentradiuschange; /** * 圆点大小变化率 */ private float mradiuschangerate; /** * 最大圆点半径 */ private int mdotmaxradius; /** * 圆点最大间距 */ private int mdotspacing; /** * 当前变大的圆点索引 */ private int mcurrentdot = 0; private int malphachange = 0; private int malphachangetotal = 220; private final int dot_status_big = 0x101; private final int dot_status_small = 0x102; private int mdotchangestatus = dot_status_big; public void setcolor(int mcolor) { this.mcolor = mcolor; mnormalpaint.setcolor(mcolor); } public void setselectedcolor(int mselectedcolor) { this.mselectedcolor = mselectedcolor; mselectedpaint.setcolor(mselectedcolor); } public void setdottotalcount(int mdottotalcount) { this.mdottotalcount = mdottotalcount; } public void setdotradius(int mdotradius) { this.mdotradius = mdotradius; } public void setradiuschangerate(float mradiuschangerate) { this.mradiuschangerate = mradiuschangerate; } public void setdotmaxradius(int mdotmaxradius) { this.mdotmaxradius = mdotmaxradius; } public void setdotspacing(int mdotspacing) { this.mdotspacing = mdotspacing; } public dotpollingview(context context) { this(context, null); } public dotpollingview(context context, @nullable attributeset attrs) { this(context, attrs, 0); } public dotpollingview(context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.dotpollingview, defstyleattr, 0); initattributes(typedarray); typedarray.recycle(); init(); } private void initattributes(typedarray attributes) { mcolor = attributes.getcolor(r.styleable.dotpollingview_dotp_dot_color, contextcompat.getcolor(getcontext(),r.color.colorprimary)); mselectedcolor = attributes.getcolor(r.styleable.dotpollingview_dotp_dot_selected_color, contextcompat.getcolor(getcontext(),r.color.coloraccent)); mdotradius = attributes.getdimensionpixelsize(r.styleable.dotpollingview_dotp_dot_radius,densityutils.dp2px(getcontext(),3)); mdotmaxradius = attributes.getdimensionpixelsize(r.styleable.dotpollingview_dotp_dot_max_radius,densityutils.dp2px(getcontext(),5)); mdotspacing = attributes.getdimensionpixelsize(r.styleable.dotpollingview_dotp_dot_spacing,densityutils.dp2px(getcontext(),6)); mdottotalcount = attributes.getinteger(r.styleable.dotpollingview_dotp_dot_count,3); mradiuschangerate = attributes.getfloat(r.styleable.dotpollingview_dotp_dot_size_change_rate,0.3f); } /** * 初始化 */ private void init() { mdotcurrentradiuschange = 0f; mselectedpaint.setcolor(mselectedcolor); mselectedpaint.setantialias(true); mselectedpaint.setstyle(paint.style.fill); mnormalpaint.setcolor(mcolor); mnormalpaint.setantialias(true); mnormalpaint.setstyle(paint.style.fill); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { //测量宽高 int widthmode = measurespec.getmode(widthmeasurespec); int widthsize = measurespec.getsize(widthmeasurespec); int heightmode = measurespec.getmode(heightmeasurespec); int heightsize = measurespec.getsize(heightmeasurespec); int width; int height; if(widthmode == measurespec.exactly) { width = widthsize; log.e(tag, "onmeasure measurespec.exactly widthsize=" widthsize); } else { //指定最小宽度所有圆点加上间距的宽度, 以最小半径加上间距算总和再加上最左边和最右边变大后的距离 width = (mdottotalcount * mdotradius * 2 ((mdottotalcount - 1) * mdotspacing)) (mdotmaxradius - mdotradius) * 2; log.e(tag, "onmeasure no measurespec.exactly widthsize=" widthsize " width=" width); if(widthmode == measurespec.at_most) { width = math.min(width, widthsize); log.e(tag, "onmeasure measurespec.at_most width=" width); } } if(heightmode == measurespec.exactly) { height = heightsize; log.e(tag, "onmeasure measurespec.exactly heightsize=" heightsize); } else { height = mdotmaxradius * 2; log.e(tag, "onmeasure no measurespec.exactly heightsize=" heightsize " height=" height); if(heightmode == measurespec.at_most) { height = math.min(height, heightsize); log.e(tag, "onmeasure measurespec.at_most height=" height); } } setmeasureddimension(width,height); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); mnormalpaint.setalpha(255); mselectedpaint.setalpha(255); if(mdotchangestatus == dot_status_big) { mdotcurrentradiuschange = mradiuschangerate; malphachange =12; } else { mdotcurrentradiuschange -= mradiuschangerate; malphachange -=12; } if(malphachange >= malphachangetotal) { malphachange = malphachangetotal; } log.e("dotpollingview", "dot current radius change: " mdotcurrentradiuschange); //第一个圆点的圆心x坐标计算:控件宽度的一半减去(所有圆点直径的和以及所有间距的和相加的总和的一半)再加上一个半径大小 // ,为什么要加上半径?因为我们起点要的是圆心,但算出来的是最左边x坐标 int startpointx = getwidth() / 2 - (mdottotalcount * mdotradius * 2 ((mdottotalcount - 1) * mdotspacing)) / 2 mdotradius; //所有圆点的圆心y坐标一致控件高度的一半 int startpointy = getheight() / 2; for (int i = 0; i < mdottotalcount; i ) { if(mcurrentdot == i) {//当前圆点 mselectedpaint.setalpha(255 - malphachange); canvas.drawcircle(startpointx mcurrentdot * (mdotradius * 2 mdotspacing), startpointy , mdotradius mdotcurrentradiuschange, mselectedpaint); continue; } else if(mcurrentdot > 1 && mcurrentdot - 2 == i) {//当前圆点前两个 mnormalpaint.setalpha(255 - malphachange); canvas.drawcircle(startpointx (mcurrentdot - 2) * (mdotradius * 2 mdotspacing), startpointy, mdotradius, mnormalpaint); continue; } //画正常的圆点 mnormalpaint.setalpha(255); canvas.drawcircle(startpointx i * (mdotradius * 2 mdotspacing), startpointy, mdotradius, mnormalpaint); } //当圆点变化率达到最大或超过最大半径和正常半径之差时 变化率重置0,当前变大圆点移至下一圆点 if (mdotcurrentradiuschange >= (mdotmaxradius - mdotradius) && mdotchangestatus == dot_status_big) { mdotcurrentradiuschange = mdotmaxradius - mdotradius; mdotchangestatus = dot_status_small; } else if(mdotcurrentradiuschange <= 0 && mdotchangestatus == dot_status_small) { mdotchangestatus = dot_status_big; mdotcurrentradiuschange = 0f; mcurrentdot = mcurrentdot == mdottotalcount - 1 ? 0 : mcurrentdot 1; malphachange = 0; } invalidate(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。