aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/shadows/ShadowImageView.java
blob: 16360ddee9564fa52a65f04f32635815d65b8251 (plain)
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
package com.xtremelabs.robolectric.shadows;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.widget.ImageView;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.res.ResourceLoader;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@Implements(ImageView.class)
public class ShadowImageView extends ShadowView {
    private Drawable imageDrawable;
    private int resourceId;
    private Bitmap imageBitmap;
    private ImageView.ScaleType scaleType;
    private Matrix matrix;
    private int imageLevel;

    @Override
    public void applyAttributes() {
        super.applyAttributes();
        applyImageAttribute();
    }

    @Implementation
    public void setImageBitmap(Bitmap imageBitmap) {
        setImageDrawable(new BitmapDrawable(imageBitmap));
        this.imageBitmap = imageBitmap;
    }

    @Deprecated
    public Bitmap getImageBitmap() {
        return imageBitmap;
    }

    @Implementation
    public void setImageDrawable(Drawable drawable) {
        this.imageDrawable = drawable;
    }

    @Implementation
    public void setImageResource(int resId) {
        this.resourceId = resId;
        setImageDrawable(buildDrawable(resId));
    }

    /**
     * Build drawable, either LayerDrawable or BitmapDrawable.
     *
     * @param resourceId Resource id
     * @return Drawable
     */
    protected Drawable buildDrawable(int resourceId) {
        if (isDrawableXml(resourceId)) {
            ResourceLoader resourceLoader = shadowOf(Robolectric.application).getResourceLoader();
            int[] resourceIds = resourceLoader.getDrawableIds(resourceId);
            Drawable[] drawables = new Drawable[resourceIds.length];

            for (int i = 0; i < resourceIds.length; i++) {
                drawables[i] = buildDrawable(resourceIds[i]);
            }
            if (resourceLoader.isAnimatableXml(resourceId)) {
                AnimationDrawable animationDrawable = new AnimationDrawable();
                for (Drawable drawable : drawables) {
                    animationDrawable.addFrame(drawable, -1);
                }
                return animationDrawable;
            } else {
                LayerDrawable layerDrawable = new LayerDrawable(drawables);
                shadowOf(layerDrawable).setLoadedFromResourceId(resourceId);
                return layerDrawable;
            }
        } else {
            return new BitmapDrawable(BitmapFactory.decodeResource(
                    getResources(), resourceId));
        }
    }

    /**
     * Does the resource id point to xml resource.
     *
     * @param resourceId Resource id
     * @return Boolean
     */
    protected boolean isDrawableXml(int resourceId) {
        return shadowOf(Robolectric.application).getResourceLoader()
                .isDrawableXml(resourceId);
    }

    @Implementation
    public ImageView.ScaleType getScaleType() {
        return scaleType;
    }

    @Implementation
    public void setScaleType(ImageView.ScaleType scaleType) {
        this.scaleType = scaleType;
    }

    @Implementation
    public Drawable getDrawable() {
        return imageDrawable;
    }

    /**
     * @return the image drawable
     * @deprecated Use android.widget.ImageView#getDrawable() instead.
     */
    @Deprecated
    public Drawable getImageDrawable() {
        return imageDrawable;
    }

    @Deprecated
    public int getResourceId() {
        return resourceId;
    }

    @Implementation
    public void setImageMatrix(Matrix matrix) {
        this.matrix = new Matrix(matrix);
    }

    @Implementation
    public void draw(Canvas canvas) {
        if (matrix != null) {
            canvas.translate(shadowOf(matrix).getTransX(), shadowOf(matrix)
                    .getTransY());
            canvas.scale(shadowOf(matrix).getScaleX(), shadowOf(matrix)
                    .getScaleY());
        }
        if (imageDrawable != null) {
            imageDrawable.draw(canvas);
        }
    }

    private void applyImageAttribute() {
        String source = attributeSet.getAttributeValue("android", "src");
        if (source != null) {
            if (source.startsWith("@drawable/")) {
                setImageResource(attributeSet.getAttributeResourceValue(
                        "android", "src", 0));
            }
        }
    }

    @Implementation
    public void setImageLevel(int imageLevel) {
        this.imageLevel = imageLevel;
    }

    /**
     * Non-Android accessor.
     *
     * @return the imageLevel set in {@code setImageLevel(int)}
     */
    public int getImageLevel() {
        return imageLevel;
    }
}