ShadowImageView.java revision 73d4a15f75e5175c3b07de348834dd2d6684aae5
1package com.xtremelabs.robolectric.shadows;
2
3import android.graphics.Bitmap;
4import android.graphics.BitmapFactory;
5import android.graphics.Canvas;
6import android.graphics.Matrix;
7import android.graphics.drawable.BitmapDrawable;
8import android.graphics.drawable.Drawable;
9import android.graphics.drawable.LayerDrawable;
10import android.widget.ImageView;
11
12import com.xtremelabs.robolectric.Robolectric;
13import com.xtremelabs.robolectric.internal.Implementation;
14import com.xtremelabs.robolectric.internal.Implements;
15
16import static com.xtremelabs.robolectric.Robolectric.shadowOf;
17
18@Implements(ImageView.class)
19public class ShadowImageView extends ShadowView {
20    private Drawable imageDrawable;
21    private int alpha;
22    private int resourceId;
23    private Bitmap imageBitmap;
24    private ImageView.ScaleType scaleType;
25    private Matrix matrix;
26
27    @Override
28    public void applyAttributes() {
29        super.applyAttributes();
30        applyImageAttribute();
31    }
32
33    @Implementation
34    public void setImageBitmap(Bitmap imageBitmap) {
35        setImageDrawable(new BitmapDrawable(imageBitmap));
36        this.imageBitmap = imageBitmap;
37    }
38
39    @Deprecated
40    public Bitmap getImageBitmap() {
41        return imageBitmap;
42    }
43
44    @Implementation
45    public void setImageDrawable(Drawable drawable) {
46        this.imageDrawable = drawable;
47    }
48
49    @Implementation
50    public void setImageResource(int resId) {
51        this.resourceId = resId;
52        setImageDrawable(buildDrawable(resId));
53    }
54
55    /**
56     * Build drawable, either LayerDrawable or BitmapDrawable.
57     *
58     * @param resourceId
59     *            Resource id
60     * @return Drawable
61     */
62    protected Drawable buildDrawable(int resourceId) {
63        if (isDrawableXml(resourceId)) {
64            int[] resourceIds = shadowOf(Robolectric.application)
65                    .getResourceLoader().getDrawableIds(resourceId);
66
67            Drawable[] drawables = new Drawable[resourceIds.length];
68
69            for (int i = 0; i < resourceIds.length; i++) {
70                drawables[i] = buildDrawable(resourceIds[i]);
71            }
72
73            return new LayerDrawable(drawables);
74        } else {
75            return new BitmapDrawable(BitmapFactory.decodeResource(
76                    getResources(), resourceId));
77        }
78    }
79
80    /**
81     * Does the resource id point to xml resource.
82     *
83     * @param resourceId
84     *            Resource id
85     * @return Boolean
86     */
87    private boolean isDrawableXml(int resourceId) {
88        return shadowOf(Robolectric.application).getResourceLoader()
89                .isDrawableXml(resourceId);
90    }
91
92    @Implementation
93    public void setAlpha(int alpha) {
94        this.alpha = alpha;
95    }
96
97    @Implementation
98    public ImageView.ScaleType getScaleType() {
99        return scaleType;
100    }
101
102    @Implementation
103    public void setScaleType(ImageView.ScaleType scaleType) {
104        this.scaleType = scaleType;
105    }
106
107    @Implementation
108    public Drawable getDrawable() {
109        return imageDrawable;
110    }
111
112    /**
113     * @return the image drawable
114     * @deprecated Use android.widget.ImageView#getDrawable() instead.
115     */
116    @Deprecated
117    public Drawable getImageDrawable() {
118        return imageDrawable;
119    }
120
121    public int getAlpha() {
122        return alpha;
123    }
124
125    @Deprecated
126    public int getResourceId() {
127        return resourceId;
128    }
129
130    @Implementation
131    public void setImageMatrix(Matrix matrix) {
132        this.matrix = new Matrix(matrix);
133    }
134
135    @Implementation
136    public void draw(Canvas canvas) {
137        if (matrix != null) {
138            canvas.translate(shadowOf(matrix).getTransX(), shadowOf(matrix)
139                    .getTransY());
140            canvas.scale(shadowOf(matrix).getScaleX(), shadowOf(matrix)
141                    .getScaleY());
142        }
143        imageDrawable.draw(canvas);
144    }
145
146    private void applyImageAttribute() {
147        String source = attributeSet.getAttributeValue("android", "src");
148        if (source != null) {
149            if (source.startsWith("@drawable/")) {
150                setImageResource(attributeSet.getAttributeResourceValue(
151                        "android", "src", 0));
152            }
153        }
154    }
155}
156