Android告别使用shape标签,自定义实现圆角、背景色、描边Button

| void | setOrientation(GradientDrawable.Orientation orientation)设置此drawable中定义的渐变的方向。 |

| void | setShape(int shape)设置用于绘制渐变的形状的类型。 |

| void | setSize(int width, int height)设置由该drawable绘制的形状的大小。 |

| void | setStroke(int width, ColorStateList colorStateList)设置绘图的笔触宽度和颜色状态列表。 |

| void | setStroke(int width, ColorStateList colorStateList, float dashWidth, float dashGap)设置绘图的笔触宽度和颜色状态列表。 |

| void | setStroke(int width, int color, float dashWidth, float dashGap)设置绘图的笔触宽度和颜色。 |

| void | setStroke(int width, int color)设置绘图的笔触宽度和颜色。 |

| void | setTintList(ColorStateList tint)指定该drawable的色彩颜色作为颜色状态列表。 |

| void | setTintMode(PorterDuff.Mode tintMode)指定该drawable的色调混合模式。 |

| void | setUseLevel(boolean useLevel)设置这个drawable是否会遵守它的 level属性。 |

更多用法请查看官方API:www.apiref.com/android-zh/…\

如果我们想更加方便的自定义配置圆角值等功能,需要继承GradientDrawable,关于自定义view的流程想必大家都熟悉这里不再详细说明,代码如下:

/**

  • @author xiaoman

  • res/drawable 中的shape文件动态设置

*/

public class RoundButtonDrawable extends GradientDrawable {

private int mStrokeWidth = 0;

/**

  • 设置描边宽度和颜色

*/

void setStrokeData(int width, int color) {

mStrokeWidth = width;

setStroke(width, color);

}

void setStrokeColor(int color){

setStrokeData(mStrokeWidth, color);

}

static RoundButtonDrawable fromAttrSet(Context context, AttributeSet attrs, int defStyleAttr) {

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundButton, defStyleAttr, 0);

int bgColor = typedArray.getColor(R.styleable.RoundButton_bgColor, ContextCompat.getColor(context,R.color.white));

int mRadius = typedArray.getDimensionPixelSize(R.styleable.RoundButton_radius, 0);

int mTopLeftRadius = typedArray.getDimensionPixelSize(R.styleable.RoundButton_topLeftRadius, 0);

int mTopRightRadius = typedArray.getDimensionPixelSize(R.styleable.RoundButton_topRightRadius, 0);

int mBottomLeftRadius = typedArray.getDimensionPixelSize(R.styleable.RoundButton_bottomLeftRadius, 0);

int mBottomRightRadius = typedArray.getDimensionPixelSize(R.styleable.RoundButton_bottomRightRadius, 0);

int strokeColor = typedArray.getColor(R.styleable.RoundButton_strokeColor,ContextCompat.getColor(context,R.color.white));

int strokeWidth = typedArray.getDimensionPixelSize(R.styleable.RoundButton_strokeWidth, 0);

typedArray.recycle();

RoundButtonDrawable roundButtonDrawable = new RoundButtonDrawable();

//设置背景颜色

roundButtonDrawable.setColor(bgColor);

//优先设置指定的圆角

if (mTopLeftRadius > 0 || mTopRightRadius > 0 || mBottomLeftRadius > 0 || mBottomRightRadius > 0) {

float[] radii = new float[]{

mTopLeftRadius, mTopLeftRadius,

mTopRightRadius, mTopRightRadius,

mBottomRightRadius, mBottomRightRadius,

mBottomLeftRadius, mBottomLeftRadius

};

roundButtonDrawable.setCornerRadii(radii);

} else {

roundButtonDrawable.setCornerRadius(mRadius);

}

//设置描边的宽度和颜色

roundButtonDrawable.setStrokeData(strokeWidth, strokeColor);

return roundButtonDrawable;

}

}

attr代码如下:

<?xml version="1.0" encoding="utf-8"?>

2、自定义Button,设置GradientDrawable为背景

因为GradientDrawable是shape标签的具体代码实现,所以,如果我们想通过GradientDrawable来实现圆角等功能需求的话,需要把上一步骤中我们自定义的GradientDrawable来作为button的background,具体代码如下:

/**

  • @author xiaoman

  • 可以设置背景色、指定圆角、描边的宽度和颜色

*/

public class RoundButton extends AppCompatTextView {

private RoundButtonDrawable roundButtonDrawable;

public RoundButton(Context context) {

super(context);

init(context, null, 0);

}

public RoundButton(Context context, AttributeSet attrs) {

super(context, attrs, R.attr.RoundButtonStyle);

init(context, attrs, R.attr.RoundButtonStyle);

}

public RoundButton(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init(context, attrs, defStyleAttr);

}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {

roundButtonDrawable = RoundButtonDrawable.fromAttrSet(context, attrs, defStyleAttr);

ViewHelperUtils.setBackgroundKeepingPadding(this, roundButtonDrawable);

}

/**

  • 设置背景颜色

  • @param color

*/

@Override

public void setBackgroundColor(int color) {

roundButtonDrawable. setColor(color);

}

/**

  • 设置描边的宽度和颜色

  • @param width

  • @param color

*/

public void setStrokeData(int width, int color) {

roundButtonDrawable.setStrokeData(width, color);

}

/**

  • 设置描边颜色

  • @param color

*/

public void setStrokeColors(int color) {

roundButtonDrawable.setStrokeColor(color);

}

/**

  • 设置四个角的半径

  • @param radius

*/

public void setRadius(int radius){

roundButtonDrawable.setCornerRadius(radius);

}

/**

  • 设置 每一个角的半径

  • @param topLeftRadius 左上角半径

  • @param topRightRadius 右上角半径

  • @param bottomLeftRadius 右下角半径

  • @param bottomRightRadius 左下角半径

*/

public void setEachCornerRadius(int topLeftRadius,int topRightRadius,int bottomLeftRadius,int bottomRightRadius){

float[] radius = new float[]{

topLeftRadius, topLeftRadius,

topRightRadius, topRightRadius,

bottomRightRadius, bottomRightRadius,

bottomLeftRadius, bottomLeftRadius

};

roundButtonDrawable. setCornerRadii(radius);

}

/**

  • 设置渐变

  • @param gradientType 渐变类型

  • @param orientation 渐变方向

  • @param colors 渐变颜色

*/

public void setGradient(int gradientType, GradientDrawable.Orientation orientation, int[] colors){

总结

学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

最后如何才能让我们在面试中对答如流呢?

答案当然是平时在工作或者学习中多提升自身实力的啦,那如何才能正确的学习,有方向的学习呢?有没有免费资料可以借鉴?为此我整理了一份Android学习资料路线:

这里是一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套BAT大厂面试资料专题包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家。

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

最后,祝愿即将跳槽和已经开始求职的大家都能找到一份好的工作!

这些只是整理出来的部分面试题,后续会持续更新,希望通过这些高级面试题能够降低面试Android岗位的门槛,让更多的Android工程师理解Android系统,掌握Android系统。喜欢的话麻烦点击一个喜欢再关注一下~

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

之后的面试计划及职业规划。

最后,祝愿即将跳槽和已经开始求职的大家都能找到一份好的工作!

这些只是整理出来的部分面试题,后续会持续更新,希望通过这些高级面试题能够降低面试Android岗位的门槛,让更多的Android工程师理解Android系统,掌握Android系统。喜欢的话麻烦点击一个喜欢再关注一下~

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 28
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值