Android修炼系列(十三),2021年五面蚂蚁

  • @param p 当前刻度
  • @param specialScaleCorlors 每个范围的颜色值
  • @return 当前需要的颜色值
    */
    public static int evaluateColor(int p, int[] specialScaleCorlors) {
    // 定义的颜色区间
    int startInt = 0xFFbebebe;
    int endInt = 0xFFbebebe;
    float fraction = 0.5f;

if (p != 0 && p != 100) {
startInt = specialScaleCorlors[p / 20];
endInt = specialScaleCorlors[p / 20 + 1];
fraction = (p - (p / 20) * 20) / 20f;
}
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;

int endA = (endInt >> 24) & 0xff;
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;

return (int) ((startA + (int) (fraction * (endA - startA))) << 24)
| (int) ((startR + (int) (fraction * (endR - startR))) << 16)
| (int) ((startG + (int) (fraction * (endG - startG))) << 8)
| (int) ((startB + (int) (fraction * (endB - startB))));
}

其余的细节和方法就不贴了,都是比较常规的Paint方法。

栗子γ

这个效果和上面的很类似,不同的是这个控件可以通过拖动来选择刻度,具体见下:

2021-04-25 at 22.34.12.gif

在绘制“拖动按钮”Bitmap的时候,难点是确定bitmap的坐标,即根据圆心坐标,半径,扇形角度来求扇形终射线与圆弧交叉点的x, y坐标,上面是不是已经说啦,这样我们就能算出bitmap的左上角坐标了。

拖动效果是在我们允许的区域内,当手指按下,手指滑动,手指弹起时,不断绘制对应的进度p,给人一种圆环被拖着动画的错觉,其实这只是不断重绘的结果。这里需要我们通过onTouchEvent方法来监听手势及获取当前坐标。难点在于这是一个弧形轨迹,我们怎么通过当前坐标来获取角度,再根据角度获取相对应的进度。代码示例如下:

@Override
public synchronized boolean onTouchEvent(MotionEvent event) {

int action = event.getAction();
int x = (int) event.getX();
int y = (int) event.getY();

switch (action) {
case MotionEvent.ACTION_DOWN:
// isOnRing 注释见下
if (isOnRing(x, y) && y <&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值