android - Download image from URL, show it as a circular image with default behaviour -
after stucking few days on problem wanted share solution , save time.
problem
1. download image url
2. show circular image (if download succeeded)
3. or show default image in same imageview
(if download failed)
[edit] removed answer because of useful hints coming @budius , better working solution offers below
best way achieve task question using picasso circular transformation.
like following code:
picasso .with(context) .load(url) .placeholder(placeholderdrawable) .error(errordrawable) .transform(circletransform) .into(imageview);
and code circletransform
copied link: https://gist.github.com/julianshen/5829333
public class circletransform implements transformation { @override public bitmap transform(bitmap source) { int size = math.min(source.getwidth(), source.getheight()); int x = (source.getwidth() - size) / 2; int y = (source.getheight() - size) / 2; bitmap squaredbitmap = bitmap.createbitmap(source, x, y, size, size); if (squaredbitmap != source) { source.recycle(); } bitmap bitmap = bitmap.createbitmap(size, size, source.getconfig()); canvas canvas = new canvas(bitmap); paint paint = new paint(); bitmapshader shader = new bitmapshader(squaredbitmap, bitmapshader.tilemode.clamp, bitmapshader.tilemode.clamp); paint.setshader(shader); paint.setantialias(true); float r = size/2f; canvas.drawcircle(r, r, r, paint); squaredbitmap.recycle(); return bitmap; } @override public string key() { return "circle"; } }
Comments
Post a Comment