# Android修炼系列（十三），这些知识你必须拿下

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);
}

/**

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

/*

• @param cirAngle 当前弧角度
*/
public static PointF calcArcEndPointXY(float cirX, float cirY,
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;
} 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) {
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;
} 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);
}

/**

• 通过刻度获取当前渐变颜色值
• @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))));
}

### 栗子γ

@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:
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)

&& distance > radius - slideAbleLocation) {
result = true;
}

return result;
}

### 栗子δ

public void startAnima() {
float time = (mRMaxRadius - mRMinRadius) / distance * 0.5f; // 先取整，再取中
}
}

} else {
}
circlePointF0 = drawCircleOnRipple(MSG_DRAW0, curIndex0);

mRPaint0.setAlpha(getAlphaOfRipple(curIndex0));//透明度
mCirclePaint0.setAlpha(getAlphaOfRipple(curIndex0));
curIndex0++;
curIndex0 = 0;

cancleHandle(MSG_DRAW0);

private PointF drawCircleOnRipple(int msg, int index) {

// 周期开始，随机初始角度
if (index == 0 && msg == MSG_DRAW0) {
cirAngel0 = (float) (Math.random() * -360 + 180);
} else if (index == 0) {
cirAngel1 = (float) (Math.random() * -360 + 180);
}

return CommentUtil.calcArcEndPointXY(
// 每个周期旋转45度
, (msg == MSG_DRAW0 ? curIndex0 : curIndex1) * 1.0f
, msg == MSG_DRAW0 ? cirAngel0 : cirAngel1);
}

private int getAlphaOfRipple(int curIndex) {
final int alpha = curIndex * 150 * distance / (mRMaxRadius - mRMinRadius); // 只取150的二进制
return 255 - alpha;
}

### 最后

Android架构视频+BAT面试专题PDF+学习笔记?

https://blog.51cto.com/u_15466190/4928950

Scroll to Top