@@ -58,7 +58,7 @@ public class ShapeBlurView extends View {
58
58
private float mBlurRadius ;
59
59
public static final int DEFAULT_BORDER_COLOR = Color .WHITE ;
60
60
61
- private final BlurImpl mBlurImpl ;
61
+ private BlurImpl mBlurImpl ;
62
62
private boolean mDirty ;
63
63
private Bitmap mBitmapToBlur , mBlurredBitmap ;
64
64
private Canvas mBlurringCanvas ;
@@ -80,7 +80,7 @@ public class ShapeBlurView extends View {
80
80
private static int BLUR_IMPL ;
81
81
82
82
private int blurMode = BlurMode .MODE_RECTANGLE ;
83
- private final Paint mBitmapPaint ;
83
+ private Paint mBitmapPaint ;
84
84
//圆形 相关
85
85
private float cx = 0 , cy = 0 , cRadius = 0 ;
86
86
@@ -94,62 +94,105 @@ public class ShapeBlurView extends View {
94
94
private static final float DEFAULT_BORDER_WIDTH = 0f ;
95
95
96
96
private final RectF mBorderRect = new RectF ();
97
- private final Paint mBorderPaint ;
97
+ private Paint mBorderPaint ;
98
98
private float mBorderWidth = 0 ;
99
99
private ColorStateList mBorderColor = ColorStateList .valueOf (DEFAULT_BORDER_COLOR );
100
100
private Matrix matrix = new Matrix ();
101
101
private BitmapShader shader ;
102
102
103
+ public static class Config {
104
+ public float blurRadius = 25.0f ;
105
+ public float downSampleFactor = 4f ;
106
+ public int overlayColor = 0x000000 ;
107
+
108
+ public float cornerRadiusOverride = -1 ;
109
+ public float cornerTopLeft = -1 ;
110
+ public float cornerTopRight = -1 ;
111
+ public float cornerBottomRight = -1 ;
112
+ public float cornerBottomLeft = -1 ;
113
+
114
+ public int blurMode = BlurMode .MODE_RECTANGLE ;
115
+
116
+ public float borderWidth = DEFAULT_BORDER_WIDTH ;
117
+ public ColorStateList borderColor = ColorStateList .valueOf (DEFAULT_BORDER_COLOR );
118
+ }
119
+
120
+ public ShapeBlurView (Context context , Config config ) {
121
+ super (context );
122
+ mContext = context ;
123
+ mBlurImpl = getBlurImpl ();
124
+
125
+ initConfig (config );
126
+
127
+
128
+ }
129
+
103
130
public ShapeBlurView (Context context , AttributeSet attrs ) {
104
131
super (context , attrs );
105
132
mContext = context ;
106
- // provide your own by override getBlurImpl()
107
133
mBlurImpl = getBlurImpl ();
134
+
135
+ Config config = new Config ();
108
136
try {
109
137
TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .ShapeBlurView );
110
- mBlurRadius = a .getDimension (R .styleable .ShapeBlurView_blur_radius ,
138
+ config . blurRadius = a .getDimension (R .styleable .ShapeBlurView_blur_radius ,
111
139
TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , 10 , context .getResources ().getDisplayMetrics ()));
112
- mDownSampleFactor = a .getFloat (R .styleable .ShapeBlurView_blur_down_sample , 4 );
113
- mOverlayColor = a .getColor (R .styleable .ShapeBlurView_blur_overlay_color , 0x000000 );
140
+ config . downSampleFactor = a .getFloat (R .styleable .ShapeBlurView_blur_down_sample , 4 );
141
+ config . overlayColor = a .getColor (R .styleable .ShapeBlurView_blur_overlay_color , 0x000000 );
114
142
115
- float cornerRadiusOverride =
143
+ config . cornerRadiusOverride =
116
144
a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_corner_radius , -1 );
117
- mCornerRadii [ BlurCorner . TOP_LEFT ] =
145
+ config . cornerTopLeft =
118
146
a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_corner_radius_top_left , -1 );
119
- mCornerRadii [ BlurCorner . TOP_RIGHT ] =
147
+ config . cornerTopRight =
120
148
a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_corner_radius_top_right , -1 );
121
- mCornerRadii [ BlurCorner . BOTTOM_RIGHT ] =
149
+ config . cornerBottomRight =
122
150
a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_corner_radius_bottom_right , -1 );
123
- mCornerRadii [ BlurCorner . BOTTOM_LEFT ] =
151
+ config . cornerBottomLeft =
124
152
a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_corner_radius_bottom_left , -1 );
125
- initCornerData (cornerRadiusOverride );
126
- blurMode = a .getInt (R .styleable .ShapeBlurView_blur_mode , BlurMode .MODE_RECTANGLE );
127
-
128
- mBorderWidth = a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_border_width , -1 );
129
- if (mBorderWidth < 0 ) {
130
- mBorderWidth = DEFAULT_BORDER_WIDTH ;
131
- }
132
- mBorderColor = a .getColorStateList (R .styleable .ShapeBlurView_blur_border_color );
133
- if (mBorderColor == null ) {
134
- mBorderColor = ColorStateList .valueOf (DEFAULT_BORDER_COLOR );
135
- }
153
+ config .blurMode = a .getInt (R .styleable .ShapeBlurView_blur_mode , BlurMode .MODE_RECTANGLE );
136
154
155
+ config .borderWidth = a .getDimensionPixelSize (R .styleable .ShapeBlurView_blur_border_width , -1 );
156
+ config .borderColor = a .getColorStateList (R .styleable .ShapeBlurView_blur_border_color );
137
157
138
158
a .recycle ();
139
159
} catch (Exception e ) {
140
160
e .printStackTrace ();
141
161
}
162
+ initConfig (config );
163
+ }
164
+
165
+ private void initConfig (Config config ) {
166
+ mBlurRadius = config .blurRadius ;
167
+ mDownSampleFactor = config .downSampleFactor ;
168
+ mOverlayColor = config .overlayColor ;
169
+
170
+ mCornerRadii [BlurCorner .TOP_LEFT ] = config .cornerTopLeft ;
171
+ mCornerRadii [BlurCorner .TOP_RIGHT ] = config .cornerTopRight ;
172
+ mCornerRadii [BlurCorner .BOTTOM_RIGHT ] = config .cornerBottomRight ;
173
+ mCornerRadii [BlurCorner .BOTTOM_LEFT ] = config .cornerBottomLeft ;
174
+ initCornerData (config .cornerRadiusOverride );
175
+
176
+ blurMode = config .blurMode ;
177
+ mBorderWidth = config .borderWidth ;
178
+ if (mBorderWidth == -1 ) {
179
+ mBorderWidth = DEFAULT_BORDER_WIDTH ;
180
+ }
181
+
182
+ mBorderColor = config .borderColor ;
183
+ if (mBorderColor == null ) {
184
+ mBorderColor = ColorStateList .valueOf (DEFAULT_BORDER_COLOR );
185
+ }
186
+
187
+
142
188
mBitmapPaint = new Paint ();
143
189
// mBitmapPaint.setStyle(Paint.Style.FILL);
144
190
mBitmapPaint .setAntiAlias (true );
145
-
146
191
mBorderPaint = new Paint ();
147
192
mBorderPaint .setStyle (Paint .Style .STROKE );
148
193
mBorderPaint .setAntiAlias (true );
149
194
mBorderPaint .setColor (mBorderColor .getColorForState (getState (), DEFAULT_BORDER_COLOR ));
150
195
mBorderPaint .setStrokeWidth (mBorderWidth );
151
-
152
- // matrix = new Matrix();
153
196
}
154
197
155
198
private void initCornerData (float cornerRadiusOverride ) {
0 commit comments