Creating a shadow around a canvas drawn shape?

前端 未结 3 877
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 17:44

What steps are required to create a shape e.g. rectangle with a shadow from scratch using a Canvas?

Adding a shadow layer to the paint used to draw the rectangle yielded

3条回答
  •  灰色年华
    2021-02-01 18:11

    No need for a Bitmap, just needed to set the layer type to LAYER_TYPE_SOFTWARE the original approach worked.

    public class TestShapeShadow extends View
    {
        Paint paint;
    
        public TestShapeShadow(Context context)
        {
           super(context);  
    
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setShadowLayer(12, 0, 0, Color.YELLOW);
    
            // Important for certain APIs 
            setLayerType(LAYER_TYPE_SOFTWARE, paint);
        }
    
        @Override
        protected void onDraw(Canvas canvas)
        {   
            canvas.drawRect(20, 20, 100, 100, paint);
        }
    }
    

提交回复
热议问题