绘制图像时画外的影子 [英] Drawing an outer shadow when drawing an image
问题描述
我目前通过绘制在画布创建一个图像在我的应用程序一个圆形的版本。我想提请图像周围淡淡在外阴影,但我不能完全得到它的权利。我有2个问题: 1.怎样绘制外侧阴影(我只能似乎绘制阴影用斧头或y偏移) 2.如何绘制阴影,使其不具有附加的图像显示的假象。 code:
I currently create a rounded version of an image in my app by drawing to a canvas. I would like to draw a faint outershadow around the image, but I cant quite get it right. I have 2 questions: 1. How can I draw an outer shadow (I can only seem to draw a shadow with a x or y offset) 2. How can I draw the shadow so that it does not have the artifacts shown in the 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);
// This does not achieve the 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 is an example of the effect I am trying to achieve:
推荐答案
在这里,我们去
是啊我还是掏了Nexus S的
Yup I still dig the Nexus S
首先,请停止屏蔽位图的方式,你可以做到这一点,而不需要分配另一个位图
,结账的有关如何绘制圆角(实际上任何形状)图片这个博客帖子。
First of all, please stop masking bitmaps that way, you can accomplish this without allocating another Bitmap
, checkout this blog post about how to draw rounded (and actually any shape) images.
二使用绘制对象
你也许可以弄清楚如何增加你的影子,只是确保它不会裁剪,在18岁以上,你可以使用 ViewOverlay
■对于,也请记住,有有几个不支持的绘图操作的硬件加速层,包括 setShadowLayer
和 BlurMaskFilter
,如果表现不你的问题,你可以禁用它一如既往:
Second using that Drawable
you probably can figure out how to add your shadow, just make sure it does not get clipped, on 18+ you could use ViewOverlay
s for that, also keep in mind that there are several unsupported drawing operations for hardware accelerated layers, that includes setShadowLayer
and BlurMaskFilter
, if performance is not an issue for you, you can disable it as always:
if (SDK_INT >= HONEYCOMB) {
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
和使用 setShadowLayer
因为你已经尝试了:
And use setShadowLayer
as you were trying already:
somePaint.setShadowLayer(shadowSize, deltaX, deltaY, shadowColor);
有关样本请在文末链接。
For a sample please check the link at the end.
如果你仍然想成为硬件加速,你将不得不伪造它在透支的风险,你可以使用径向渐变或绘制另一个椭圆自己模糊它(如前面提到的,不能使用 BlurMaskFilter
)或使用pre-模糊位图
(更多掩模)。
If you still want to be hardware accelerated you will have to fake it at risk of overdrawing, you could use a radial gradient or draw another oval blurring it yourself (as mentioned before can't use BlurMaskFilter
) or use a pre-blurred Bitmap
(more masking).
对于这样一个微妙的影子,我宁愿只是走平,如果需要的性能,完整酱的香蕉立场。
For such a subtle shadow I would rather just go flat if performance is required, the full sauce is in the banana stand.
更新:启动L您可以用真实的阴影
这篇关于绘制图像时画外的影子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!