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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
| /** * view 添加阴影 */ public class ShadowDrawableUtil extends Drawable {
private Paint mShadowPaint; private Paint mBgPaint; private Paint mPaint; private Paint mBgPartPaint; private int mShadowRadius; private int mShape; private int mShapeRadius; private int mBgColor[]; private RectF mRect; private TypeEnum type; private static int SHAPE_ROUND = 1; private static int SHAPE_CIRCLE = 2; private static int SHAPE_ROUND_PART = 3;
public enum TypeEnum { All, LEFT, TOP, RIGHT, BOTTOM }
private ShadowDrawableUtil(int shape, TypeEnum type, int[] bgColor, int shapeRadius, int shadowColor, int shadowRadius) { this.mShape = shape; this.type = type; this.mBgColor = bgColor; this.mShapeRadius = shapeRadius; this.mShadowRadius = shadowRadius; /** * 阴影paint */ mShadowPaint = new Paint(); mShadowPaint.setAntiAlias(true); mShadowPaint.setShadowLayer(shadowRadius, 0, 0, shadowColor); /** * 覆盖部分背景的paint */ mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); /** * 背景paint */ mBgPaint = new Paint(); mBgPaint.setAntiAlias(true); /** * 覆盖部分圆角的paint */ mBgPartPaint = new Paint(); mBgPartPaint.setAntiAlias(true); }
@Override public void setBounds(int left, int top, int right, int bottom) { super.setBounds(left, top, right, bottom); switch (type) { case All: left = left + mShadowRadius; top = top + mShadowRadius; right = right - mShadowRadius; bottom = bottom - mShadowRadius; break; case TOP: case BOTTOM: top = top + mShadowRadius; bottom = bottom - mShadowRadius; break; case LEFT: case RIGHT: left = left + mShadowRadius; right = right - mShadowRadius; break; default: break; }
mRect = new RectF(left, top, right, bottom); }
@Override public void draw(@NonNull Canvas canvas) { if (mBgColor != null) { if (mBgColor.length == 1) { mBgPaint.setColor(mBgColor[0]); mShadowPaint.setColor(mBgColor[0]); mBgPartPaint.setColor(mBgColor[0]); } else { mBgPaint.setShader(new LinearGradient(mRect.left, mRect.height() / 2, mRect.right, mRect.height() / 2, mBgColor, null, Shader.TileMode.CLAMP)); } } /** * 设置覆盖阴影/圆角 的 RectF */ RectF rect = new RectF(); RectF rect1 = new RectF(); switch (type) { case All: rect.left = 0; rect.right = 0; rect.top = 0; rect.bottom = 0; break; case TOP: rect.left = mRect.left; rect.right = mRect.right; rect.top = mRect.height() / 2; rect.bottom = mRect.bottom + mShadowRadius; rect1.left = mRect.left; rect1.right = mRect.right; rect1.top = mRect.height() / 2; rect1.bottom = mRect.bottom; break; case BOTTOM: rect.left = mRect.left; rect.right = mRect.right; rect.top = mRect.top - mShadowRadius; rect.bottom = mRect.height() / 2;
rect1.left = mRect.left; rect1.right = mRect.right; rect1.top = mRect.top; rect1.bottom = mRect.height() / 2;
break; case LEFT: rect.left = mRect.width() / 2; rect.right = mRect.right + mShadowRadius; rect.top = mRect.top; rect.bottom = mRect.bottom;
rect1.left = mRect.width() / 2; rect1.right = mRect.right ; rect1.top = mRect.top; rect1.bottom = mRect.bottom;
break; case RIGHT: rect.left = mRect.left-mShadowRadius; rect.right = mRect.width() / 2; rect.top = mRect.top; rect.bottom = mRect.bottom;
rect1.left = mRect.left; rect1.right = mRect.width() / 2; rect1.top = mRect.top; rect1.bottom = mRect.bottom; break;
default: break; }
if (mShape == SHAPE_ROUND) { /** * 绘制全圆角阴影 */ canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint); canvas.drawRect(rect, mPaint); canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint); } else if (mShape == SHAPE_ROUND_PART) { /** * 绘制部分圆角阴影 */ canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint); canvas.drawRect(rect, mPaint); canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint); canvas.drawRect(rect1, mBgPartPaint); } else { /** * 绘制圆形阴影 */ canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height()) / 2, mShadowPaint); canvas.drawRect(rect, mPaint); canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height()) / 2, mBgPaint); } }
@Override public void setAlpha(int alpha) { mShadowPaint.setAlpha(alpha); }
@Override public void setColorFilter(@Nullable ColorFilter colorFilter) { mShadowPaint.setColorFilter(colorFilter); }
@Override public int getOpacity() { return PixelFormat.TRANSLUCENT; }
public static void setShadowDrawable(View view) { ShadowDrawableUtil drawable = new Builder() .setType(TypeEnum.BOTTOM) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawable(View view, String bgColor) { ShadowDrawableUtil drawable = new Builder() .setType(TypeEnum.BOTTOM) .setBgColor(bgColor) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawable(View view, TypeEnum type) { ShadowDrawableUtil drawable = new Builder() .setType(type) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawableCircle(View view) { ShadowDrawableUtil drawable = new Builder() .setShape(SHAPE_CIRCLE) .setType(TypeEnum.All) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawableTop(View view, String shapeColor) { ShadowDrawableUtil drawable = new Builder() .setShape(SHAPE_ROUND_PART) .setType(TypeEnum.TOP) .setShadowColor(shapeColor) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawable(View view, String shapeColor, int shapeRadius, String shadowColor, int shadowRadius) { ShadowDrawableUtil drawable = new Builder() .setBgColor(shapeColor) .setShapeRadius(shapeRadius) .setShadowColor(shadowColor) .setShadowRadius(shadowRadius) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawable(View view, int shape, TypeEnum type, String shapeColor, int shapeRadius, String shadowColor, int shadowRadius) { ShadowDrawableUtil drawable = new Builder() .setBgColor(shapeColor) .setShapeRadius(shapeRadius) .setShadowColor(shadowColor) .setShadowRadius(shadowRadius) .setShape(shape) .setType(type) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
public static void setShadowDrawable(View view, int shape, TypeEnum type, String[] shapeColor, int shapeRadius, String shadowColor, int shadowRadius) { ShadowDrawableUtil drawable = new Builder() .setBgColor(shapeColor) .setShapeRadius(shapeRadius) .setShadowColor(shadowColor) .setShadowRadius(shadowRadius) .setShape(shape) .setType(type) .builder(); view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); ViewCompat.setBackground(view, drawable); }
private static class Builder { private int mShape; private int mShapeRadius; private int mShadowColor; private int mShadowRadius; private int[] mBgColor; private TypeEnum type;
private Builder() { mShape = SHAPE_ROUND; type = TypeEnum.All; mShapeRadius = 17; mShadowColor = Color.parseColor("#FF0000"); mShadowRadius = 4; mBgColor = new int[1]; mBgColor[0] = Color.GREEN; }
private Builder setShape(int mShape) { this.mShape = mShape; return this; }
private Builder setShapeRadius(int ShapeRadius) { this.mShapeRadius = ShapeRadius; return this; }
private Builder setShadowColor(String shadowColor) {
this.mShadowColor = Color.parseColor(shadowColor); return this; }
private Builder setShadowRadius(int shadowRadius) { this.mShadowRadius = shadowRadius; return this; }
private Builder setBgColor(String bgColor) { this.mBgColor[0] = Color.parseColor(bgColor); return this; }
private Builder setType(TypeEnum type) { this.type = type; return this; }
private Builder setBgColor(String[] bgColors) { if (bgColors != null && bgColors.length > 0) { int[] b = new int[bgColors.length]; for (int i = 0; i < bgColors.length; i++) { b[i] = Color.parseColor(bgColors[i]); } this.mBgColor = b; }
return this; }
private ShadowDrawableUtil builder() { return new ShadowDrawableUtil(mShape, type, mBgColor, dp2px(mShapeRadius), mShadowColor, dp2px(mShadowRadius)); } }
private static int dp2px(float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, ShadowApplication.getApplication().getResources().getDisplayMetrics()); } }
|