Android颜色选择器

参考网上文章,做了两种颜色选择器。

一种是固定颜色的选择器,这个很简单,只要画出来各种颜色区域,用户选择哪个,就选择了什么颜色。

另一种是万能颜色选择器,这个有一些算法的,所以就参考了网上的文章(由于原始出处不详,就不表示感谢了),又做了一些优化和修改。

目前的这个万能颜色选择器的功能已经足够了,也没什么可保密的,就放上来,给需要的人做个参考。喜欢就拿去好了。

view plaincopy to clipboardprint?
01.package arui.csdn.generaltools.colorchooser.universalcolor;
02.import arui.csdn.generaltools.colorchooser.ColorChooserType;
03.import arui.csdn.generaltools.colorchooser.OnColorChangedListener;
04.import android.content.Context;
05.import android.graphics.Canvas;
06.import android.graphics.Color;
07.import android.graphics.LinearGradient;
08.import android.graphics.Paint;
09.import android.graphics.RectF;
10.import android.graphics.Shader;
11.import android.graphics.SweepGradient;
12.import android.view.MotionEvent;
13.import android.view.View;
14./**
15. * Universal color view class. This class will draw color chooser graph.
16. *
17. * @author http://blog.csdn.net/arui319
18. *
19. */
20.public class UniversalColorView extends View {
21. private Paint mPaint;
22. private Paint mCenterPaint;
23. private Paint mHSVPaint;
24. private final int[] mColors;
25. private int[] mHSVColors;
26. private boolean mRedrawHSV;
27. private OnColorChangedListener mListener;
28. private boolean mTrackingCenter;
29. private boolean mHighlightCenter;
30. private static final int CENTER_X = 100;
31. private static final int CENTER_Y = CENTER_X;
32. private static final int CENTER_RADIUS = 30;
33. private static final int OUTER_RADIUS = 100;
34. private static final int HSV_X = CENTER_X;
35. private static final int HSV_Y_TOP = CENTER_Y + 10;
36. private static final int HSV_Y_BOTOM = HSV_Y_TOP + 20;
37. private static final float PI = 3.1415926f;
38. public UniversalColorView(Context context, OnColorChangedListener listener,
39. int color) {
40. super(context);
41. this.setBackgroundColor(Color.LTGRAY);
42. mListener = listener;
43. mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF,
44. 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
45. Shader s = new SweepGradient(0, 0, mColors, null);
46. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
47. mPaint.setShader(s);
48. mPaint.setStyle(Paint.Style.STROKE);
49. mPaint.setStrokeWidth(55);
50. mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
51. mCenterPaint.setColor(color);
52. mCenterPaint.setStrokeWidth(5);
53. mHSVColors = new int[] { 0xFF000000, color, 0xFFFFFFFF };
54. mHSVPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
55. mHSVPaint.setStrokeWidth(10);
56. mRedrawHSV = true;
57. }
58. @Override
59. protected void onDraw(Canvas canvas) {
60. float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;
61. canvas.translate(CENTER_X, CENTER_X);
62. int c = mCenterPaint.getColor();
63. if (mRedrawHSV) {
64. mHSVColors[1] = c;
65. mHSVPaint.setShader(new LinearGradient(0 - HSV_X, 0, HSV_X, 0,
66. mHSVColors, null, Shader.TileMode.CLAMP));
67. }
68. canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
69. canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);
70. canvas.drawRect(new RectF(0 - HSV_X, HSV_Y_TOP, HSV_X, HSV_Y_BOTOM),
71. mHSVPaint);
72. if (mTrackingCenter) {
73. mCenterPaint.setStyle(Paint.Style.STROKE);
74. if (mHighlightCenter) {
75. mCenterPaint.setAlpha(0xFF);
76. } else {
77. mCenterPaint.setAlpha(0x80);
78. }
79. canvas.drawCircle(0, 0, CENTER_RADIUS
80. + mCenterPaint.getStrokeWidth(), mCenterPaint);
81. mCenterPaint.setStyle(Paint.Style.FILL);
82. mCenterPaint.setColor(c);
83. }
84. mRedrawHSV = true;
85. }
86. @Override
87. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
88. setMeasuredDimension(CENTER_X * 2, HSV_Y_BOTOM * 2 - 20);
89. }
90. @Override
91. public boolean onTouchEvent(MotionEvent event) {
92. float x = event.getX() - CENTER_X;
93. float y = event.getY() - CENTER_Y;
94. double radius = Math.sqrt(x * x + y * y);
95. boolean inCenter = radius <= CENTER_RADIUS;
96. boolean inOuter = radius <= OUTER_RADIUS;
97. switch (event.getAction()) {
98. case MotionEvent.ACTION_DOWN:
99. mTrackingCenter = inCenter;
100. if (inCenter) {
101. mHighlightCenter = true;
102. invalidate();
103. break;
104. }
105. case MotionEvent.ACTION_MOVE:
106. if (mTrackingCenter) {
107. if (mHighlightCenter != inCenter) {
108. mHighlightCenter = inCenter;
109. invalidate();
110. }
111. } else if ((x >= 0 - HSV_X && x <= HSV_X)
112. && (y <= HSV_Y_BOTOM && y >= HSV_Y_TOP)) {
113. // see if we are in the hsv slider
114. int a, r, g, b, c0, c1;
115. float p;
116. // set the center paint to this color
117. if (x < 0) {
118. c0 = mHSVColors[0];
119. c1 = mHSVColors[1];
120. p = (x + 100) / 100;
121. } else {
122. c0 = mHSVColors[1];
123. c1 = mHSVColors[2];
124. p = x / 100;
125. }
126. a = ave(Color.alpha(c0), Color.alpha(c1), p);
127. r = ave(Color.red(c0), Color.red(c1), p);
128. g = ave(Color.green(c0), Color.green(c1), p);
129. b = ave(Color.blue(c0), Color.blue(c1), p);
130. mCenterPaint.setColor(Color.argb(a, r, g, b));
131. mRedrawHSV = false;
132. invalidate();
133. } else if (inOuter) {
134. float angle = (float) Math.atan2(y, x);
135. // need to turn angle [-PI ... PI] into unit [0....1]
136. float unit = angle / (2 * PI);
137. if (unit < 0) {
138. unit += 1;
139. }
140. mCenterPaint.setColor(interpColor(mColors, unit));
141. invalidate();
142. }
143. break;
144. case MotionEvent.ACTION_UP:
145. if (mTrackingCenter) {
146. if (inCenter && mListener != null) {
147. mListener.colorChanged(this,
148. ColorChooserType.UNIVERSAL_COLOR_TYPE, mCenterPaint
149. .getColor());
150. }
151. mTrackingCenter = false;
152. invalidate();
153. }
154. break;
155. }
156. return true;
157. }
158. private int interpColor(int colors[], float unit) {
159. if (unit <= 0) {
160. return colors[0];
161. }
162. if (unit >= 1) {
163. return colors[colors.length - 1];
164. }
165. float p = unit * (colors.length - 1);
166. int i = (int) p;
167. p -= i;
168. // now p is just the fractional part [0...1) and i is the index
169. int c0 = colors[i];
170. int c1 = colors[i + 1];
171. int a = ave(Color.alpha(c0), Color.alpha(c1), p);
172. int r = ave(Color.red(c0), Color.red(c1), p);
173. int g = ave(Color.green(c0), Color.green(c1), p);
174. int b = ave(Color.blue(c0), Color.blue(c1), p);
175. return Color.argb(a, r, g, b);
176. }
177. private int ave(int s, int d, float p) {
178. return s + Math.round(p * (d - s));
179. }
180. public int getColor() {
181. return mCenterPaint.getColor();
182. }
183. public void setColor(int color) {
184. mCenterPaint.setColor(color);
185. }
186.}
package arui.csdn.generaltools.colorchooser.universalcolor;
import arui.csdn.generaltools.colorchooser.ColorChooserType;
import arui.csdn.generaltools.colorchooser.OnColorChangedListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.view.MotionEvent;
import android.view.View;
/**
* Universal color view class. This class will draw color chooser graph.
*
* @author http://blog.csdn.net/arui319
*
*/
public class UniversalColorView extends View {
private Paint mPaint;
private Paint mCenterPaint;
private Paint mHSVPaint;
private final int[] mColors;
private int[] mHSVColors;
private boolean mRedrawHSV;
private OnColorChangedListener mListener;
private boolean mTrackingCenter;
private boolean mHighlightCenter;
private static final int CENTER_X = 100;
private static final int CENTER_Y = CENTER_X;
private static final int CENTER_RADIUS = 30;
private static final int OUTER_RADIUS = 100;
private static final int HSV_X = CENTER_X;
private static final int HSV_Y_TOP = CENTER_Y + 10;
private static final int HSV_Y_BOTOM = HSV_Y_TOP + 20;
private static final float PI = 3.1415926f;
public UniversalColorView(Context context, OnColorChangedListener listener,
int color) {
super(context);
this.setBackgroundColor(Color.LTGRAY);
mListener = listener;
mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF,
0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
Shader s = new SweepGradient(0, 0, mColors, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(s);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(55);
mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCenterPaint.setColor(color);
mCenterPaint.setStrokeWidth(5);
mHSVColors = new int[] { 0xFF000000, color, 0xFFFFFFFF };
mHSVPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHSVPaint.setStrokeWidth(10);
mRedrawHSV = true;
}
@Override
protected void onDraw(Canvas canvas) {
float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;
canvas.translate(CENTER_X, CENTER_X);
int c = mCenterPaint.getColor();
if (mRedrawHSV) {
mHSVColors[1] = c;
mHSVPaint.setShader(new LinearGradient(0 - HSV_X, 0, HSV_X, 0,
mHSVColors, null, Shader.TileMode.CLAMP));
}
canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);
canvas.drawRect(new RectF(0 - HSV_X, HSV_Y_TOP, HSV_X, HSV_Y_BOTOM),
mHSVPaint);
if (mTrackingCenter) {
mCenterPaint.setStyle(Paint.Style.STROKE);
if (mHighlightCenter) {
mCenterPaint.setAlpha(0xFF);
} else {
mCenterPaint.setAlpha(0x80);
}
canvas.drawCircle(0, 0, CENTER_RADIUS
+ mCenterPaint.getStrokeWidth(), mCenterPaint);
mCenterPaint.setStyle(Paint.Style.FILL);
mCenterPaint.setColor(c);
}
mRedrawHSV = true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(CENTER_X * 2, HSV_Y_BOTOM * 2 - 20);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX() - CENTER_X;
float y = event.getY() - CENTER_Y;
double radius = Math.sqrt(x * x + y * y);
boolean inCenter = radius <= CENTER_RADIUS;
boolean inOuter = radius <= OUTER_RADIUS;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTrackingCenter = inCenter;
if (inCenter) {
mHighlightCenter = true;
invalidate();
break;
}
case MotionEvent.ACTION_MOVE:
if (mTrackingCenter) {
if (mHighlightCenter != inCenter) {
mHighlightCenter = inCenter;
invalidate();
}
} else if ((x >= 0 - HSV_X && x <= HSV_X)
&& (y <= HSV_Y_BOTOM && y >= HSV_Y_TOP)) {
// see if we are in the hsv slider
int a, r, g, b, c0, c1;
float p;
// set the center paint to this color
if (x < 0) {
c0 = mHSVColors[0];
c1 = mHSVColors[1];
p = (x + 100) / 100;
} else {
c0 = mHSVColors[1];
c1 = mHSVColors[2];
p = x / 100;
}
a = ave(Color.alpha(c0), Color.alpha(c1), p);
r = ave(Color.red(c0), Color.red(c1), p);
g = ave(Color.green(c0), Color.green(c1), p);
b = ave(Color.blue(c0), Color.blue(c1), p);
mCenterPaint.setColor(Color.argb(a, r, g, b));
mRedrawHSV = false;
invalidate();
} else if (inOuter) {
float angle = (float) Math.atan2(y, x);
// need to turn angle [-PI ... PI] into unit [0....1]
float unit = angle / (2 * PI);
if (unit < 0) {
unit += 1;
}
mCenterPaint.setColor(interpColor(mColors, unit));
invalidate();
}
break;
case MotionEvent.ACTION_UP:
if (mTrackingCenter) {
if (inCenter && mListener != null) {
mListener.colorChanged(this,
ColorChooserType.UNIVERSAL_COLOR_TYPE, mCenterPaint
.getColor());
}
mTrackingCenter = false;
invalidate();
}
break;
}
return true;
}
private int interpColor(int colors[], float unit) {
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length - 1];
}
float p = unit * (colors.length - 1);
int i = (int) p;
p -= i;
// now p is just the fractional part [0...1) and i is the index
int c0 = colors[i];
int c1 = colors[i + 1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
private int ave(int s, int d, float p) {
return s + Math.round(p * (d - s));
}
public int getColor() {
return mCenterPaint.getColor();
}
public void setColor(int color) {
mCenterPaint.setColor(color);
}
}


