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

}

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){

roundButtonDrawable.setGradientType(gradientType);

roundButtonDrawable.setOrientation(orientation);

roundButtonDrawable.setColors(colors);

}

public static void setBackgroundKeepingPadding(View view, Drawable drawable) {

int[] padding = new int[]{view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()};

view.setBackground(drawable);

view.setPadding(padding[0], padding[1], padding[2], padding[3]);

}

}

3、在xml布局文件中直接引用自定义的button

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

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:app=“http://schemas.android.com/apk/res-auto”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.MainActivity”>

<com.ztk.demo.uitool.widget.RoundButton

android:id=“@+id/bt”

android:layout_width=“300dp”

android:layout_height=“wrap_content”

android:text=“Hello World!”

android:padding=“10dp”

app:radius=“30dp”

app:bgColor=“@color/colorAccent”

android:textColor=“#ffffff”

android:gravity=“center_horizontal”

android:layout_marginTop=“60dp”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

<com.ztk.demo.uitool.widget.RoundButton

android:id=“@+id/bt2”

android:layout_width=“300dp”

android:layout_height=“wrap_content”

android:text=“”

android:padding=“10dp”

app:topLeftRadius=“10dp”

app:bgColor=“@color/colorPrimary”

android:textColor=“#ffffff”

android:gravity=“center_horizontal”

android:layout_marginTop=“30dp”

app:layout_constraintTop_toBottomOf=“@+id/bt”

app:layout_constraintLeft_toLeftOf=“parent”

app:layout_constraintRight_toRightOf=“parent” />

<com.ztk.demo.uitool.widget.RoundButton

android:id=“@+id/bt3”

关于面试的充分准备

一些基础知识和理论肯定是要背的,要理解的背,用自己的语言总结一下背下来。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,我能明显感觉到国庆后多了很多高级职位,所以努力让自己成为高级工程师才是最重要的。

好了,希望对大家有所帮助。

接下来是整理的一些Android学习资料,有兴趣的朋友们可以关注下我免费领取方式

①Android开发核心知识点笔记

②对标“阿里 P7” 40W+年薪企业资深架构师成长学习路线图

③面试精品集锦汇总

④全套体系化高级架构视频

**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水!


《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
cfUx03z-1714650603303)]

③面试精品集锦汇总

[外链图片转存中…(img-bUdLyque-1714650603307)]

④全套体系化高级架构视频

**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水!

[外链图片转存中…(img-veZtnRbw-1714650603308)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 12
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值