@Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int width = source.getWidth(); int height = source.getHeight(); Bitmap.Config config = source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888; Bitmap bitmap = mBitmapPool.get(width, height, config); if (bitmap == null) { bitmap = Bitmap.createBitmap(width, height, config); } Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); canvas.drawRoundRect( new RectF(margin, margin, width - margin, height - margin), radius, radius, paint); source.recycle(); return BitmapResource.obtain(bitmap, mBitmapPool); }
@Test public void testDoesNotPutNullBitmapAcquiredFromPool() { when(pool.get(anyInt(), anyInt(), any(Bitmap.Config.class))).thenReturn(null); fitCenter.transform(resource, 100, 100); verify(pool, never()).put(any(Bitmap.class)); }
private Bitmap roungTransForm(BitmapPool pool, Bitmap source, int outWidth, int outHeight) { if (source == null) return null; Bitmap result = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader( new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, outWidth, outHeight); canvas.drawRoundRect(rectF, radius, radius, paint); return result; }
@Override public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { Bitmap source = resource.get(); int size = Math.min(source.getWidth(), source.getHeight()); mWidth = (source.getWidth() - size) / 2; mHeight = (source.getHeight() - size) / 2; Bitmap.Config config = source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888; Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config); if (bitmap == null) { bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size); } return BitmapResource.obtain(bitmap, mBitmapPool); }
@Test public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse); toReuse.setHasAlpha(false); toTransform.setHasAlpha(true); Bitmap result = TransformationUtils.centerCrop( bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertTrue(result.hasAlpha()); }
private Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) { return null; } int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader( new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); return result; }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(bitmapPool.get(anyInt(), anyInt(), any(Bitmap.Config.class))) .thenAnswer(new Util.CreateBitmap()); }