GradientDrawable以编程方式更改角度

And*_*Dev 8 android

我为GradientDrawable提供了以下xml.如何以编程方式更改角度?

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:type="linear" 
       android:angle="45" 
       android:startColor="#FF0000" 
       android:endColor="#00FF00" />
</shape>
Run Code Online (Sandbox Code Playgroud)

Tim*_*nen 9

GradientDrawable有一个名为:的方法.setOrientation(GradientDrawable.Orientation orientation),您可以使用此方法更改渐变的方向.您需要将XML扩展为GradientDrawable.

  • 似乎 XML 有一个与 setOrientation 相匹配的限制: https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#attr_android:angle “必须是范围内 45 的倍数[0, 315]。” 。https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.Orientation.html 。所以它们实际上是相同的,但由于某种原因具有不同的名称。 (2认同)