-
Notifications
You must be signed in to change notification settings - Fork 329
/
CropIwaOvalShape.java
76 lines (60 loc) 路 2.21 KB
/
CropIwaOvalShape.java
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
package com.steelkiwi.cropiwa.shape;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import com.steelkiwi.cropiwa.config.CropIwaOverlayConfig;
/**
* Created by yarolegovich on 04.02.2017.
* https://github.com/yarolegovich
*/
public class CropIwaOvalShape extends CropIwaShape {
private Path clipPath;
public CropIwaOvalShape(CropIwaOverlayConfig config) {
super(config);
clipPath = new Path();
}
@Override
protected void clearArea(Canvas canvas, RectF cropBounds, Paint clearPaint) {
canvas.drawOval(cropBounds, clearPaint);
}
@Override
protected void drawBorders(Canvas canvas, RectF cropBounds, Paint paint) {
canvas.drawOval(cropBounds, paint);
if (overlayConfig.isDynamicCrop()) {
canvas.drawRect(cropBounds, paint);
}
}
@Override
protected void drawGrid(Canvas canvas, RectF cropBounds, Paint paint) {
clipPath.rewind();
clipPath.addOval(cropBounds, Path.Direction.CW);
canvas.save(Canvas.CLIP_SAVE_FLAG);
canvas.clipPath(clipPath);
super.drawGrid(canvas, cropBounds, paint);
canvas.restore();
}
@Override
public CropIwaShapeMask getMask() {
return new OvalShapeMask();
}
private static class OvalShapeMask implements CropIwaShapeMask {
@Override
public Bitmap applyMaskTo(Bitmap croppedRegion) {
croppedRegion.setHasAlpha(true);
Paint maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
RectF ovalRect = new RectF(0, 0, croppedRegion.getWidth(), croppedRegion.getHeight());
Path maskShape = new Path();
//This is similar to ImageRect\Oval
maskShape.addRect(ovalRect, Path.Direction.CW);
maskShape.addOval(ovalRect, Path.Direction.CCW);
Canvas canvas = new Canvas(croppedRegion);
canvas.drawPath(maskShape, maskPaint);
return croppedRegion;
}
}
}