private static Bitmap createHardwareBitmap(int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Config.HARDWARE); ShadowBitmap shadowBitmap = Shadow.extract(bitmap); shadowBitmap.setMutable(false); return bitmap; } }
@Implementation protected static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) { if (colors.length != width * height) { throw new IllegalArgumentException("array length (" + colors.length + ") did not match width * height (" + (width * height) + ")"); } Bitmap newBitmap = Bitmap.createBitmap(width, height, config); ShadowBitmap shadowBitmap = Shadow.extract(newBitmap); shadowBitmap.setMutable(false); shadowBitmap.createdFromColors = colors; shadowBitmap.colors = new int[colors.length]; System.arraycopy(colors, 0, shadowBitmap.colors, 0, colors.length); return newBitmap; }
@NonNull @Override public Bitmap obtain(int width, int height, Bitmap.Config config) { Bitmap result = Bitmap.createBitmap(width, height, config); Shadows.shadowOf(result).setMutable(true); return result; }
private Bitmap createMutableBitmap(Bitmap.Config config) { Bitmap bitmap = Bitmap.createBitmap(100, 100, config); Shadows.shadowOf(bitmap).setMutable(true); return bitmap; }
@Implementation public static Bitmap createBitmap(int width, int height, Bitmap.Config config) { Bitmap bitmap = ShadowBitmap.createBitmap(width, height, config); Shadows.shadowOf(bitmap).setMutable(true); return bitmap; } }
@Test public void testImmutableBitmapsAreNotAdded() { Bitmap bitmap = createMutableBitmap(); Shadows.shadowOf(bitmap).setMutable(false); pool.put(bitmap); assertThat(strategy.bitmaps).isEmpty(); }
@Test public void shouldSetPixel_allocateOnTheFly() { Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); shadowOf(bitmap).setMutable(true); bitmap.setPixel(0, 0, 2); assertThat(bitmap.getPixel(0, 0)).isEqualTo(2); assertThat(shadowOf(bitmap).getCreatedFromColors()).isNull(); }
@Test public void shouldSetPixel() { Bitmap bitmap = Bitmap.createBitmap(new int[] { 1 }, 1, 1, Bitmap.Config.ARGB_8888); shadowOf(bitmap).setMutable(true); bitmap.setPixel(0, 0, 2); assertThat(bitmap.getPixel(0, 0)).isEqualTo(2); assertThat(shadowOf(bitmap).getCreatedFromColors()).isEqualTo(new int[] { 1 }); }
@Implementation(minSdk = JELLY_BEAN_MR1) protected static Bitmap createBitmap( DisplayMetrics displayMetrics, int width, int height, Bitmap.Config config) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } Bitmap scaledBitmap = ReflectionHelpers.callConstructor(Bitmap.class); ShadowBitmap shadowBitmap = Shadow.extract(scaledBitmap); shadowBitmap.setDescription("Bitmap (" + width + " x " + height + ")"); shadowBitmap.width = width; shadowBitmap.height = height; shadowBitmap.config = config; shadowBitmap.setMutable(true); if (displayMetrics != null) { shadowBitmap.density = displayMetrics.densityDpi; } shadowBitmap.setPixels(new int[shadowBitmap.getHeight() * shadowBitmap.getWidth()], 0, shadowBitmap.getWidth(), 0, 0, shadowBitmap.getWidth(), shadowBitmap.getHeight()); return scaledBitmap; }
@Implementation public static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) { if (colors.length != width * height) { throw new IllegalArgumentException("array length (" + colors.length + ") did not match width * height (" + (width * height) + ")"); } Bitmap newBitmap = Bitmap.createBitmap(width, height, config); ShadowBitmap shadowBitmap = shadowOf(newBitmap); shadowBitmap.setMutable(false); shadowBitmap.createdFromColors = colors; shadowBitmap.colors = new int[colors.length]; System.arraycopy(colors, 0, shadowBitmap.colors, 0, colors.length); return newBitmap; }
@Implementation public static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) { if (colors.length != width * height) { throw new IllegalArgumentException("array length (" + colors.length + ") did not match width * height (" + (width * height) + ")"); } Bitmap newBitmap = Bitmap.createBitmap(width, height, config); ShadowBitmap shadowBitmap = shadowOf(newBitmap); shadowBitmap.setMutable(false); shadowBitmap.createdFromColors = colors; shadowBitmap.colors = new int[colors.length]; System.arraycopy(colors, 0, shadowBitmap.colors, 0, colors.length); return newBitmap; }
@Implementation public static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) { if (colors.length != width * height) { throw new IllegalArgumentException("array length (" + colors.length + ") did not match width * height (" + (width * height) + ")"); } Bitmap newBitmap = Bitmap.createBitmap(width, height, config); ShadowBitmap shadowBitmap = shadowOf(newBitmap); shadowBitmap.setMutable(false); shadowBitmap.createdFromColors = colors; shadowBitmap.colors = new int[colors.length]; System.arraycopy(colors, 0, shadowBitmap.colors, 0, colors.length); return newBitmap; }
@Implementation protected static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config) { if (colors.length != width * height) { throw new IllegalArgumentException("array length (" + colors.length + ") did not match width * height (" + (width * height) + ")"); } Bitmap newBitmap = Bitmap.createBitmap(width, height, config); ShadowBitmap shadowBitmap = Shadow.extract(newBitmap); shadowBitmap.setMutable(false); shadowBitmap.createdFromColors = colors; shadowBitmap.colors = new int[colors.length]; System.arraycopy(colors, 0, shadowBitmap.colors, 0, colors.length); return newBitmap; }
@Implementation(minSdk = O) protected static Bitmap createHardwareBitmap(RenderNode node, int width, int height) { Bitmap bitmap = Bitmap.createBitmap(width, height, Config.HARDWARE); ShadowBitmap shadowBitmap = Shadow.extract(bitmap); shadowBitmap.setMutable(false); return bitmap; } }
@Implementation public static Bitmap createBitmap(DisplayMetrics displayMetrics, int width, int height, Bitmap.Config config) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } Bitmap scaledBitmap = ReflectionHelpers.callConstructor(Bitmap.class); ShadowBitmap shadowBitmap = shadowOf(scaledBitmap); shadowBitmap.setDescription("Bitmap (" + width + " x " + height + ")"); shadowBitmap.width = width; shadowBitmap.height = height; shadowBitmap.config = config; shadowBitmap.setMutable(true); if (displayMetrics != null) { shadowBitmap.density = displayMetrics.densityDpi; } shadowBitmap.setPixels(new int[shadowBitmap.getHeight() * shadowBitmap.getWidth()], 0, shadowBitmap.getWidth(), 0, 0, shadowBitmap.getWidth(), shadowBitmap.getHeight()); return scaledBitmap; }
@Implementation(minSdk = JELLY_BEAN_MR1) protected static Bitmap createBitmap( DisplayMetrics displayMetrics, int width, int height, Bitmap.Config config) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } Bitmap scaledBitmap = ReflectionHelpers.callConstructor(Bitmap.class); ShadowBitmap shadowBitmap = Shadow.extract(scaledBitmap); shadowBitmap.setDescription("Bitmap (" + width + " x " + height + ")"); shadowBitmap.width = width; shadowBitmap.height = height; shadowBitmap.config = config; shadowBitmap.setMutable(true); if (displayMetrics != null) { shadowBitmap.density = displayMetrics.densityDpi; } shadowBitmap.setPixels(new int[shadowBitmap.getHeight() * shadowBitmap.getWidth()], 0, shadowBitmap.getWidth(), 0, 0, shadowBitmap.getWidth(), shadowBitmap.getHeight()); return scaledBitmap; }
@Implementation public static Bitmap createBitmap(DisplayMetrics displayMetrics, int width, int height, Bitmap.Config config) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } Bitmap scaledBitmap = ReflectionHelpers.callConstructor(Bitmap.class); ShadowBitmap shadowBitmap = shadowOf(scaledBitmap); shadowBitmap.setDescription("Bitmap (" + width + " x " + height + ")"); shadowBitmap.width = width; shadowBitmap.height = height; shadowBitmap.config = config; shadowBitmap.setMutable(true); if (displayMetrics != null) { shadowBitmap.density = displayMetrics.densityDpi; } shadowBitmap.setPixels(new int[shadowBitmap.getHeight() * shadowBitmap.getWidth()], 0, shadowBitmap.getWidth(), 0, 0, shadowBitmap.getWidth(), shadowBitmap.getHeight()); return scaledBitmap; }
@Implementation public static Bitmap createBitmap(DisplayMetrics displayMetrics, int width, int height, Bitmap.Config config) { if (width <= 0 || height <= 0) { throw new IllegalArgumentException("width and height must be > 0"); } Bitmap scaledBitmap = ReflectionHelpers.callConstructor(Bitmap.class); ShadowBitmap shadowBitmap = shadowOf(scaledBitmap); shadowBitmap.setDescription("Bitmap (" + width + " x " + height + ")"); shadowBitmap.width = width; shadowBitmap.height = height; shadowBitmap.config = config; shadowBitmap.setMutable(true); if (displayMetrics != null) { shadowBitmap.density = displayMetrics.densityDpi; } shadowBitmap.setPixels(new int[shadowBitmap.getHeight() * shadowBitmap.getWidth()], 0, shadowBitmap.getWidth(), 0, 0, shadowBitmap.getWidth(), shadowBitmap.getHeight()); return scaledBitmap; }