Circular gradient in android

后端 未结 7 918
灰色年华
灰色年华 2020-12-04 06:30

I\'m trying to make a gradient that emits from the middle of the screen in white, and turns to black as it moves toward the edges of the screen.

As I make a \"normal

7条回答
  •  再見小時候
    2020-12-04 07:19

    I always find images helpful when learning a new concept, so this is a supplemental answer.

    The %p means a percentage of the parent, that is, a percentage of the narrowest dimension of whatever view we set our drawable on. The images above were generated by changing the gradientRadius in this code

    my_gradient_drawable

    
    
        
    
    

    Which can be set on a view's background attribute like this

    
    

    Center

    You can change the center of the radius with

    android:centerX="0.2"
    android:centerY="0.7"
    

    where the decimals are fractions of the width and height for x and y respectively.

    Documentation

    Here are some notes from the documentation explaining things a little more.

    android:gradientRadius

    Radius of the gradient, used only with radial gradient. May be an explicit dimension or a fractional value relative to the shape's minimum dimension.

    May be a floating point value, such as "1.2".

    May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), and mm (millimeters).

    May be a fractional value, which is a floating point number appended with either % or %p, such as "14.5%". The % suffix always means a percentage of the base size; the optional %p suffix provides a size relative to some parent container.

提交回复
热议问题