Android - how to draw 2-directional gradient?

微笑、不失礼 提交于 2019-12-02 08:23:44

问题


I started playing with gradients and I've found it's pretty easy to draw 1-directional gradient (like from top to bottom, from left to right, or in diagonale...) but how to draw 2-directional gradient? I mean something like this:

Big blue rectangle it's 2-directional gradient - on top right corner there is blue and to the left its transforming to white and to the bottom it's transforming to the black. How to draw this?


回答1:


Answer is: you must combine 2 different LinearGradients, for example:

LinearGradient val = new LinearGradient(0, 0, 0, height, Color.WHITE, Color.BLACK, TileMode.CLAMP);
                LinearGradient sat = new LinearGradient(0, 0, width, 0, Color.WHITE, Color.HSVToColor(hsvCopy), TileMode.CLAMP);
                ComposeShader merged = new ComposeShader(val, sat, PorterDuff.Mode.MULTIPLY)

;

And of course important:

[view with this background].setLayerType(View.LAYER_TYPE_SOFTWARE, null);

on phones with 3.0 android and higher




回答2:


You can do something like this when you create your gradient color:

 <gradient
    android:centerColor="@color/my_white"
    android:startColor="@color/my_darker_gray"
    android:endColor="@color/my_darker_gray"
    android:angle="45"/>

Specify the color you desire for the start/end/center location of the gradient, and specify the angle of the color switching.



来源:https://stackoverflow.com/questions/15878769/android-how-to-draw-2-directional-gradient

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