Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent Android's drawBitmap from only drawing black images?
    primarykey
    data
    text
    <p>As per the <a href="https://stackoverflow.com/questions/3567312/android-drop-shadow-on-view/3568992#3568992">original question</a>, The end result is a rounded-rect <code>png</code> in an <code>ImageView</code> with a natural looking drop shadow.</p> <p>I have the shadow working, but when it draws, it makes the entire image black.</p> <p><img src="https://i.stack.imgur.com/kZJF4.png" alt="alt text"></p> <p>How can I prevent the original image (definitely not black) from being black when adding the shadow?</p> <pre><code> 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); </code></pre> <p><br/> <br/> <br/> <br/> <strong>UPDATE:</strong></p> <p>I implemented Josh's suggestion about copying over to the correct color space and now it works great! For future searchers, <strong>this code produces a drop shadow on an image view</strong>. You can play around with the x and y, as well as the OUTER constant to get the desired effect.</p> <pre><code>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); </code></pre> <p><img src="https://i.stack.imgur.com/bzBZW.png" alt="alt text"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload