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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
| class HighlightView {
public static final int GROW_NONE = (1 << 0); public static final int GROW_LEFT_EDGE = (1 << 1); public static final int GROW_RIGHT_EDGE = (1 << 2); public static final int GROW_TOP_EDGE = (1 << 3); public static final int GROW_BOTTOM_EDGE = (1 << 4); public static final int MOVE = (1 << 5);
private static final int DEFAULT_HIGHLIGHT_COLOR = 0xFF33B5E5; private static final float HANDLE_RADIUS_DP = 12f; private static final float OUTLINE_DP = 2f;
enum ModifyMode {None, Move, Grow}
enum HandleMode {Changing, Always, Never}
RectF cropRect; Rect drawRect; Matrix matrix; private RectF imageRect;
private final Paint outsidePaint = new Paint(); private final Paint outlinePaint = new Paint(); private final Paint handlePaint = new Paint();
private View viewContext; private boolean showThirds; private boolean showCircle; private int highlightColor;
private ModifyMode modifyMode = ModifyMode.None; private HandleMode handleMode = HandleMode.Changing; private boolean maintainAspectRatio; private float initialAspectRatio; private float handleRadius; private float outlineWidth; private boolean isFocused;
public HighlightView(View context) { viewContext = context; initStyles(context.getContext()); }
private void initStyles(Context context) { TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(R.attr.cropImageStyle, outValue, true); TypedArray attributes = context.obtainStyledAttributes(outValue.resourceId, R.styleable.CropImageView); try { showThirds = attributes.getBoolean(R.styleable.CropImageView_showThirds, false); showCircle = attributes.getBoolean(R.styleable.CropImageView_showCircle, false); highlightColor = attributes.getColor(R.styleable.CropImageView_highlightColor, DEFAULT_HIGHLIGHT_COLOR); handleMode = HandleMode.values()[attributes.getInt(R.styleable.CropImageView_showHandles, 0)]; } finally { attributes.recycle(); } }
public void setup(Matrix m, Rect imageRect, RectF cropRect, boolean maintainAspectRatio) { matrix = new Matrix(m); this.cropRect = cropRect; this.imageRect = new RectF(imageRect); this.maintainAspectRatio = maintainAspectRatio; initialAspectRatio = this.cropRect.width() / this.cropRect.height(); drawRect = computeLayout(); outsidePaint.setARGB(125, 50, 50, 50); outlinePaint.setStyle(Paint.Style.STROKE); outlinePaint.setAntiAlias(true); outlineWidth = dpToPx(OUTLINE_DP);
handlePaint.setColor(highlightColor); handlePaint.setStyle(Paint.Style.FILL); handlePaint.setAntiAlias(true); handleRadius = dpToPx(HANDLE_RADIUS_DP); modifyMode = ModifyMode.None; }
private float dpToPx(float dp) { return dp * viewContext.getResources().getDisplayMetrics().density; }
protected void draw(Canvas canvas) { canvas.save(); Path path = new Path(); outlinePaint.setStrokeWidth(outlineWidth); if (!hasFocus()) { outlinePaint.setColor(Color.BLACK); canvas.drawRect(drawRect, outlinePaint); } else { Rect viewDrawingRect = new Rect(); viewContext.getDrawingRect(viewDrawingRect);
path.addRect(new RectF(drawRect), Path.Direction.CW); outlinePaint.setColor(highlightColor); if (isClipPathSupported(canvas)) { canvas.clipPath(path, Region.Op.DIFFERENCE); canvas.drawRect(viewDrawingRect, outsidePaint); } else { drawOutsideFallback(canvas); } canvas.restore(); canvas.drawPath(path, outlinePaint); if (showThirds) { drawThirds(canvas); } if (showCircle) { drawCircle(canvas); }
if (handleMode == HandleMode.Always || (handleMode == HandleMode.Changing && modifyMode == ModifyMode.Grow)) { drawHandles(canvas); } } }
* Fall back to naive method for darkening outside crop area */ private void drawOutsideFallback(Canvas canvas) { canvas.drawRect(0, 0, canvas.getWidth(), drawRect.top, outsidePaint); canvas.drawRect(0, drawRect.bottom, canvas.getWidth(), canvas.getHeight(), outsidePaint); canvas.drawRect(0, drawRect.top, drawRect.left, drawRect.bottom, outsidePaint); canvas.drawRect(drawRect.right, drawRect.top, canvas.getWidth(), drawRect.bottom, outsidePaint); }
* Clip path is broken, unreliable or not supported on: * - JellyBean MR1 * - ICS & ICS MR1 with hardware acceleration turned on */ @SuppressLint("NewApi") private boolean isClipPathSupported(Canvas canvas) { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) { return false; } else if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) || Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { return true; } else { return !canvas.isHardwareAccelerated(); } }
* 画handle的圆 * * @param canvas */ private void drawHandles(Canvas canvas) { int xMiddle = drawRect.left + ((drawRect.right - drawRect.left) / 2); int yMiddle = drawRect.top + ((drawRect.bottom - drawRect.top) / 2);
canvas.drawCircle(drawRect.left, yMiddle, handleRadius, handlePaint); canvas.drawCircle(xMiddle, drawRect.top, handleRadius, handlePaint); canvas.drawCircle(drawRect.right, yMiddle, handleRadius, handlePaint); canvas.drawCircle(xMiddle, drawRect.bottom, handleRadius, handlePaint); }
* 画里面的四条线 * * @param canvas */ private void drawThirds(Canvas canvas) { outlinePaint.setStrokeWidth(1); float xThird = (drawRect.right - drawRect.left) / 3; float yThird = (drawRect.bottom - drawRect.top) / 3;
canvas.drawLine(drawRect.left + xThird, drawRect.top, drawRect.left + xThird, drawRect.bottom, outlinePaint); canvas.drawLine(drawRect.left + xThird * 2, drawRect.top, drawRect.left + xThird * 2, drawRect.bottom, outlinePaint); canvas.drawLine(drawRect.left, drawRect.top + yThird, drawRect.right, drawRect.top + yThird, outlinePaint); canvas.drawLine(drawRect.left, drawRect.top + yThird * 2, drawRect.right, drawRect.top + yThird * 2, outlinePaint); }
private void drawCircle(Canvas canvas) { outlinePaint.setStrokeWidth(1); canvas.drawOval(new RectF(drawRect), outlinePaint); }
public void setMode(ModifyMode mode) { if (mode != modifyMode) { modifyMode = mode; viewContext.invalidate(); } }
public int getHit(float x, float y) { Rect r = computeLayout(); final float hysteresis = 20F; int retval = GROW_NONE;
boolean verticalCheck = (y >= r.top - hysteresis) && (y < r.bottom + hysteresis); boolean horizCheck = (x >= r.left - hysteresis) && (x < r.right + hysteresis);
if ((Math.abs(r.left - x) < hysteresis) && verticalCheck) { retval |= GROW_LEFT_EDGE; } if ((Math.abs(r.right - x) < hysteresis) && verticalCheck) { retval |= GROW_RIGHT_EDGE; } if ((Math.abs(r.top - y) < hysteresis) && horizCheck) { retval |= GROW_TOP_EDGE; } if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) { retval |= GROW_BOTTOM_EDGE; }
if (retval == GROW_NONE && r.contains((int) x, (int) y)) { retval = MOVE; } return retval; }
void handleMotion(int edge, float dx, float dy) { Rect r = computeLayout(); if (edge == MOVE) { moveBy(dx * (cropRect.width() / r.width()), dy * (cropRect.height() / r.height())); } else { if (((GROW_LEFT_EDGE | GROW_RIGHT_EDGE) & edge) == 0) { dx = 0; } if (((GROW_TOP_EDGE | GROW_BOTTOM_EDGE) & edge) == 0) { dy = 0; }
float xDelta = dx * (cropRect.width() / r.width()); float yDelta = dy * (cropRect.height() / r.height()); growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta, (((edge & GROW_TOP_EDGE) != 0) ? -1 : 1) * yDelta); } }
void moveBy(float dx, float dy) { Rect invalRect = new Rect(drawRect);
cropRect.offset(dx, dy);
cropRect.offset( Math.max(0, imageRect.left - cropRect.left), Math.max(0, imageRect.top - cropRect.top));
cropRect.offset( Math.min(0, imageRect.right - cropRect.right), Math.min(0, imageRect.bottom - cropRect.bottom));
drawRect = computeLayout(); invalRect.union(drawRect); invalRect.inset(-(int) handleRadius, -(int) handleRadius); viewContext.invalidate(invalRect); }
void growBy(float dx, float dy) { if (maintainAspectRatio) { if (dx != 0) { dy = dx / initialAspectRatio; } else if (dy != 0) { dx = dy * initialAspectRatio; } }
RectF r = new RectF(cropRect); if (dx > 0F && r.width() + 2 * dx > imageRect.width()) { dx = (imageRect.width() - r.width()) / 2F; if (maintainAspectRatio) { dy = dx / initialAspectRatio; } } if (dy > 0F && r.height() + 2 * dy > imageRect.height()) { dy = (imageRect.height() - r.height()) / 2F; if (maintainAspectRatio) { dx = dy * initialAspectRatio; } } r.inset(-dx, -dy);
final float widthCap = 25F; if (r.width() < widthCap) { r.inset(-(widthCap - r.width()) / 2F, 0F); } float heightCap = maintainAspectRatio ? (widthCap / initialAspectRatio) : widthCap; if (r.height() < heightCap) { r.inset(0F, -(heightCap - r.height()) / 2F); }
if (r.left < imageRect.left) { r.offset(imageRect.left - r.left, 0F); } else if (r.right > imageRect.right) { r.offset(-(r.right - imageRect.right), 0F); } if (r.top < imageRect.top) { r.offset(0F, imageRect.top - r.top); } else if (r.bottom > imageRect.bottom) { r.offset(0F, -(r.bottom - imageRect.bottom)); }
cropRect.set(r); drawRect = computeLayout(); viewContext.invalidate(); }
public Rect getScaledCropRect(float scale) { return new Rect((int) (cropRect.left * scale), (int) (cropRect.top * scale), (int) (cropRect.right * scale), (int) (cropRect.bottom * scale)); }
private Rect computeLayout() { RectF r = new RectF(cropRect.left, cropRect.top, cropRect.right, cropRect.bottom); matrix.mapRect(r); return new Rect(Math.round(r.left), Math.round(r.top), Math.round(r.right), Math.round(r.bottom)); }
public void invalidate() { drawRect = computeLayout(); }
public boolean hasFocus() { return isFocused; }
public void setFocus(boolean isFocused) { this.isFocused = isFocused; }
}
|