Android修炼系列(十三),这些知识点你会吗

实现的难点是外围文字在环绕过程中,坐标位置的确认,即通过圆心坐标,半径,扇形角度,如何计算出扇形终射线与圆弧交叉点的x, y坐标,所幸网上都能找到解决方案及背后的数学模型,代码见下:

private void paintOutWord(Canvas canvas, String state) {
PointF progressPoint = CommentUtil.calcArcEndPointXY
(radius + getPaddingLeft() + specialScaleLineLength + scaleToRingSpace + wordWith
, radius + getPaddingTop() + specialScaleLineLength + scaleToRingSpace + wordHeigh
, radius + specialScaleLineLength + scaleToRingSpace
, progress * (360 / 100f), -90);
int left = (int) progressPoint.x;
int top = (int) progressPoint.y;
wordPaint.getTextBounds(state, 0, state.length(), rect);
if (left < radius + getPaddingLeft() + specialScaleLineLength + scaleToRingSpace + wordWith) {
left -= rect.width();
}
if (top > radius + getPaddingTop() + specialScaleLineLength + scaleToRingSpace + wordHeigh) {
top += rect.height();
}
canvas.drawText(state, left, top, wordPaint);
}

这个方法的作用是获取扇形终射线与圆弧交叉点的x, y坐标,感兴趣的可以研究下:

/**

  • @param cirX 圆centerX
  • @param cirY 圆centerY
  • @param radius 圆半径
  • @param cirAngle 当前弧角度
  • @param orginAngle 起点弧角度
  • @return 扇形终射线与圆弧交叉点的xy坐标
    */
    public static PointF calcArcEndPointXY(float cirX, float cirY, float radius,
    float cirAngle, float orginAngle) {
    cirAngle = (orginAngle + cirAngle) % 360;
    return calcArcEndPointXY(cirX, cirY, radius, cirAngle);
    }

/*

  • @param cirAngle 当前弧角度
    */
    public static PointF calcArcEndPointXY(float cirX, float cirY,
    float radius, float cirAngle) {
    float posX = 0.0f;
    float posY = 0.0f;
    // 将角度转换为弧度
    float arcAngle = (float) (Math.PI * cirAngle / 180.0);
    if (cirAngle < 90) {
    posX = cirX + (float) (Math.cos(arcAngle)) * radius;
    posY = cirY + (float) (Math.sin(arcAngle)) * radius;
    } else if (cirAngle == 90) {
    posX = cirX;
    posY = cirY + radius;
    } else if (cirAngle > 90 && cirAngle < 180) {
    arcAngle = (float) (Math.PI * (180 - cirAngle) / 180.0);
    posX = cirX - (float) (Math.cos(arcAngle)) * radius;
    posY = cirY + (float) (Math.sin(arcAngle)) * radius;
    } else if (cirAngle == 180) {
    posX = cirX - radius;
    posY = cirY;
    } else if (cirAngle > 180 && cirAngle < 270) {
    arcAngle = (float) (Math.PI * (cirAngle - 180) / 180.0);
    posX = cirX - (float) (Math.cos(arcAngle)) * radius;
    posY = cirY - (float) (Math.sin(arcAngle)) * radius;
    } else if (cirAngle == 270) {
    posX = cirX;
    posY = cirY - radius;
    } else {
    arcAngle = (float) (Math.PI * (360 - cirAngle) / 180.0);
    posX = cirX + (float) (Math.cos(arcAngle)) * radius;
    posY = cirY - (float) (Math.sin(arcAngle)) * radius;
    }
    return new PointF(posX, posY);
    }

颜色的渐变效果实现,就是获取每个刻度所对应的颜色段内等比例的16进制颜色值,代码如下:

/**

  • 通过刻度获取当前渐变颜色值
  • @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 <= radius + getPaddingTop() + specialScaleLineLength + scaleToRingSpace) {
updateProgress(x, y);
return true;
}
break;
case MotionEvent.ACTION_MOVE:
if (y <= radius + getPaddingTop() + specialScaleLineLength + scaleToRingSpace) {
updateProgress(x, y);
}
return true;
case MotionEvent.ACTION_UP:
invalidate();
break;
}

return super.onTouchEvent(event);
}

这是根据当前点的位置求角度,再转换成当前进度的方法:

private void updateProgress(int eventX, int eventY) {

double angle = Math.atan2(eventY - (radius + getPaddingLeft() + specialScaleLineLength + scaleToRingSpace)
, eventX - (radius + getPaddingLeft() + specialScaleLineLength + scaleToRingSpace)) / Math.PI;
angle = ((2 + angle) % 2 + (-beginLocation / 180f)) % 2;

if ((int) Math.round(angle * 100) >= 0) {
progress = (int) Math.round(angle * 100);
realShowProgress = getShowProgress(progress);
}

invalidate();
}

需要注意的是,当我们拖动“拖动按钮”时,我们需要定一个特定的接收事件的区域范围,只有当用户按在了规定的可滑动区域内,才能让用户拖动进度条,并不是在任意位置都能拖动小图标改变进度的,这是判断当前触摸屏幕的位置是否处于可滑动区域内的方法:

private boolean isOnRing(float eventX, float eventY) {

boolean result = false;
double distance = Math.sqrt(Math.pow(eventX - (radius + getPaddingLeft() + specialScaleLineLength + scaleToRingSpace), 2)

  • Math.pow(eventY - (radius+getPaddingLeft() + specialScaleLineLength + scaleToRingSpace), 2));

if (distance < (2 * radius+getPaddingLeft() + getPaddingRight() + 2 * (specialScaleLineLength + scaleToRingSpace))
&& distance > radius - slideAbleLocation) {
result = true;
}

return result;
}

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

栗子δ

这是一个波纹扩散、圆球旋转缩小的效果,具体见下:

2021-04-25 at 23.12.11.gif

这个效果是由一个整体的自定义View不断绘制而成。其中波纹扩散动画,是通过定时改变波纹半径来实现的,此波纹是由先后两个空心圆组成,在实现过程中要注意时间和各自的尺寸变化,核心代码见下:

public void startAnima() {
if (drawTimingThread != null) {
drawTimingThread.sendEmptyMessage(MSG_DRAW0); // 开始1波纹
float time = (mRMaxRadius - mRMinRadius) / distance * 0.5f; // 先取整,再取中
drawTimingThread.sendEmptyMessageDelayed(MSG_DRAW1, (int)(animaBotIntervalTime * time));//定时开启2波纹
}
}

这是波纹1的半径变化,参考代码如下:

if (mCurRadius0 <= mRMaxRadius) {
mCurRadius0 += distance;
} else {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

总结

Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

上面分享的字节跳动公司2021年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

Android学习PDF+学习视频+面试文档+知识点笔记

【Android高级架构视频学习资源】

所以:贵在坚持!*

上面分享的字节跳动公司2021年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。
[外链图片转存中…(img-uAqFlzzd-1710508501218)]

Android学习PDF+学习视频+面试文档+知识点笔记

【Android高级架构视频学习资源】

Android部分精讲视频领取学习后更加是如虎添翼!进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值