Oval Gradient in Android

前端 未结 4 1212
礼貌的吻别
礼貌的吻别 2020-12-05 19:42

I know how to setup and display an oval shape. I know how to apply a gradient to this shape. What I cant figure out is how I can get an oval gradient to match the shape.

4条回答
  •  清歌不尽
    2020-12-05 20:22

    This is difficult since drawables defined in this fashion draw themselves at runtime, adapting to the space you put them in. Your best solution, if you must do this in code, would be to take the shape drawable you have defined in XML and draw it onto a Canvas or into a Bitmap as a perfect circle. At this point, the gradient pattern will follow the shape outline. Once the shape has been drawn into a static context you can add the shape to a view (as a background, let's say), which will distort it into an oval when it tries to fit the view bounds. Since you have an image, the whole thing will distort proportionately.

    Hopefully, it won't pixel too bad with this method.

提交回复
热议问题