首页 > 技术文章 > <Android 基础(二十六)> 渐变色圆角Button

lanzhi 2016-10-25 14:43 原文

简介

总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加。作为一个Android开发者,这部分应该是不可或缺的。

自定义属性

位置:res/values/attrs.xml
格式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="name_of_style">
        <attr name="name_of_attr" format="reference|string|color|boolean|dimension|enum|flag|float|fraction|integer"/>
    </declare-styleable>
</resources>
format 意义
reference 参考某一资源ID, 如R.drawable.xxx
string 字符串
color 颜色
boolean 布尔值
dimension 尺寸值
enum 枚举值,例如这里写图片描述
flag 位或运算,例如:这里写图片描述
float 浮点数
fraction 百分数,例如pivotX,pivotY这一类属性
integer 整数

获取自定义属性

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.name_of_style);

Color mColor = typedArray.getColor(R.styleable.name_of_style_name_of_attr, Color.BLUE);//其他的属性获取类似
typedArray.recycle();//记得回收


名字:
R.styleable.{name_of_style}
R.styleable.{name_of_style}_{name_of_attr}

举个栗子

举个栗子,实现一个背景为渐变色的圆角按钮,圆角半径,开始颜色,中心颜色,结束颜色,渐变方向用户可自定义。
attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CornerButton">
        <attr name="corner_radius" format="dimension" />
        <attr name="background_start_color" format="color" />
        <attr name="background_center_color" format="color" />
        <attr name="background_end_color" format="color" />
        <attr name="backgrouund_gradient_orientation">
            <enum name="TOP_BOTTOM" value="0" />
            <enum name="TR_BL" value="1" />
            <enum name="RIGHT_LEFT" value="2" />
            <enum name="BR_TL" value="3" />
            <enum name="BOTTOM_TOP" value="4" />
            <enum name="BL_TR" value="5" />
            <enum name="LEFT_RIGHT" value="6" />
            <enum name="TL_BR" value="7" />
        </attr>
    </declare-styleable>
</resources>

CornerButton

public class CornerButton extends Button {

    private GradientDrawable mBg;
    private float mRandius;
    private int mStartColor;
    private int mCenterColor;
    private int mEndColor;
    private int mOrientation;

    public CornerButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CornerButton);
        mRandius = typedArray.getDimension(R.styleable.CornerButton_corner_radius, 10);
        mStartColor = typedArray.getColor(R.styleable.CornerButton_background_start_color, Color.BLUE);
        mCenterColor = typedArray.getColor(R.styleable.CornerButton_background_center_color, Color.GREEN);
        mEndColor = typedArray.getColor(R.styleable.CornerButton_background_end_color, Color.BLACK);
        mOrientation = typedArray.getInt(R.styleable.CornerButton_backgrouund_gradient_orientation, 0);
        typedArray.recycle();

        int[] colors = {mStartColor, mCenterColor, mEndColor};
        mBg = new GradientDrawable();
        mBg.setCornerRadius(mRandius);
        mBg.setOrientation(Orientation.TR_BL);
        mBg.setColors(colors);
        switch (mOrientation) {
            case 0:
                mBg.setOrientation(Orientation.TOP_BOTTOM);
                break;
            case 1:
                mBg.setOrientation(Orientation.TR_BL);
                break;
            case 2:
                mBg.setOrientation(Orientation.RIGHT_LEFT);
                break;
            case 3:
                mBg.setOrientation(Orientation.BR_TL);
                break;
            case 4:
                mBg.setOrientation(Orientation.BOTTOM_TOP);
                break;
            case 5:
                mBg.setOrientation(Orientation.BL_TR);
                break;
            case 6:
                mBg.setOrientation(Orientation.LEFT_RIGHT);
                break;
            case 7:
                mBg.setOrientation(Orientation.TL_BR);
                break;
        }
        this.setBackground(mBg);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yidong="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gs.com.customview.MainActivity">

    <gs.com.customview.CornerButton
        android:id="@+id/cb_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        yidong:background_start_color="#CCFF0000"
        yidong:background_center_color="#CCAADD00"
        yidong:background_end_color="#CC00EEFF"
        yidong:corner_radius="100dp"
        yidong:backgrouund_gradient_orientation="BOTTOM_TOP"
        />
</RelativeLayout>

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

yidong:corner_radius=”100dp”
XML命名空间和属性的Tag对应。

实际效果
这里写图片描述 这里写图片描述

推荐阅读