如何防止Android的drawBitmap只绘制黑色图像?
根据原始问题,最终结果是一个圆角矩形ImageView
中的 png
具有自然的投影效果。
我有阴影,但当它绘制时,它会使整个图像变黑。
添加阴影时如何防止原始图像(绝对不是黑色)变成黑色?
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);
更新:
我实施了 Josh 关于复制到正确色彩空间的建议,现在效果很好!对于未来的搜索者,此代码会在图像视图上生成阴影。您可以使用 x 和 y 以及 OUTER 常量来获得所需的效果。
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);
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.
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:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在你的上一个问题中发表了评论,但又是这样。
问题可能是您正在将 32 位图像(原始图像)绘制到 8 位图像(提取的 ShadowImage)上。如果是这种情况,请
在 extractAlpha 调用之后执行类似操作,并绘制到那个家伙而不是 8 位shadowImage 上。
I commented in your last question, but here it is again.
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
after the extractAlpha call, and draw onto that guy instead of the 8-bit shadowImage.
您必须绘制阴影图像和原始图像。阴影图像仅包含原始图像的 Alpha 值,而不包含实际的彩色像素。
You have to draw both the shadow image and the original image. The shadow image only contains the alpha values of the original image, not the actual colored pixels.