安卓省心技巧1:封装圆角/渐变按钮+vierGroup

问题:

drawable里的shape.xml:在页面很多的工程中,按钮或布局的样式也多。若一个个的写shape,则会导致最后shape文件非常多,也不好管理

封装个view,用view去写,极其整洁高效


效果:

在这里插入图片描述

代码:

此示例view只继承自Textview,你可在写几个继承自LinearLayout,RelativeLayout
写法都一样。然后自由搭配,非常舒服

<!-- 布局文件示例。对于自定义属性,可自由随意搭配,展示不同的效果-->
 <com.andromeda.volley.tcHoutai.myViews.colorV.MyColorText
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textSize="15dp"
            android:text="哈哈"
            app:colorV="#eb5a49" 
            app:endColorV="#8a8a8a"   
            app:radiusV="100">
</com.andromeda.volley.tcHoutai.myViews.colorV.MyColorText>

<!-- attr中-->

   <declare-styleable name="colorView">
        <attr name = "radiusV" format = "integer" />     //圆角
        <attr name = "colorV" format = "color" />		//背景色
        <attr name = "startColorV" format = "color" />	//渐变色开始
        <attr name = "endColorV" format = "color" />	//渐变色结束
        <attr name = "lineColorV" format = "color" />	//描边色
        <attr name = "lineWidthV" format = "integer" />   //描边宽度
        <attr name = "orgV" format = "integer" />		//渐变角度
        <attr name = "isJianbianV" format = "boolean" />	//是否开启渐变
 </declare-styleable>

代码如下

public class MyColorText extends AppCompatTextView {

    private boolean isJianbian=false;
    private int radius=0;
    private int color=0x00000000;
    private int startColor=0x00000000;
    private int endColor=0x00000000;
    private  int lineWidth=0;
    private int lineColor=0x00000000;
    private int org=0;

    public MyColorText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.colorView);
        radius=ta.getInteger(R.styleable.colorView_radiusV,0);
        color=ta.getColor(R.styleable.colorView_colorV,0x00000000);
        startColor=ta.getColor(R.styleable.colorView_startColorV,0x00000000);
        endColor=ta.getColor(R.styleable.colorView_endColorV,0x00000000);
        lineColor=ta.getColor(R.styleable.colorView_lineColorV,0x00000000);
        lineWidth=ta.getInteger(R.styleable.colorView_lineWidthV,0);
        isJianbian=ta.getBoolean(R.styleable.colorView_isJianbianV,false);
        org=ta.getInteger(R.styleable.colorView_orgV,0);

        GradientDrawable gd =null;
        if(isJianbian){
            int colors[] = { startColor,endColor };
            gd = new GradientDrawable(getO(org), colors);
        }else {
            gd = new GradientDrawable();
            gd.setColor(color);
        }
        if(radius!=0){
            gd.setCornerRadius(radius);
        }
        if(lineWidth!=0){
            gd.setStroke(lineWidth, lineColor);
        }

        setBackgroundDrawable(gd);

    }

    public MyColorText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public MyColorText(Context context) {
        this(context, null);
    }




    private GradientDrawable.Orientation getO(int a){
        switch (a){
            case 0:
                return GradientDrawable.Orientation.LEFT_RIGHT;
            case 45:
                return GradientDrawable.Orientation.TL_BR;
            case 90:
                return GradientDrawable.Orientation.TOP_BOTTOM;
            case 135:
                return GradientDrawable.Orientation.TR_BL;
            case 180:
                return GradientDrawable.Orientation.RIGHT_LEFT;
            default:
                return GradientDrawable.Orientation.LEFT_RIGHT;

        }
    }

}

学习产出:

同是天涯打工人,相逢何必论高下。
红颜何处真知己,人生无聊才编程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值