GradientDrawable 渐变背景使用

本文详细介绍了GradientDrawable在Android中如何用于动态设置背景,包括其支持的形状、构造函数、渐变色效果走向及渐变模式。GradientDrawable提供矩形、圆形、线、环形等形状,以及线性、放射性、扇形渐变模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GradientDrawable  为动态设置背景    对应 的xml 为shape静态设置

一  支持的形状和shape 一样

   设置方法: public void setShape(int shape)


/**
     * Shape is a rectangle, possibly with rounded corners
     */矩形
    public static final int RECTANGLE = 0;

    /**
     * Shape is an ellipse
     */圆形
    public static final int OVAL = 1;

    /**
     * Shape is a line
     */线
    public static final int LINE = 2;

    /**
     * Shape is a ring.
     */环形
    public static final int RING = 3;

二 构造函数  2个

    public GradientDrawable() {
        this(new GradientState(Orientation.TOP_BOTTOM, null), null);
    }

    /**
     * Create a new gradient drawable given an orientation and an array
     * of colors for the gradient.
     */
    public GradientDrawable(Orientation orientation, @ColorInt int[] colors) {
        this(new GradientState(orientation, colors), null);
    }

构造函数一 有默认渐变走向

构造函数二    参数一:渐变色效果走向    参数二 :渐变颜色数组

渐变走向8种枚举为

 public enum Orientation {
        /** draw the gradient from the top to the bottom */
        TOP_BOTTOM,
        /** draw the gradient from the top-right to the bottom-left */
        TR_BL,
        /** draw the gradient from the right to the left */
        RIGHT_LEFT,
        /** draw the gradient from the bottom-right to the top-left */
        BR_TL,
        /** draw the gradient from the bottom to the top */
        BOTTOM_TOP,
        /** draw the gradient from the bottom-left to the top-right */
        BL_TR,
        /** draw the gradient from the left to the right */
        LEFT_RIGHT,
        /** draw the gradient from the top-left to the bottom-right */
        TL_BR,
    }

三 渐变模式三种

 public void setGradientType(int gradient)

1 linear : 线性渐变

2 radial:A radial gradient. 放射性渐变(圆形渐变),起始颜色从cenralX,centralY点开始。

3 sweep:A sweeping line gradient. 扫描式渐变(扇形渐变)。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值