-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathRNCMaskedView.java
More file actions
84 lines (64 loc) · 2.22 KB
/
RNCMaskedView.java
File metadata and controls
84 lines (64 loc) · 2.22 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
package org.reactnative.maskedview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.view.View;
import com.facebook.react.views.view.ReactViewGroup;
public class RNCMaskedView extends ReactViewGroup {
private static final String TAG = "RNCMaskedView";
private Bitmap mBitmapMask = null;
private Paint mPaint;
private PorterDuffXfermode mPorterDuffXferMode;
public RNCMaskedView(Context context) {
super(context);
setLayerType(LAYER_TYPE_SOFTWARE, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPorterDuffXferMode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// redraw mask element to support animated elements
updateBitmapMask();
// draw the mask
if (mBitmapMask != null) {
mPaint.setXfermode(mPorterDuffXferMode);
canvas.drawBitmap(mBitmapMask, 0, 0, mPaint);
mPaint.setXfermode(null);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
updateBitmapMask();
}
}
private void updateBitmapMask() {
if (this.mBitmapMask != null) {
this.mBitmapMask.recycle();
}
View maskView = getChildAt(0);
maskView.setVisibility(View.VISIBLE);
this.mBitmapMask = getBitmapFromView(maskView);
maskView.setVisibility(View.INVISIBLE);
}
public void setPorterDuffMode(String mode) {
PorterDuff.Mode porterDuffMode = PorterDuff.Mode.valueOf(mode);
mPorterDuffXferMode = new PorterDuffXfermode(porterDuffMode);
}
public static Bitmap getBitmapFromView(final View view) {
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
if (view.getMeasuredWidth() <= 0 || view.getMeasuredHeight() <= 0) {
return null;
}
final Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
}