Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

paint - How to prevent Android's drawBitmap from only drawing black images?

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.

alt text

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);

alt text

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Bitmap shadowImage32 = shadowImage.copy(ARGB_8888, true);

after the extractAlpha call, and draw onto that guy instead of the 8-bit shadowImage.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...