由于每original问题,最终的结果是一个圆角矩形 png格式
在的ImageView
与自然的阴影
As per the original question, The end result is a rounded-rect png
in an ImageView
with a natural looking drop shadow.
我有阴影的工作,但是当它吸引,它使整个图像变黑。
I have the shadow working, but when it draws, it makes the entire image black.
如何从添加阴影,当被黑prevent原始图像(绝对不是黑色)?
How can I prevent the original image (definitely not black) from being black when adding the shadow?
BlurMaskFilter blurFilter = new BlurMaskFilter(2, BlurMaskFilter.Blur.OUTER);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(blurFilter);
int[] offsetXY = new int[2];
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pier_t);
Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);
Canvas c = new Canvas(shadowImage);
c.drawBitmap(originalBitmap, -offsetXY[0], -offsetXY[1], null);
imageView.setImageBitmap(shadowImage);
更新:
UPDATE:
我实现了约什的建议,有关通过复制到正确的色彩空间,现在,它的伟大工程!对于未来的搜索,这code产生的图像视图阴影。可以玩的x和y,以及所述外常数以获得所需的效果。
I implemented Josh's suggestion about copying over to the correct color space and now it works great! For future searchers, this code produces a drop shadow on an image view. You can play around with the x and y, as well as the OUTER constant to get the desired effect.
BlurMaskFilter blurFilter = new BlurMaskFilter(2, BlurMaskFilter.Blur.OUTER);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(blurFilter);
int[] offsetXY = new int[2];
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pier_t);
Bitmap shadowImage = originalBitmap.extractAlpha(shadowPaint, offsetXY);
Bitmap shadowImage32 = shadowImage.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(shadowImage32);
c.drawBitmap(originalBitmap, -offsetXY[0], -offsetXY[1], null);
imageView.setImageBitmap(shadowImage32);
推荐答案
我在评论你的最后一个问题,但在这里它了。
I commented in your last question, but here it is again.
的问题可能是,你正在绘制的32位图像(原始)上的8位图像(所提取的ShadowImage)。如果是这样的话,做一些像
The problem might be that you're drawing a 32-bit image (the original) onto an 8-bit image (the extracted shadowImage). If that's the case, do something like
Bitmap shadowImage32 = shadowImage.copy(ARGB_8888, true);
在extractAlpha通话后,并绘制到那个家伙,而不是8位的ShadowImage。
after the extractAlpha call, and draw onto that guy instead of the 8-bit shadowImage.
相关推荐
最新文章