-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorPickerView.java
More file actions
369 lines (323 loc) · 13.2 KB
/
ColorPickerView.java
File metadata and controls
369 lines (323 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package cn.itools.small.reader2.widget;
import cn.itools.lib.common.Alib;
import cn.itools.lib.common.Logger;
import cn.itools.small.reader.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class ColorPickerView extends View {
private Context mContext;
private Paint mRightPaint;
private int mHeight;
private int mWidth;
private int[] mRightColors;
private int RIGHT_WIDTH;
private int LEFT_WIDTH;
private Bitmap mLeftBitmap;
private Bitmap mLeftBitmap2;
private Bitmap mRightBitmap;
private Bitmap mRightBitmap2;
private Paint mBitmapPaint;
private final int SPLIT_WIDTH;
private boolean downInLeft = false;
private boolean downInRight = false;
private PointF mLeftSelectPoint;
private PointF mRightSelectPoint;
private OnColorChangedListener mChangedListener;
private boolean mLeftMove = false;
private boolean mRightMove = false;
private float mLeftBitmapRadius;
private Bitmap mGradualChangeBitmap;
private float mRightBitmapHalfHeight;
private float mRightBitmapQuarterWidth;
private int mCallBackColor = Integer.MAX_VALUE;
private float mSaturation;
private int mColor;
public ColorPickerView(Context context) {
this(context, null);
}
public ColorPickerView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
SPLIT_WIDTH = Alib.dp2px(mContext, 20);
init();
}
public void setOnColorChangedListenner(OnColorChangedListener listener) {
mChangedListener = listener;
}
private void init() {
mRightPaint = new Paint();
mRightPaint.setStyle(Paint.Style.FILL);
mRightPaint.setStrokeWidth(1);
mRightColors = new int[3];
mRightColors[0] = Color.WHITE;
mRightColors[2] = Color.BLACK;
mBitmapPaint = new Paint();
mLeftBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.reading__color_view__button);
mLeftBitmap2 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.reading__color_view__button);
mLeftBitmapRadius = mLeftBitmap.getWidth() / 2;
//SPLIT_WIDTH = mLeftBitmap.getWidth() / 2;
mLeftSelectPoint = new PointF(SPLIT_WIDTH, SPLIT_WIDTH);
mRightBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.reading__color_view__saturation);
mRightBitmap2 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.reading__color_view__saturation);
mRightSelectPoint = new PointF(SPLIT_WIDTH, SPLIT_WIDTH);
mRightBitmapHalfHeight = mRightBitmap.getHeight() / 2;
mRightBitmapQuarterWidth = mRightBitmap.getWidth() / 4;
RIGHT_WIDTH = mRightBitmap.getWidth() / 2;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// 左边
canvas.drawBitmap(getGradual() , null , new Rect(SPLIT_WIDTH, SPLIT_WIDTH, LEFT_WIDTH + SPLIT_WIDTH, mHeight - SPLIT_WIDTH), mBitmapPaint);
// 右边
mRightColors[1] = mRightPaint.getColor();
Shader rightShader = new LinearGradient(mWidth - SPLIT_WIDTH - RIGHT_WIDTH / 2, SPLIT_WIDTH, mWidth - SPLIT_WIDTH - RIGHT_WIDTH / 2, mHeight - SPLIT_WIDTH, mRightColors, null, Shader.TileMode.MIRROR);
mRightPaint.setShader(rightShader);
canvas.drawRect(new Rect(mWidth - SPLIT_WIDTH - RIGHT_WIDTH, SPLIT_WIDTH, mWidth - SPLIT_WIDTH, mHeight-SPLIT_WIDTH), mRightPaint);
// 两个图标
if (mLeftMove) {
canvas.drawBitmap(mLeftBitmap, mLeftSelectPoint.x - mLeftBitmapRadius, mLeftSelectPoint.y - mLeftBitmapRadius, mBitmapPaint);
} else {
canvas.drawBitmap(mLeftBitmap2, mLeftSelectPoint.x - mLeftBitmapRadius, mLeftSelectPoint.y - mLeftBitmapRadius, mBitmapPaint);
}
if (mRightMove) {
canvas.drawBitmap(mRightBitmap, mWidth - SPLIT_WIDTH - RIGHT_WIDTH - mRightBitmapQuarterWidth, mRightSelectPoint.y - mRightBitmapHalfHeight, mBitmapPaint);
} else {
canvas.drawBitmap(mRightBitmap2, mWidth - SPLIT_WIDTH - RIGHT_WIDTH - mRightBitmapQuarterWidth, mRightSelectPoint.y - mRightBitmapHalfHeight, mBitmapPaint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
mWidth = width;
} else {
mWidth = 480;
}
if (heightMode == MeasureSpec.EXACTLY) {
mHeight = height;
} else {
mHeight = 350;
}
LEFT_WIDTH = mWidth - SPLIT_WIDTH * 3 - RIGHT_WIDTH;
setMeasuredDimension(mWidth, mHeight);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
downInLeft = inLeftPanel(x, y);
downInRight = inRightPanel(x, y);
if (downInLeft) {
mLeftMove = true;
proofLeft(x, y);
mRightPaint.setColor(getLeftColor(mLeftSelectPoint.x-SPLIT_WIDTH, mLeftSelectPoint.y-SPLIT_WIDTH));
} else if (downInRight) {
mRightMove = true;
proofRight(x, y);
}
invalidate();
int rightColor = getRightColor(mRightSelectPoint.y - SPLIT_WIDTH);
if (mCallBackColor == Integer.MAX_VALUE || mCallBackColor != rightColor) {
mCallBackColor = rightColor;
} else {
break;
}
if (mChangedListener != null) {
mChangedListener.onColorChanged(mCallBackColor,
mRightPaint.getColor(),
(mRightSelectPoint.y - SPLIT_WIDTH) / (mHeight - 2 * SPLIT_WIDTH));
}
break;
case MotionEvent.ACTION_UP:
if (downInLeft) {
downInLeft = false;
} else if (downInRight) {
downInRight = false;
}
mLeftMove = false;
mRightMove = false;
invalidate();
if (mChangedListener != null) {
mChangedListener.onColorChanged(getRightColor(mRightSelectPoint.y - SPLIT_WIDTH),
mRightPaint.getColor(),
(mRightSelectPoint.y - SPLIT_WIDTH) / (mHeight - 2 * SPLIT_WIDTH));
}
}
return true;
}
@Override
protected void onDetachedFromWindow() {
if (mGradualChangeBitmap != null && mGradualChangeBitmap.isRecycled() == false) {
mGradualChangeBitmap.recycle();
}
if (mLeftBitmap != null && mLeftBitmap.isRecycled() == false) {
mLeftBitmap.recycle();
}
if (mLeftBitmap2 != null && mLeftBitmap2.isRecycled() == false) {
mLeftBitmap2.recycle();
}
if (mRightBitmap != null && mRightBitmap.isRecycled() == false) {
mRightBitmap.recycle();
}
if (mRightBitmap2 != null && mRightBitmap2.isRecycled() == false) {
mRightBitmap2.recycle();
}
super.onDetachedFromWindow();
}
private Bitmap getGradual() {
if (mGradualChangeBitmap == null) {
Paint leftPaint = new Paint();
leftPaint.setStrokeWidth(1);
mGradualChangeBitmap = Bitmap.createBitmap(LEFT_WIDTH, mHeight - 2 * SPLIT_WIDTH, Config.RGB_565);
Canvas canvas = new Canvas(mGradualChangeBitmap);
int bitmapWidth = mGradualChangeBitmap.getWidth();
LEFT_WIDTH = bitmapWidth;
int bitmapHeight = mGradualChangeBitmap.getHeight();
int[] leftColors = new int[] {Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA};
Shader leftShader = new LinearGradient(0, bitmapHeight / 2, bitmapWidth, bitmapHeight / 2, leftColors, null, TileMode.REPEAT);
LinearGradient shadowShader = new LinearGradient(bitmapWidth / 2, 0, bitmapWidth / 2, bitmapHeight,
Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
ComposeShader shader = new ComposeShader(leftShader, shadowShader, PorterDuff.Mode.SCREEN);
leftPaint.setShader(shader);
canvas.drawRect(0, 0, bitmapWidth, bitmapHeight, leftPaint);
}
return mGradualChangeBitmap;
}
private boolean inLeftPanel(float x, float y) {
if ( 0 < x && x < SPLIT_WIDTH + LEFT_WIDTH + SPLIT_WIDTH / 2 && 0 < y && y < mWidth) {
return true;
} else {
return false;
}
}
private boolean inRightPanel(float x, float y) {
if (mWidth - SPLIT_WIDTH - RIGHT_WIDTH - SPLIT_WIDTH / 2 < x && x < mWidth && 0 < y && y < mHeight) {
return true;
} else {
return false;
}
}
// 校正xy
private void proofLeft(float x, float y) {
if (x < SPLIT_WIDTH) {
mLeftSelectPoint.x = SPLIT_WIDTH;
} else if (x > (SPLIT_WIDTH + LEFT_WIDTH)) {
mLeftSelectPoint.x = SPLIT_WIDTH + LEFT_WIDTH;
} else {
mLeftSelectPoint.x = x;
}
if (y < SPLIT_WIDTH) {
mLeftSelectPoint.y = SPLIT_WIDTH;
} else if (y > (mHeight - SPLIT_WIDTH)) {
mLeftSelectPoint.y = mHeight - SPLIT_WIDTH;
} else {
mLeftSelectPoint.y = y;
}
}
private void proofRight(float x, float y) {
if (x < SPLIT_WIDTH) {
mRightSelectPoint.x = SPLIT_WIDTH;
} else if (x > (SPLIT_WIDTH + LEFT_WIDTH)) {
mRightSelectPoint.x = SPLIT_WIDTH + LEFT_WIDTH;
} else {
mRightSelectPoint.x = x;
}
if (y < SPLIT_WIDTH) {
mRightSelectPoint.y = SPLIT_WIDTH;
} else if (y > (mHeight - SPLIT_WIDTH)) {
mRightSelectPoint.y = mHeight - SPLIT_WIDTH;
} else {
mRightSelectPoint.y = y;
}
}
private int getLeftColor(float x, float y) {
Bitmap temp = getGradual();
// 为了防止越界
int intX = (int) x;
int intY = (int) y;
if (intX >= temp.getWidth()) {
intX = temp.getWidth() - 1;
}
if (intY >= temp.getHeight()) {
intY = temp.getHeight() - 1;
}
return temp.getPixel(intX, intY);
}
private int getRightColor(float y) {
int a, r, g, b, so, dst;
float p;
float rightHalfHeight = (mHeight - (float)SPLIT_WIDTH * 2) / 2;
if (y < rightHalfHeight) {
so = mRightColors[0];
dst = mRightColors[1];
p = y / rightHalfHeight;
} else {
so = mRightColors[1];
dst = mRightColors[2];
p = (y - rightHalfHeight) / rightHalfHeight;
}
a = ave(Color.alpha(so), Color.alpha(dst), p);
r = ave(Color.red(so), Color.red(dst), p);
g = ave(Color.green(so), Color.green(dst), p);
b = ave(Color.blue(so), Color.blue(dst), p);
return Color.argb(a, r, g, b);
}
private int ave(int s, int d, float p) {
return s + Math.round(p * (d - s));
}
/**
* 设置选择器当前的颜色和饱和度
* @param color
* @param saturation
*/
public void setColorAndSaturation(int color, float saturation){
mColor = color;
mSaturation = saturation;
requestLayout();
invalidate();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Logger.i("ColorPickerView", "onLayout");
Bitmap temp = getGradual();
for(int i=0; i < temp.getWidth(); i++){
for(int j=0; j<temp.getHeight(); j++){
if(temp.getPixel(i, j) == mColor){
mLeftSelectPoint.x = (i + SPLIT_WIDTH);
mLeftSelectPoint.y = (j + SPLIT_WIDTH);
mRightPaint.setColor(mColor);
break;
}
}
}
mRightSelectPoint.y = (mHeight - 2*SPLIT_WIDTH)*mSaturation + SPLIT_WIDTH;
super.onLayout(changed, left, top, right, bottom);
}
// ### 内部类 ###
public interface OnColorChangedListener {
void onColorChanged(int color, int originalColor, float saturation);
}
}