private static final int[] COLORS = new int[]{0xFFFFFFFF, 0xFFFF0000, 0xFFFF00FF,
0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFF000000};
再来分析下,这里我们需要画几个圆,再画圆角矩形,,还要对滑动做1下运动区间,左右左右出去就不好了
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {//点击时
case MotionEvent.ACTION_DOWN:
currentOffset = (int) event.getX();
if (currentOffset <= thumbRadius+ScreemHelper.dp2px(context,60)) currentOffset = thumbRadius+ScreemHelper.dp2px(context,60);
if (currentOffset >= barWidth + thumbRadius+ScreemHelper.dp2px(context,60))
currentOffset = barWidth + thumbRadius+ScreemHelper.dp2px(context,60);
STATUS = STATUS_CHANGE;
break;
case MotionEvent.ACTION_MOVE:
currentOffset = (int) event.getX();
if (currentOffset <= thumbRadius+ScreemHelper.dp2px(context,60)) currentOffset = thumbRadius+ScreemHelper.dp2px(context,60);
if (currentOffset >= barWidth + thumbRadius+ScreemHelper.dp2px(context,60))
currentOffset = barWidth + thumbRadius+ScreemHelper.dp2px(context,60);
break;
}
invalidate();
return true;
}
barPaint.setShader(
new LinearGradient(startX, startY + barHeight / 2,
startX + barWidth, startY + barHeight / 2,
COLORS, null, Shader.TileMode.CLAMP));
int unit = barWidth / (COLORS.length - 1);
int position = currentOffset - thumbRadius-ScreemHelper.dp2px(context,60);
int i = position / unit;
int step = position % unit;
if (i >= COLORS.length - 1) return COLORS[COLORS.length - 1];
int c0 = COLORS[i];
int c1 = COLORS[i + 1];
int a = ave(Color.alpha(c0), Color.alpha(c1), unit, step);
int r = ave(Color.red(c0), Color.red(c1), unit, step);
int g = ave(Color.green(c0), Color.green(c1), unit, step);
int b = ave(Color.blue(c0), Color.blue(c1), unit, step);
return Color.rgb(r, g, b);
or(看你需要哪个了)
return Color.argb(a,r, g, b);
public interface onColorChangeListener {
void onColorChange(int color);
}
上一篇 JAVA多线程与并发学习总结
下一篇 能在多种前端框架下使用的表格控件