-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathBlurView.java
More file actions
90 lines (75 loc) · 2.53 KB
/
PathBlurView.java
File metadata and controls
90 lines (75 loc) · 2.53 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
public class PathBlurView extends View {
private Rect drawableRect;
private Rect viewRect;
private Bitmap mImageBitmap;
private Canvas mMaskCanvas;
private Paint mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Bitmap mMaskBitmap;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Path mPath = new Path();
public PathBlurView(Context context) {
this(context, null);
}
public PathBlurView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mMaskPaint.setMaskFilter(new BlurMaskFilter(10f, BlurMaskFilter.Blur.NORMAL));
mMaskPaint.setStyle(Paint.Style.STROKE);
mMaskPaint.setStrokeWidth(20);
mMaskPaint.setStrokeCap(Paint.Cap.ROUND);
mMaskPaint.setColor(Color.BLUE);
mMaskPaint.setAlpha(50);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w != oldw || h != oldh) {
viewRect = new Rect(0, 0, w, h);
mMaskBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mMaskCanvas = new Canvas(mMaskBitmap);
}
}
@Override
protected void onDraw(Canvas canvas) {
if (mImageBitmap == null) {
return;
}
canvas.drawBitmap(mImageBitmap, drawableRect, viewRect, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(mMaskBitmap, drawableRect, viewRect, paint);
paint.setXfermode(null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
return true;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
return true;
case MotionEvent.ACTION_UP:
break;
}
return super.onTouchEvent(event);
}
private void touchStart(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
}
private void touchMove(float x, float y) {
mPath.lineTo(x, y);
mMaskCanvas.drawPath(mPath, mMaskPaint);
}
public void setBitmap(Bitmap bitmap) {
mImageBitmap = bitmap;
drawableRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
invalidate();
}
}