@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)); } }