Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im trying to reproduce the following gradient programmatically.

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="@color/startcolor" 
        android:centerColor="#343434"
        android:endColor="#00000000"
        android:type="radial"
        android:gradientRadius="140"
        android:centerY="45%"
     />
    <corners android:radius="0dp" />
</shape>

How can I set programmatically the paramether? Thanks

        android:centerY="45%"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
435 views
Welcome To Ask or Share your Answers For Others

1 Answer

http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

To set that specific parameter (I'm assuming a centerX value as you haven't specified one):

yourGradientDrawable.setGradientCenter(1.0f,  0.45f);

So to create the above gradient (except with different colors) programatically:

GradientDrawable g = new GradientDrawable(Orientation.TL_BR, new int[] { getResources().getColor(R.color.startcolor), Color.rgb(255, 0, 0), Color.BLUE });
g.setGradientType(GradientDrawable.RADIAL_GRADIENT);
g.setGradientRadius(140.0f);
g.setGradientCenter(0.0f, 0.45f);

Note: The orientation is ignored for a radial gradient but is needed for the constructor that takes colors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...