35
35
public class ShadowLayout extends FrameLayout {
36
36
37
37
// Default shadow values
38
- private final static float DEFAULT_SHADOW_RADIUS = 30.0f ;
39
- private final static float DEFAULT_SHADOW_DISTANCE = 15.0f ;
40
- private final static float DEFAULT_SHADOW_ANGLE = 45.0f ;
38
+ private final static float DEFAULT_SHADOW_RADIUS = 30.0F ;
39
+ private final static float DEFAULT_SHADOW_DISTANCE = 15.0F ;
40
+ private final static float DEFAULT_SHADOW_ANGLE = 45.0F ;
41
41
private final static int DEFAULT_SHADOW_COLOR = Color .DKGRAY ;
42
42
43
43
// Shadow bounds values
44
- private final static float MAX_ANGLE = 360.0f ;
45
- private final static float MIN_RADIUS = 0.1f ;
46
- private final static float MIN_ANGLE = 0.0f ;
44
+ private final static int MAX_ALPHA = 255 ;
45
+ private final static float MAX_ANGLE = 360.0F ;
46
+ private final static float MIN_RADIUS = 0.1F ;
47
+ private final static float MIN_ANGLE = 0.0F ;
47
48
// Shadow paint
48
49
private final Paint mPaint = new Paint (Paint .ANTI_ALIAS_FLAG ) {
49
50
{
@@ -53,9 +54,9 @@ public class ShadowLayout extends FrameLayout {
53
54
};
54
55
// Shadow bitmap and canvas
55
56
private Bitmap mBitmap ;
56
- private Canvas mCanvas = new Canvas ();
57
+ private final Canvas mCanvas = new Canvas ();
57
58
// View bounds
58
- private Rect mBounds = new Rect ();
59
+ private final Rect mBounds = new Rect ();
59
60
// Check whether need to redraw shadow
60
61
private boolean mInvalidateShadow = true ;
61
62
@@ -64,6 +65,7 @@ public class ShadowLayout extends FrameLayout {
64
65
65
66
// Shadow variables
66
67
private int mShadowColor ;
68
+ private int mShadowAlpha ;
67
69
private float mShadowRadius ;
68
70
private float mShadowDistance ;
69
71
private float mShadowAngle ;
@@ -170,8 +172,8 @@ public int getShadowColor() {
170
172
171
173
public void setShadowColor (final int shadowColor ) {
172
174
mShadowColor = shadowColor ;
175
+ mShadowAlpha = Color .alpha (shadowColor );
173
176
174
- mPaint .setColor (shadowColor );
175
177
resetShadow ();
176
178
}
177
179
@@ -186,15 +188,24 @@ public float getShadowDy() {
186
188
// Reset shadow layer
187
189
private void resetShadow () {
188
190
// Detect shadow axis offset
189
- mShadowDx = (float ) ((mShadowDistance ) * Math .cos (mShadowAngle / 180.0f * Math .PI ));
190
- mShadowDy = (float ) ((mShadowDistance ) * Math .sin (mShadowAngle / 180.0f * Math .PI ));
191
+ mShadowDx = (float ) ((mShadowDistance ) * Math .cos (mShadowAngle / 180.0F * Math .PI ));
192
+ mShadowDy = (float ) ((mShadowDistance ) * Math .sin (mShadowAngle / 180.0F * Math .PI ));
191
193
192
194
// Set padding for shadow bitmap
193
195
final int padding = (int ) (mShadowDistance + mShadowRadius );
194
196
setPadding (padding , padding , padding , padding );
195
197
requestLayout ();
196
198
}
197
199
200
+ private int adjustShadowAlpha (final boolean adjust ) {
201
+ return Color .argb (
202
+ adjust ? MAX_ALPHA : mShadowAlpha ,
203
+ Color .red (mShadowColor ),
204
+ Color .green (mShadowColor ),
205
+ Color .blue (mShadowColor )
206
+ );
207
+ }
208
+
198
209
@ Override
199
210
protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
200
211
super .onMeasure (widthMeasureSpec , heightMeasureSpec );
@@ -241,6 +252,7 @@ protected void dispatchDraw(final Canvas canvas) {
241
252
mCanvas .drawColor (0 , PorterDuff .Mode .CLEAR );
242
253
243
254
// Draw extracted alpha bounds of our local canvas
255
+ mPaint .setColor (adjustShadowAlpha (false ));
244
256
mCanvas .drawBitmap (extractedAlpha , mShadowDx , mShadowDy , mPaint );
245
257
246
258
// Recycle and clear extracted alpha
@@ -251,9 +263,11 @@ protected void dispatchDraw(final Canvas canvas) {
251
263
}
252
264
}
253
265
266
+ // Reset alpha to draw child with full alpha
267
+ mPaint .setColor (adjustShadowAlpha (true ));
254
268
// Draw shadow bitmap
255
269
if (mCanvas != null && mBitmap != null && !mBitmap .isRecycled ())
256
- canvas .drawBitmap (mBitmap , 0.0f , 0.0f , null );
270
+ canvas .drawBitmap (mBitmap , 0.0F , 0.0F , mPaint );
257
271
}
258
272
259
273
// Draw child`s
0 commit comments