如何prevent Android的仅绘制黑色图像drawBitmap? [英] How to prevent Android's drawBitmap from only drawing black images?

查看:199
本文介绍了如何prevent Android的仅绘制黑色图像drawBitmap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于每<一href="http://stackoverflow.com/questions/3567312/android-drop-shadow-on-view/3568992#3568992">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.

这篇关于如何prevent Android的仅绘制黑色图像drawBitmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