android angle属性,Java GradientDrawable.RECTANGLE属性代码示例

private void initView(Context context, AttributeSet attrs) {

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundLatout);

//显示类型

int shapeTpe = a.getInt(R.styleable.RoundLatout_viewShapeTpe, shapeTypes[0]);

//圆角大小

float cornerRadius = a.getLayoutDimension(R.styleable.RoundLatout_viewCornerRadius, 0);

float topLeftRadius = a.getLayoutDimension(R.styleable.RoundLatout_viewTopLeftRadius, 0);

float topRightRadius = a.getLayoutDimension(R.styleable.RoundLatout_viewTopRightRadius, 0);

float bottomLeftRadius = a.getLayoutDimension(R.styleable.RoundLatout_viewBottomLeftRadius, 0);

float bottomRightRadius = a.getLayoutDimension(R.styleable.RoundLatout_viewBottomRightRadius, 0);

//填充色

int solidColor = a.getColor(R.styleable.RoundLatout_viewSolidColor, 0x0);

//边框

int strokeColor = a.getColor(R.styleable.RoundLatout_viewStrokeColor, 0x0);

int strokeWidth = a.getDimensionPixelSize(R.styleable.RoundLatout_viewStrokeWidth, 0);

int strokeDashWidth = a.getDimensionPixelSize(R.styleable.RoundLatout_viewStrokeDashWidth, 0);

int strokeDashGap = a.getDimensionPixelSize(R.styleable.RoundLatout_viewStrokeDashGap, 0);

a.recycle();

GradientDrawable gd = new GradientDrawable();

gd.setColor(solidColor);

//设置类型

gd.setShape(shapeTypes[shapeTpe]);

//类型为矩形才可设置圆角

if (shapeTypes[shapeTpe] == GradientDrawable.RECTANGLE) {

if (cornerRadius != 0) {

gd.setCornerRadius(cornerRadius);

} else {

gd.setCornerRadii(new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius, bottomRightRadius, bottomRightRadius, bottomLeftRadius, bottomLeftRadius});

}

}

gd.setStroke(strokeWidth, strokeColor, strokeDashWidth, strokeDashGap);

setBackground(gd);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GradientDrawable类是Android中的一个可绘制对象,用于绘制渐变图形。它可以绘制矩形、椭圆形、线条等,还可以设置渐变效果、边框、圆角等属性。 以下是GradientDrawable类的一些常用属性: 1. 形状:可以设置矩形、椭圆形、线条等形状。 2. 渐变效果:可以设置线性渐变、放射性渐变、扫描性渐变等。 3. 颜色:可以设置渐变颜色、填充颜色、描边颜色等。 4. 边框:可以设置边框的粗细、颜色等属性。 5. 圆角:可以设置圆角的大小。 使用GradientDrawable类创建一个渐变背景,可以通过以下步骤: 1. 创建GradientDrawable对象。 2. 调用setShape()方法设置图形形状。 3. 调用setColor()方法设置填充颜色。 4. 调用setStroke()方法设置边框。 5. 调用setCornerRadius()方法设置圆角。 6. 调用setGradientType()方法设置渐变类型。 7. 调用setGradientCenter()方法设置渐变中心点。 8. 调用setGradientRadius()方法设置渐变半径。 9. 调用setColors()方法设置渐变颜色。 10. 将GradientDrawable对象设置为View的背景。 例如,以下代码可以创建一个渐变背景: ``` GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColor(Color.WHITE); gradientDrawable.setStroke(2, Color.BLACK); gradientDrawable.setCornerRadius(10); gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); gradientDrawable.setGradientCenter(0.5f, 0.5f); gradientDrawable.setGradientRadius(100); gradientDrawable.setColors(new int[]{Color.RED, Color.BLUE}); button.setBackground(gradientDrawable); ``` 这将创建一个矩形背景,填充颜色为白色,描边为黑色,圆角为10px,渐变类型为线性渐变,渐变中心点为控件中心,渐变半径为100px,渐变颜色为红色和蓝色。最后将GradientDrawable对象设置为Button的背景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值