OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 package org.chromium.ui; | 4 package org.chromium.ui; |
5 | 5 |
6 import android.content.Context; | 6 import android.content.Context; |
7 import android.graphics.Canvas; | 7 import android.graphics.Canvas; |
8 import android.graphics.Color; | 8 import android.graphics.Color; |
9 import android.graphics.Paint; | 9 import android.graphics.Paint; |
10 import android.graphics.Rect; | 10 import android.graphics.Rect; |
11 import android.util.AttributeSet; | 11 import android.util.AttributeSet; |
12 import android.view.MotionEvent; | 12 import android.view.MotionEvent; |
13 import android.view.View; | 13 import android.view.View; |
| 14 import android.view.ViewGroup; |
| 15 import android.widget.ListView; |
| 16 import android.widget.AdapterView; |
14 | 17 |
| 18 import org.chromium.ui.ColorSuggestionListAdapter.OnColorSuggestionClickListener
; |
| 19 |
| 20 import java.util.ArrayList; |
| 21 import java.util.Arrays; |
15 | 22 |
16 /** | 23 /** |
17 * Draws a grid of (predefined) colors and allows the user to choose one of | 24 * Draws a grid of (predefined) colors and allows the user to choose one of |
18 * those colors. | 25 * those colors. |
19 */ | 26 */ |
20 public class ColorPickerSimple extends View { | 27 public class ColorPickerSimple extends ListView implements OnColorSuggestionClic
kListener { |
21 private static final int ROW_COUNT = 2; | |
22 | 28 |
23 private static final int COLUMN_COUNT = 4; | 29 private OnColorChangedListener mOnColorChangedListener; |
24 | 30 |
25 private static final int GRID_CELL_COUNT = ROW_COUNT * COLUMN_COUNT; | 31 private static final int[] DEFAULT_COLORS = { Color.RED, |
| 32 Color.CYAN, |
| 33 Color.BLUE, |
| 34 Color.GREEN, |
| 35 Color.MAGENTA, |
| 36 Color.YELLOW, |
| 37 Color.BLACK, |
| 38 Color.WHITE |
| 39 }; |
26 | 40 |
27 private static final int[] COLORS = { Color.RED, | 41 private static final int[] DEFAULT_COLOR_LABEL_IDS = { R.string.color_picker
_button_red, |
28 Color.CYAN, | 42 R.string.color_picker
_button_cyan, |
29 Color.BLUE, | 43 R.string.color_picker
_button_blue, |
30 Color.GREEN, | 44 R.string.color_picker
_button_green, |
31 Color.MAGENTA, | 45 R.string.color_picker
_button_magenta, |
32 Color.YELLOW, | 46 R.string.color_picker
_button_yellow, |
33 Color.BLACK, | 47 R.string.color_picker
_button_black, |
34 Color.WHITE | 48 R.string.color_picker
_button_white |
35 }; | 49 }; |
36 | |
37 private Paint mBorderPaint; | |
38 | |
39 private Rect[] mBounds; | |
40 | |
41 private Paint[] mPaints; | |
42 | |
43 private OnColorChangedListener mOnColorTouchedListener; | |
44 | |
45 private int mLastTouchedXPosition; | |
46 | |
47 private int mLastTouchedYPosition; | |
48 | 50 |
49 public ColorPickerSimple(Context context) { | 51 public ColorPickerSimple(Context context) { |
50 super(context); | 52 super(context); |
51 } | 53 } |
52 | 54 |
53 public ColorPickerSimple(Context context, AttributeSet attrs) { | 55 public ColorPickerSimple(Context context, AttributeSet attrs) { |
54 super(context, attrs); | 56 super(context, attrs); |
55 } | 57 } |
56 | 58 |
57 public ColorPickerSimple(Context context, AttributeSet attrs, int defStyle)
{ | 59 public ColorPickerSimple(Context context, AttributeSet attrs, int defStyle)
{ |
58 super(context, attrs, defStyle); | 60 super(context, attrs, defStyle); |
59 } | 61 } |
60 | 62 |
61 /** | 63 /** |
62 * Initializes the listener and precalculates the grid and color positions. | 64 * Initializes the listener and sets the adapter for the given list of sugge
stions. If the |
| 65 * suggestions is null a default set of colors will be used. |
63 * | 66 * |
| 67 * @param suggestions The list of suggestions that should be displayed. |
64 * @param onColorChangedListener The listener that gets notified when the us
er touches | 68 * @param onColorChangedListener The listener that gets notified when the us
er touches |
65 * a color. | 69 * a color. |
66 */ | 70 */ |
67 public void init(OnColorChangedListener onColorChangedListener) { | 71 public void init(ColorSuggestion[] suggestions, |
68 mOnColorTouchedListener = onColorChangedListener; | 72 OnColorChangedListener onColorChangedListener) { |
| 73 mOnColorChangedListener = onColorChangedListener; |
69 | 74 |
70 // This will get calculated when the layout size is updated. | 75 if (suggestions == null) { |
71 mBounds = null; | 76 suggestions = new ColorSuggestion[DEFAULT_COLORS.length]; |
72 | 77 for (int i = 0; i < suggestions.length; ++i) { |
73 mPaints = new Paint[GRID_CELL_COUNT]; | 78 suggestions[i] = |
74 for (int i = 0; i < GRID_CELL_COUNT; ++i) { | 79 new ColorSuggestion( |
75 Paint newPaint = new Paint(); | 80 DEFAULT_COLORS[i], |
76 newPaint.setColor(COLORS[i]); | 81 getContext().getString(DEFAULT_COLOR_LABEL_IDS[i])); |
77 mPaints[i] = newPaint; | 82 } |
78 } | 83 } |
79 | 84 |
80 mBorderPaint = new Paint(); | 85 ColorSuggestionListAdapter adapter = new ColorSuggestionListAdapter(getC
ontext(), |
81 int borderColor = getContext().getResources().getColor(R.color.color_pic
ker_border_color); | 86 sugg
estions); |
82 mBorderPaint.setColor(borderColor); | 87 adapter.setOnColorSuggestionClickListener(this); |
83 | 88 setAdapter(adapter); |
84 // Responds to the user touching the grid and works out which color has
been chosen as | |
85 // a result, depending on the X,Y coordinate. Note that we respond to th
e click event | |
86 // here, but the onClick() method doesn't provide us with the X,Y coordi
nates, so we | |
87 // track them in onTouchEvent() below. This way the grid reacts properly
to touch events | |
88 // whereas if we put this onClick() code in onTouchEvent below then we g
et some strange | |
89 // interactions with the ScrollView in the parent ColorPickerDialog. | |
90 setOnClickListener(new OnClickListener() { | |
91 @Override | |
92 public void onClick(View v) { | |
93 if (mOnColorTouchedListener != null && getWidth() > 0 && getHeig
ht() > 0) { | |
94 int column = mLastTouchedXPosition * COLUMN_COUNT / getWidth
(); | |
95 int row = mLastTouchedYPosition * ROW_COUNT / getHeight(); | |
96 | |
97 int colorIndex = (row * COLUMN_COUNT) + column; | |
98 if (colorIndex >= 0 && colorIndex < COLORS.length) { | |
99 mOnColorTouchedListener.onColorChanged(COLORS[colorIndex
]); | |
100 } | |
101 } | |
102 } | |
103 }); | |
104 } | 89 } |
105 | 90 |
106 /** | |
107 * Draws the grid of colors, based on the rectangles calculated in onSizeCha
nged(). | |
108 * Also draws borders in between the colored rectangles. | |
109 * | |
110 * @param canvas The canvas the colors are drawn onto. | |
111 */ | |
112 @Override | 91 @Override |
113 public void onDraw(Canvas canvas) { | 92 public void onColorSuggestionClick(ColorSuggestion suggestion) { |
114 if (mBounds == null || mPaints == null) { | 93 mOnColorChangedListener.onColorChanged(suggestion.mColor); |
115 return; | |
116 } | |
117 | |
118 canvas.drawColor(Color.WHITE); | |
119 | |
120 // Draw the actual colored rectangles. | |
121 for (int i = 0; i < GRID_CELL_COUNT; ++i) { | |
122 canvas.drawRect(mBounds[i], mPaints[i]); | |
123 } | |
124 | |
125 // Draw 1px borders between the rows. | |
126 for (int i = 0; i < ROW_COUNT - 1; ++i) { | |
127 canvas.drawLine(0, | |
128 mBounds[i * COLUMN_COUNT].bottom + 1, | |
129 getWidth(), | |
130 mBounds[i * COLUMN_COUNT].bottom + 1, | |
131 mBorderPaint); | |
132 } | |
133 | |
134 // Draw 1px borders between the columns. | |
135 for (int j = 0; j < COLUMN_COUNT - 1; ++j) { | |
136 canvas.drawLine(mBounds[j].right + 1, | |
137 0, | |
138 mBounds[j].right + 1, | |
139 getHeight(), | |
140 mBorderPaint); | |
141 } | |
142 } | |
143 | |
144 /** | |
145 * Stores the X,Y coordinates of the touch so that we can use them in the on
Click() listener | |
146 * above to work out where the click was on the grid. | |
147 * | |
148 * @param event The MotionEvent the X,Y coordinates are retrieved from. | |
149 */ | |
150 @Override | |
151 public boolean onTouchEvent(MotionEvent event) { | |
152 if (event.getAction() == MotionEvent.ACTION_DOWN) { | |
153 mLastTouchedXPosition = (int) event.getX(); | |
154 mLastTouchedYPosition = (int) event.getY(); | |
155 } | |
156 return super.onTouchEvent(event); | |
157 } | |
158 | |
159 /** | |
160 * Recalculates the color grid with the new sizes. | |
161 */ | |
162 @Override | |
163 protected void onSizeChanged(int width, int height, int oldw, int oldh) { | |
164 calculateGrid(width, height); | |
165 } | |
166 | |
167 /** | |
168 * Calculates the sizes and positions of the cells in the grid, splitting | |
169 * them up as evenly as possible. Leaves 3 pixels between each cell so that | |
170 * we can draw a border between them as well, and leaves a pixel around the | |
171 * edge. | |
172 */ | |
173 private void calculateGrid(final int width, final int height) { | |
174 mBounds = new Rect[GRID_CELL_COUNT]; | |
175 | |
176 for (int i = 0; i < ROW_COUNT; ++i) { | |
177 for (int j = 0; j < COLUMN_COUNT; ++j) { | |
178 int left = j * (width + 1) / COLUMN_COUNT + 1; | |
179 int right = (j + 1) * (width + 1) / COLUMN_COUNT - 2; | |
180 | |
181 int top = i * (height + 1) / ROW_COUNT + 1; | |
182 int bottom = (i + 1) * (height + 1) / ROW_COUNT - 2; | |
183 | |
184 Rect rect = new Rect(left, top, right, bottom); | |
185 mBounds[(i * COLUMN_COUNT) + j] = rect; | |
186 } | |
187 } | |
188 } | 94 } |
189 } | 95 } |
OLD | NEW |