一、前言
android是一种以linux与java为基础的开放源代码操作系统,主要使用于便携设备。中国大陆地区较多人使用“安卓”。android操作系统最初由andy rubin开发,被谷歌收购后则由google公司和开放手机联盟领导及开发,主要支持手机与平板。
二、实现
先看下实现的效果吧:
说下实现思路:圆点x轴会有个位移变化量,当位移达到圆点直径 圆点间距之和就回改变方向(改变方向就是通过变化量值不断增加和不断减少来实现),可能写的有点模糊,接下来看代码:
package com.kincai.testcustomview_dotalternatelyprogress; 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-16 21:44 * . * desc 两个源点来回移动 */ public class dotalternatelyview extends view { private final string tag = this.getclass().getsimplename(); private paint mpaint = new paint(); /** * 可视为左边圆点颜色值 */ private int mleftcolor; /** * 可视为右边圆点颜色值 */ private int mrightcolor; /** * 圆点半径 */ private int mdotradius; /** * 圆点间距 */ private int mdotspacing; /** * 圆点位移量 */ private float mmovedistance; /** * 圆点移动率 */ private float mmoverate; /** * 以刚开始左边圆点为准 向右移 */ private final int dot_status_right = 0x101; /** * 以刚开始左边圆点为准 圆点移动方向-向左移 */ private final int dot_status_left = 0x102; /** * 以刚开始左边圆点为准,圆点移动方向 */ private int mdotchangestatus = dot_status_right; /** * 圆点透明度变化最大(也就是透明度在255-malphachangetotal到255之间) */ private int malphachangetotal = 130; /** * 透明度变化率 */ private float malphachangerate; /** * 透明度改变量 */ private float malphachange; public dotalternatelyview(context context) { this(context, null); } public dotalternatelyview(context context, @nullable attributeset attrs) { this(context, attrs, 0); } public dotalternatelyview(context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.dotalternatelyview, defstyleattr, 0); initattributes(typedarray); typedarray.recycle(); init(); } private void initattributes(typedarray attributes) { mleftcolor = attributes.getcolor(r.styleable.dotalternatelyview_dot_dark_color, contextcompat.getcolor(getcontext(), r.color.colorprimary)); mrightcolor = attributes.getcolor(r.styleable.dotalternatelyview_dot_light_color, contextcompat.getcolor(getcontext(), r.color.coloraccent)); mdotradius = attributes.getdimensionpixelsize(r.styleable.dotalternatelyview_dot_radius, densityutils.dp2px(getcontext(), 3)); mdotspacing = attributes.getdimensionpixelsize(r.styleable.dotalternatelyview_dot_spacing, densityutils.dp2px(getcontext(), 6)); mmoverate = attributes.getfloat(r.styleable.dotalternatelyview_dot_move_rate, 1.2f); } /** * 初始化 */ private void init() { //移动总距离/移动率 = alpha总变化/x //x = 移动率 * alpha总变化 / 移动总距离 malphachangerate = mmoverate * malphachangetotal / (mdotradius * 2 mdotspacing); mpaint.setcolor(mleftcolor); mpaint.setantialias(true); mpaint.setstyle(paint.style.fill); log.e(tag, " aaaa " malphachangerate); } @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 = (mdotradius * 2) * 2 mdotspacing; 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 = mdotradius * 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) { //左边圆点起点x轴 int startpointx = getwidth() / 2 - (2 * mdotradius * 2 mdotspacing) / 2 mdotradius; //左边圆点起点y轴 int startpointy = getheight() / 2; //向右移 位移要增加对应透明度变化量也需要增加 反之都需要减小 if (mdotchangestatus == dot_status_right) { mmovedistance = mmoverate; malphachange = malphachangerate; } else { malphachange -= malphachangerate; mmovedistance -= mmoverate; } log.e(tag, "malphachange " malphachange); //当移动到最右 那么需要改变方向 反过来 if (mmovedistance >= mdotradius * 2 mdotspacing && mdotchangestatus == dot_status_right) { mdotchangestatus = dot_status_left; mmovedistance = mdotradius * 2 mdotspacing; malphachange = malphachangetotal; } else if (mmovedistance <= 0 && mdotchangestatus == dot_status_left) { //当移动到最座 那么需要改变方向 反过来 mdotchangestatus = dot_status_right; mmovedistance = 0f; malphachange = 0f; } //因为两个圆点可能会给定不同的颜色来显示 所以提供两种颜色设置mleftcolor和mrightcolor mpaint.setcolor(mleftcolor); mpaint.setalpha((int) (255 - malphachange)); canvas.drawcircle(startpointx mmovedistance, startpointy, mdotradius, mpaint); mpaint.setcolor(mrightcolor); mpaint.setalpha((int) (255 - malphachange)); canvas.drawcircle(startpointx mdotradius * 2 - mmovedistance mdotspacing, startpointy, mdotradius, mpaint); invalidate(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。