view plaincopy to clipboardprint?
01.package arui.csdn.generaltools.colorchooser;
02./**
03. * color changed listener.
04. *
05. * @author http://blog.csdn.net/arui319
06. *
07. */
08.public interface OnColorChangedListener {
09. /**
10. * Color changed event happened.
11. *
12. * @param source
13. * event source object
14. * @param type
15. * ColorChooserType
16. * @param color
17. * color int value
18. */
19. public void colorChanged(Object source, ColorChooserType type, int color);
20.}
package arui.csdn.generaltools.colorchooser;
/**
* color changed listener.
*
* @author http://blog.csdn.net/arui319
*
*/
public interface OnColorChangedListener {
/**
* Color changed event happened.
*
* @param source
* event source object
* @param type
* ColorChooserType
* @param color
* color int value
*/
public void colorChanged(Object source, ColorChooserType type, int color);
}

view plaincopy to clipboardprint?
01.package arui.csdn.generaltools.colorchooser;
02.
03./**
04. * Color chooser's type. One is defined color panel, another is universal color
05. * panel.
06. *
07. * @author http://blog.csdn.net/arui319
08. *
09. */
10.public class ColorChooserType {
11. private int type = 0;
12. private static final int DEFINED_COLOR = 1;
13. private static final int UNIVERSAL_COLOR = 2;
14. public static final ColorChooserType DEFINED_COLOR_TYPE = new ColorChooserType(
15. DEFINED_COLOR);
16. public static final ColorChooserType UNIVERSAL_COLOR_TYPE = new ColorChooserType(
17. UNIVERSAL_COLOR);
18. private ColorChooserType(int type) {
19. this.type = type;
20. }
21. public int getType() {
22. return type;
23. }
24. @Override
25. public boolean equals(Object type) {
26. if (type instanceof ColorChooserType) {
27. if (this.getType() == ((ColorChooserType) type).getType()) {
28. return true;
29. }
30. }
31. return false;
32. }
33. @Override
34. public int hashCode() {
35. return this.getType();
36. }
37.}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/arui319/archive/2010/11/02/5982036.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值