Creating a shadow around a canvas drawn shape?

感情迁移 提交于 2019-12-03 11:15:20

问题


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 no success.


回答1:


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



回答2:


  1. create. a Path, add some elements to it

  2. set BlurMaskFilter to a Paint

  3. draw a path with dx, dy shadow offset

  4. unset mask filter

  5. draw a path again with no. offset



来源:https://stackoverflow.com/questions/18099465/creating-a-shadow-around-a-canvas-drawn-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!