android 个性按钮,酷炫的日间夜间模式切换按钮

前言

最近看到一个酷炫的日间夜间模式切换按钮,于是就动手写了仿写了一个。

这里是Github链接,欢迎star,fork...

PO上效果图,录制的不太好

bVCWZF

主要的编写思路

这里主要是用属性动画中的ValueAnimator来产生具有一组有规律数字,然后主要绘制日间模式(云朵跳动的效果),夜间模式(星星闪烁的效果,月亮逆向旋转的效果),全局设计有四种固定的模式(SWITCH_ANIMATION_OFF, SWITCH_ANIMATION_ON, SWITCH_ON, SWITCH_OFF)分别对应着按钮动画关闭,按钮动画打开,打开,关闭,这几种具体的状态。

一、日间模式

主要分为日间模式打开状态。

private void drawSwitchOn(Canvas canvas) {

float[] rectAttrs = compRoundRectAttr(SWITCH_OFF_POS);

drawRoundRect(canvas, switchOnColor, rectAttrs);//按钮主题的背景

float[] ovalAttrs = compOvalAttr(SWITCH_ON_POS);

drawOval(canvas, spotOnColor, ovalAttrs);//绘制太阳

drawOvalIn(canvas, spotOnColorIn, ovalAttrs);

drawCloud(canvas, 1);//绘制云朵

drawRoundRectStroke(canvas, DEFAULT_SWITCH_ON_COLOR_OUT);

}

日间模式动画打开状态。

private void drawSwitchOnAnim(Canvas canvas) {

float[] rectAttrs = compRoundRectAttr(SWITCH_OFF_POS);

drawRoundRect(canvas, switchOnColor, rectAttrs);

rectAttrs = compRoundRectAttr(currentPos);

drawRoundRect(canvas, switchOffColor, rectAttrs);

float[] ovalShadeOnAttrs = compRoundRectShadeOnAttr(currentPos * 3/2);

float[] ovalAttrs = compOvalAttr(currentPos* 3/2);

int color = compColor(currentPos, DEFAULT_SPOT_OFF_COLOR, DEFAULT_SPOT_ON_COLOR);

int colorIn = compColor(currentPos, DEFAULT_SPOT_OFF_COLOR_IN, DEFAULT_SPOT_ON_COLOR_IN);

drawRoundRect(canvas, color, ovalShadeOnAttrs);

drawOval(canvas, color, ovalAttrs);

drawOvalIn(canvas, colorIn, ovalAttrs);

if(currentPos > 0.6) {

drawCloud(canvas, currentPos);//云朵的动态效果

}

int strokeColor = compColor(currentPos, DEFAULT_SWITCH_OFF_COLOR_OUT, DEFAULT_SWITCH_ON_COLOR_OUT);

drawRoundRectStroke(canvas, strokeColor);

}

二、夜间模式

主要分为夜间模式打开状态。

float[] rectAttrs = compRoundRectAttr(SWITCH_OFF_POS);

drawRoundRect(canvas, switchOffColor, rectAttrs);

float[] ovalAttrs = compOvalAttr(SWITCH_OFF_POS);

drawOval(canvas, spotOffColor, ovalAttrs);

drawOvalIn(canvas, spotOffColorIn, ovalAttrs);

drawCircleDot(canvas, spotOffColor, spotOffColorIn, 1, ovalAttrs);

drawCircleDot2(canvas, spotOffColor, spotOffColorIn, 1, ovalAttrs);//绘制月亮上的原点

drawCircleDot3(canvas, spotOffColor, spotOffColorIn, 1, ovalAttrs);//绘制月亮上的原点

drawStar(canvas, DEFAULT_SPOT_OFF_COLOR_IN, 1);//绘制星星

drawRoundRectStroke(canvas, DEFAULT_SWITCH_OFF_COLOR_OUT);

主要分为夜间模式动画打开状态。

float[] rectAttrs = compRoundRectAttr(SWITCH_OFF_POS);

if (currentPos != 1) {

drawRoundRect(canvas, switchOffColor, rectAttrs);

}

//夜间模式的背景

rectAttrs = compRoundRectAttr(1 - currentPos);

drawRoundRect(canvas, switchOffColor, rectAttrs);

float[] ovalAttrs;

if(currentPos > 2.0/3){

ovalAttrs = compOvalAttr(0);

}else{

ovalAttrs = compOvalAttr(1 - currentPos * 3/2);

}

float[] ovalShadeOffAttrs = compRoundRectShadeOffAttr(1 - currentPos * 3/2);

int color = compColor(currentPos, DEFAULT_SPOT_ON_COLOR, DEFAULT_SPOT_OFF_COLOR);

int colorIn = compColor(currentPos, DEFAULT_SPOT_ON_COLOR_IN, DEFAULT_SPOT_OFF_COLOR_IN);

drawRoundRect(canvas, color, ovalShadeOffAttrs);

drawOval(canvas, color, ovalAttrs);

drawOvalIn(canvas, colorIn, ovalAttrs);

drawCircleDot(canvas, DEFAULT_SPOT_OFF_COLOR, DEFAULT_SPOT_OFF_COLOR_IN, currentPos, ovalAttrs);//绘制月亮上的动态旋转的原点

drawCircleDot2(canvas, DEFAULT_SPOT_OFF_COLOR, DEFAULT_SPOT_OFF_COLOR_IN, currentPos, ovalAttrs);

drawCircleDot3(canvas, DEFAULT_SPOT_OFF_COLOR, DEFAULT_SPOT_OFF_COLOR_IN, currentPos, ovalAttrs);

if(currentPos > 0.6) {

drawStar(canvas, DEFAULT_SPOT_OFF_COLOR_IN, currentPos);//绘制闪烁的星星

}

int strokeColor = compColor(currentPos, DEFAULT_SWITCH_ON_COLOR_OUT, DEFAULT_SWITCH_OFF_COLOR_OUT);

drawRoundRectStroke(canvas, strokeColor);

最后

具体的代码见我的Github,先写这么多,有空再来继续补全文章。这里是Github链接,欢迎star,fork...谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值