android - Drawing an outer shadow when drawing an image -
i create rounded version of image in app drawing canvas. draw faint outershadow around image, cant quite right. have 2 questions: 1. how can draw outer shadow (i can seem draw shadow x or y offset) 2. how can draw shadow not have artifacts shown in attached image. code:
![public bitmap getroundedcornerbitmap(bitmap bitmap, float cornerradius) { bitmap output = bitmap.createbitmap(bitmap.getwidth()+6, bitmap.getheight() +6, config.argb_8888); canvas canvas = new canvas(output); final int color = 0xff424242; int shadowradius = getdipsfrompixel(3); final rect imagerect = new rect(shadowradius, shadowradius, bitmap.getwidth(), bitmap.getheight()); final rectf rectf = new rectf(imagerect); // not achieve desired effect paint shadowpaint = new paint(); shadowpaint.setantialias(true); shadowpaint.setcolor(color.black); shadowpaint.setshadowlayer((float)shadowradius, 2.0f, 2.0f,color.black); canvas.drawoval(rectf, shadowpaint); canvas.drawargb(0, 0, 0, 0); final paint paint = new paint(); paint.setantialias(true); paint.setcolor(color); canvas.drawroundrect(rectf, cornerradius, cornerradius, paint); paint.setxfermode(new porterduffxfermode(mode.src_in)); canvas.drawbitmap(bitmap, imagerect, imagerect, paint); return output; }][1]
this example of effect trying achieve:
here go
yup still dig nexus s
first of all, please stop masking bitmaps way, can accomplish without allocating bitmap
, checkout this blog post how draw rounded (and shape) images.
second using drawable
can figure out how add shadow, make sure not clipped, on 18+ use viewoverlay
s that, keep in mind there several unsupported drawing operations hardware accelerated layers, includes setshadowlayer
, blurmaskfilter
, if performance not issue you, can disable always:
if (sdk_int >= honeycomb) { view.setlayertype(view.layer_type_software, null); }
and use setshadowlayer
trying already:
somepaint.setshadowlayer(shadowsize, deltax, deltay, shadowcolor);
for sample please check link @ end.
if still want hardware accelerated have fake @ risk of overdrawing, use radial gradient or draw oval blurring (as mentioned before can't use blurmaskfilter
) or use pre-blurred bitmap
(more masking).
for such subtle shadow rather go flat if performance required, the full sauce in banana stand.
update: starting l can use real shadows.
Comments
Post a Comment