【发布时间】:2013-07-22 09:00:28
【问题描述】:
我目前通过在画布上绘制来在我的应用程序中创建一个圆形版本的图像。我想在图像周围画一个微弱的外阴影,但我不能完全正确。我有两个问题: 1.如何绘制外阴影(我似乎只能绘制带有x或y偏移的阴影) 2.如何绘制阴影,使其没有附图中显示的伪影。代码:
![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]
这是我想要达到的效果的一个例子:
【问题讨论】:
-
@Leonidos 我现在附上了一个例子。您会注意到图像周围有一个微弱的外阴影。