talk is cheap ,show your pic
颜色选择器,首先想用progressbar去渲染,后来觉得太麻烦了,直接自定义view了,关键还是对progressbar深入不多。
首先看看我们需要什么,这么多颜色,还有渐变效果,一脸懵逼,别怕,我们有着色器LinearGradient,不会玩的先去找度娘玩一圈再来,颜色嘛,直接找出图中视图中的纯色。
private static final int[] COLORS = new int[]{0xFFFFFFFF, 0xFFFF0000, 0xFFFF00FF,
0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFF000000};
再来分析下,这里我们需要画几个圆,再画圆角矩形,,还要对滑动做一下运动区间,左右左右出去就不好了
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;
}
这里ScreemHelper是对dp转换px的工具类。。。。。
滚动条的渲染
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);
}