1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.N; 4 5 import android.graphics.ColorFilter; 6 import android.graphics.Paint; 7 import android.graphics.PathEffect; 8 import android.graphics.Shader; 9 import android.graphics.Typeface; 10 import org.robolectric.annotation.Implementation; 11 import org.robolectric.annotation.Implements; 12 import org.robolectric.annotation.RealObject; 13 import org.robolectric.shadow.api.Shadow; 14 import org.robolectric.util.ReflectionHelpers.ClassParameter; 15 16 @SuppressWarnings({"UnusedDeclaration"}) 17 @Implements(Paint.class) 18 public class ShadowPaint { 19 20 private int color; 21 private Paint.Style style; 22 private Paint.Cap cap; 23 private Paint.Join join; 24 private float width; 25 private float shadowRadius; 26 private float shadowDx; 27 private float shadowDy; 28 private int shadowColor; 29 private Shader shader; 30 private int alpha; 31 private ColorFilter filter; 32 private boolean antiAlias; 33 private boolean dither; 34 private int flags; 35 private PathEffect pathEffect; 36 37 @RealObject Paint paint; 38 private Typeface typeface; 39 private float textSize; 40 private Paint.Align textAlign = Paint.Align.LEFT; 41 42 @Implementation 43 protected void __constructor__(Paint otherPaint) { 44 ShadowPaint otherShadowPaint = Shadow.extract(otherPaint); 45 this.color = otherShadowPaint.color; 46 this.style = otherShadowPaint.style; 47 this.cap = otherShadowPaint.cap; 48 this.join = otherShadowPaint.join; 49 this.width = otherShadowPaint.width; 50 this.shadowRadius = otherShadowPaint.shadowRadius; 51 this.shadowDx = otherShadowPaint.shadowDx; 52 this.shadowDy = otherShadowPaint.shadowDy; 53 this.shadowColor = otherShadowPaint.shadowColor; 54 this.shader = otherShadowPaint.shader; 55 this.alpha = otherShadowPaint.alpha; 56 this.filter = otherShadowPaint.filter; 57 this.antiAlias = otherShadowPaint.antiAlias; 58 this.dither = otherShadowPaint.dither; 59 this.flags = otherShadowPaint.flags; 60 this.pathEffect = otherShadowPaint.pathEffect; 61 62 Shadow.invokeConstructor(Paint.class, paint, ClassParameter.from(Paint.class, otherPaint)); 63 } 64 65 @Implementation(minSdk = N) 66 protected static long nInit() { 67 return 1; 68 } 69 70 @Implementation 71 protected int getFlags() { 72 return flags; 73 } 74 75 @Implementation 76 protected void setFlags(int flags) { 77 this.flags = flags; 78 } 79 80 @Implementation 81 protected Shader setShader(Shader shader) { 82 this.shader = shader; 83 return shader; 84 } 85 86 @Implementation 87 protected int getAlpha() { 88 return alpha; 89 } 90 91 @Implementation 92 protected void setAlpha(int alpha) { 93 this.alpha = alpha; 94 } 95 96 @Implementation 97 protected Shader getShader() { 98 return shader; 99 } 100 101 @Implementation 102 protected void setColor(int color) { 103 this.color = color; 104 } 105 106 @Implementation 107 protected int getColor() { 108 return color; 109 } 110 111 @Implementation 112 protected void setStyle(Paint.Style style) { 113 this.style = style; 114 } 115 116 @Implementation 117 protected Paint.Style getStyle() { 118 return style; 119 } 120 121 @Implementation 122 protected void setStrokeCap(Paint.Cap cap) { 123 this.cap = cap; 124 } 125 126 @Implementation 127 protected Paint.Cap getStrokeCap() { 128 return cap; 129 } 130 131 @Implementation 132 protected void setStrokeJoin(Paint.Join join) { 133 this.join = join; 134 } 135 136 @Implementation 137 protected Paint.Join getStrokeJoin() { 138 return join; 139 } 140 141 @Implementation 142 protected void setStrokeWidth(float width) { 143 this.width = width; 144 } 145 146 @Implementation 147 protected float getStrokeWidth() { 148 return width; 149 } 150 151 @Implementation 152 protected void setShadowLayer(float radius, float dx, float dy, int color) { 153 shadowRadius = radius; 154 shadowDx = dx; 155 shadowDy = dy; 156 shadowColor = color; 157 } 158 159 @Implementation 160 protected Typeface getTypeface() { 161 return typeface; 162 } 163 164 @Implementation 165 protected Typeface setTypeface(Typeface typeface) { 166 this.typeface = typeface; 167 return typeface; 168 } 169 170 @Implementation 171 protected float getTextSize() { 172 return textSize; 173 } 174 175 @Implementation 176 protected void setTextSize(float textSize) { 177 this.textSize = textSize; 178 } 179 180 @Implementation 181 protected void setTextAlign(Paint.Align align) { 182 textAlign = align; 183 } 184 185 @Implementation 186 protected Paint.Align getTextAlign() { 187 return textAlign; 188 } 189 190 /** 191 * @return shadow radius (Paint related shadow, not Robolectric Shadow) 192 */ 193 public float getShadowRadius() { 194 return shadowRadius; 195 } 196 197 /** 198 * @return shadow Dx (Paint related shadow, not Robolectric Shadow) 199 */ 200 public float getShadowDx() { 201 return shadowDx; 202 } 203 204 /** 205 * @return shadow Dx (Paint related shadow, not Robolectric Shadow) 206 */ 207 public float getShadowDy() { 208 return shadowDy; 209 } 210 211 /** 212 * @return shadow color (Paint related shadow, not Robolectric Shadow) 213 */ 214 public int getShadowColor() { 215 return shadowColor; 216 } 217 218 public Paint.Cap getCap() { 219 return cap; 220 } 221 222 public Paint.Join getJoin() { 223 return join; 224 } 225 226 public float getWidth() { 227 return width; 228 } 229 230 @Implementation 231 protected ColorFilter getColorFilter() { 232 return filter; 233 } 234 235 @Implementation 236 protected ColorFilter setColorFilter(ColorFilter filter) { 237 this.filter = filter; 238 return filter; 239 } 240 241 @Implementation 242 protected void setAntiAlias(boolean antiAlias) { 243 this.flags = (flags & ~Paint.ANTI_ALIAS_FLAG) | (antiAlias ? Paint.ANTI_ALIAS_FLAG : 0); 244 } 245 246 @Implementation 247 protected void setDither(boolean dither) { 248 this.dither = dither; 249 } 250 251 @Implementation 252 protected final boolean isDither() { 253 return dither; 254 } 255 256 @Implementation 257 protected final boolean isAntiAlias() { 258 return (flags & Paint.ANTI_ALIAS_FLAG) == Paint.ANTI_ALIAS_FLAG; 259 } 260 261 @Implementation 262 protected PathEffect getPathEffect() { 263 return pathEffect; 264 } 265 266 @Implementation 267 protected PathEffect setPathEffect(PathEffect effect) { 268 this.pathEffect = effect; 269 return effect; 270 } 271 272 @Implementation 273 protected float measureText(String text) { 274 return text.length(); 275 } 276 277 @Implementation 278 protected float measureText(CharSequence text, int start, int end) { 279 return end - start; 280 } 281 282 @Implementation 283 protected float measureText(String text, int start, int end) { 284 return end - start; 285 } 286 287 @Implementation 288 protected float measureText(char[] text, int index, int count) { 289 return count; 290 } 291 } 292