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