shape selector 背景 圓形 矩形 圓環 [MD]


博文地址

我的GitHub 我的博客 我的微信 我的郵箱
baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

selector

支持的狀態

我們在定義一個drawable的時候可以通過xml定義drawable對象,它能使一個圖片能在不同的狀態下顯示不同的圖案,具體支持以下類型

  • android:state_pressed 是否按下,如一個按鈕觸摸或者點擊。☆☆☆
  • android:state_focused 是否取得焦點,比如用戶選擇了一個文本框。
  • android:state_hovered 光標是否懸停,通常與focused相同,它是4.0的新特性
  • android:state_selected 被選中,它與focus 並不完全一樣,如一個list view 被選中的時候,它里面的各個子組件可能通過方向鍵,被選中了。
  • android:state_checkable 組件是否能被check。如:RadioButton是可以被check的。
  • android:state_checked 被checked了,如:一個【RadioButton】可以被check了。☆☆☆
  • android:state_enabled 能夠接受觸摸或者點擊事件,默認為true。
  • android:state_activated 被激活(這個麻煩舉個例子,不是特明白)
  • android:state_window_focused 應用程序是否在前台,當有通知欄被拉下來或者一個對話框彈出的時候應用程序就不在前台了

注意

  • 如果有多個item,那么程序將自動【從上到下】進行匹配,最先匹配的將得到應用。(不是通過最佳匹配)
  • 如果一個item沒有任何的狀態說明,那么它將可以和任何一個狀態匹配
  • 所以,無狀態的item一定要放在最下面。

案例

顏色--不能作為背景資源

android:textColor="@drawable/_color_selector"  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/_color_press"/>
    <item android:state_enabled="false" android:color="@color/_color_enable/>
    <item android:color="@color/_color"/>
</selector>  

圖片--背景資源

android:background="@drawable/_bg_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/_bg_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/_bg_select" android:state_selected="true"/>
    <item android:drawable="@drawable/_bg"/>
</selector>  

.9和shape圖片--背景資源

android:background="@drawable/_bg_selector"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"><nine-patch android:src="@drawable/_press" ></nine>
    </item>
    <item><shape>
            <corners android:radius="3dp" ></corners>
            <stroke android:width="1dp" android:color="@color/line_divider" ></stroke>
        </shape></item>
</selector

shape 基本使用

如果在某些手機中使用 shape 出現黑色填充背景,設置<solid android:color="@color/transparent"/>即可。

常用屬性

  • shape 定義shape的形狀,默認為矩形,可以設置為矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環形(ring)
  • corners 圓角半徑,在shape為rectangle時有用,可以分別設置四個角的圓角半徑
  • solid 固定的填充顏色,不能與漸變色gradient一起使用;如果出現黑色填充背景等異常效果,可以把solid設為透明試試
  • stroke 描邊、邊框,可以設置描邊線條的顏色、寬度和樣式
  • gradient 漸變填充顏色,這個稍微復雜點
  • padding 內邊距大小,可以分別設置四個方位的內邊距大小

圓角半徑 corners

  • android:radius 整型 半徑
  • android:topLeftRadius 整型 左上角半徑
  • android:topRightRadius 整型 右上角半徑
  • android:bottomLeftRadius 整型 左下角半徑
  • android:bottomRightRadius 整型 右下角半徑

描邊、邊框 stroke

  • android:width 整型 描邊的寬度;實線的寬度
  • android:color 顏色值 描邊的顏色;實線的顏色
  • android:dashWidth [dash:破折號、虛線]表示描邊為虛線時,虛線的長度, 不設置或設為0時,表示實線
  • android:dashGap [gap:間隔]表示描邊為虛線時,虛線之間的間隙寬度, 不設置或設為0時,表示實線

漸變色 gradient

  • android:startColor 起始顏色;android:endColor 結束顏色;android:centerColor 中間的顏色
  • android:angle 漸變起始角度,然后逆時針方向轉,值必須為45的整數倍。=0時(默認),從左向右;=90時,從下往上。該屬性只有在type=linear(默認)情況下起作用
  • android:type 漸變類型,取值有三個: linear線性漸變(默認),radial放射性漸變(以開始色為中心),sweep(掃描線式的漸變)
  • android:gradientRadius 漸變色半徑,整型或百分比,當 android:type="radial" 時【必須】使用,否則會報錯
  • android:useLevel boolean值,如果當做是LevelListDrawable使用時值為true(無漸變),否則為false(默認值,有漸變色)
  • android:centerX 整型 漸變中心X點坐標的相對位置
  • android:centerY 整型 漸變中心Y點坐標的相對位置

內邊距大小 padding

  • android:left 整型 左內邊距
  • android:top 整型 上內邊距
  • android:right 整型 右內邊距
  • android:bottom 整型 下內邊距

圖形大小 size

指定圖形的寬高;只有控件指定的是wrap_content時,這個屬性才會起作用

  • android:width 寬度
  • android:height 高度

ring的其他屬性

下面的屬性只有在android:shape="ring時可用:

  • android:innerRadius 內環的半徑
  • android:thickness 環的厚度
  • android:useLevel boolean值,如果當做是LevelListDrawable使用時值為true(默認值),否則為false
  • android:innerRadiusRatio 代表"環的寬度/內環半徑"的值,比如環的寬度為50,比例為2.5,那么內環半徑為20
  • android:thicknessRatio 代表"環的寬度/環的厚度"的值

shape 案例

圓形與矩形

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_oval_simple"
        android:gravity="center"
        android:text="圓形" />
    <!-- 如果設置為wrap_content,會因為字體寬高不相同導致圓被擠壓成為橢圓 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:background="@drawable/shape_oval_simple"
        android:gravity="center"
        android:text="橢圓" />
    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_oval_popular"
        android:gravity="center"
        android:text="經典" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:background="@drawable/shape_rectangle_popular"
        android:gravity="center"
        android:padding="5dp"
        android:text="圓角矩形" />
    <TextView
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_rectangle_complex"
        android:gravity="center"
        android:text="復雜矩形" />
    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_rectangle_parent"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/left"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_rectangle_l"
            android:gravity="center"
            android:text="北京"
            android:textColor="@color/white" />
        <TextView
            android:id="@+id/right"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="上海"
            android:textColor="@color/red" />
    </LinearLayout>
</LinearLayout>

圓環、環形

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_ring_simple"
        android:gravity="center"
        android:text="正常圓環" />
    <!-- 如果設置為wrap_content,會導致圓環外緣線被切割掉 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:background="@drawable/shape_ring_simple"
        android:gravity="center"
        android:text="會被切割" />
    <!-- 如果layout_width或layout_height小於圓環中定義的大小,也會導致圓環外緣線被切割掉 -->
    <TextView
        android:id="@+id/tv_42"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_ring_failed"
        android:gravity="center"
        android:text="會被切割" />
    <TextView
        android:id="@+id/tv_43"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/shape_ring_ratio"
        android:gravity="center"
        android:text="Ratio屬性" />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp" >
        <TextView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerInParent="true"
            android:background="@drawable/shape_ring_simple"
            android:gravity="center"
            android:text="實心圓環" />
        <!-- View的寬高應該為:【2*innerRadius】-【stroke_width/2】 是否要除以2我也不確定-->
        <View
            android:layout_width="11dp"
            android:layout_height="11dp"
            android:layout_centerInParent="true"
            android:background="@drawable/shape_oval" />
    </RelativeLayout>
</LinearLayout>

漸變

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_gradient_simple"
        android:gravity="center"
        android:text="RGB線性漸變" />
    <!-- angle:漸變起始角度,然后逆時針方向轉,值必須為45的整數倍。=0時(默認),從左向右;=90時,從下往上。該屬性只有在type=linear(默認)情況下起作用 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_gradient_angle"
        android:gravity="center"
        android:text="從上到下" />
    <!-- type:漸變類型, linear 線性漸變,默認;  radial 放射性漸變,以開始色為中心; sweep 掃描線式的漸變。 -->
    <!-- gradientRadius:漸變色半徑,整型或百分比,當 android:type="radial" 時【必須】使用,否則會報錯。 -->
    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_gradient_type_radial_20"
        android:gravity="center"
        android:text="radial" />
    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_gradient_type_radial_60"
        android:gravity="center"
        android:text="radial" />
    <TextView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_gradient_type_radial_99"
        android:gravity="center"
        android:text="radial" />
    <TextView
        android:layout_width="42dp"
        android:layout_height="40dp"
        android:layout_margin="2dp"
        android:background="@drawable/shape_gradient_type_sweep"
        android:gravity="center"
        android:text="sweep" />
</LinearLayout>

代碼中設置 shape

tvContent.setBackground(getDrawable(mTemplateTag.color));
private GradientDrawable getDrawable(int bgColor) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColor(bgColor);
    gradientDrawable.setCornerRadius(DisplayUtil.dip2px(ApplicationContext.getContext(), 4));
    return gradientDrawable;
}

可設置圓形、半圓、圓角的自定義TV

/**
 * 可設置圓形,半圓,圓角的textview
 */
public class ShapeTextView extends android.support.v7.widget.AppCompatTextView {

    private int TOUCH_SLOP = ViewConfiguration.get(ApplicationContext.getContext()).getScaledTouchSlop();
    private float startX;
    private float startY;
    private OnClickListener mOnClickListener;
    private static final int EXPAND_SPEC = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

    //默認各種狀態圓角的大小都一樣,
    private int topLeftRadius;         //上左圓角
    private int topRightRadius;        //上右圓角
    private int bottomLeftRadius;       //下左圓角
    private int bottomRightRadius;     //下右圓角

    private int radius;                //背景圓角,這個是用

    private int normalStartBgColor;   //正常的開始顏色
    private int normalEndBgColor;     //正常的結束顏色
    private int normalBgAngle;        //正常的角度
    private int normalBgColor;        //正常的背景色
    private int normalStorkeWidth;    //未點擊的邊框寬度
    private int normalStorkeColor;    //未點擊的邊框色
    private int normaltextColor;      //未點擊時文字顏色

    private int pressedStartBgColor;   //點擊時開始顏色
    private int pressedEndBgColor;     //點擊時結束顏色
    private int pressedBgAngle;        //點擊時漸變角度
    private int pressedBgColor;       //點擊時的背景色
    private int pressedStorkeWidth;   //點擊時的邊框寬度
    private int pressedStorkeColor;   //點擊時的邊框顏色
    private int pressedTextColor;     //點擊時文字顏色

    private int selectedStartBgColor;   //選中的開始顏色
    private int selectedEndBgColor;     //選中結束顏色
    private int selectedBgAngle;        //選中的角度
    private int selectedTextColor;   //選中變換的顏色
    private int selectedStorkeWidth; //選中的邊框寬度
    private int selectedStorkeColor; //選中的邊框色
    private int selectedBgColor;     //選中的背景色

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

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

    public ShapeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShapeTextView);

        radius = a.getDimensionPixelOffset(R.styleable.ShapeTextView_allradius, 0);  //圓角

        normalBgColor = a.getColor(R.styleable.ShapeTextView_normalBgColor, Color.TRANSPARENT);
        normalStorkeWidth = a.getDimensionPixelOffset(R.styleable.ShapeTextView_normalStorkeWidth, Utils.dpToPx(1)); //默認邊框1dp
        normalStorkeColor = a.getColor(R.styleable.ShapeTextView_normalStorkeColor, Color.TRANSPARENT);   //邊框默認透明
        normaltextColor = a.getColor(R.styleable.ShapeTextView_normalTextColor, Color.BLACK);       //字體默認為黑色
        normalStartBgColor = a.getColor(R.styleable.ShapeTextView_normalBgStartColor, Color.TRANSPARENT);       //字體默認為黑色
        normalEndBgColor = a.getColor(R.styleable.ShapeTextView_normalBgEndColor, Color.TRANSPARENT);       //字體默認為黑色
        normalBgAngle = a.getInteger(R.styleable.ShapeTextView_normalAngle, 0); //默認邊框1dp

        pressedBgColor = a.getColor(R.styleable.ShapeTextView_pressedBgColor, Color.TRANSPARENT);
        pressedStorkeWidth = a.getDimensionPixelOffset(R.styleable.ShapeTextView_pressedStorkeWidth, Utils.dpToPx(1));//默認邊框1dp
        pressedStorkeColor = a.getColor(R.styleable.ShapeTextView_pressedStorkeColor, Color.TRANSPARENT);     //邊框默認透明
        pressedTextColor = a.getColor(R.styleable.ShapeTextView_pressedTextColor, Color.BLACK);         //字體默認為黑色
        pressedStartBgColor = a.getColor(R.styleable.ShapeTextView_pressedBgStartColor, Color.TRANSPARENT);       //字體默認為黑色
        pressedEndBgColor = a.getColor(R.styleable.ShapeTextView_pressedBgEndColor, Color.TRANSPARENT);       //字體默認為黑色
        pressedBgAngle = a.getInteger(R.styleable.ShapeTextView_pressedAngle, 0); //默認邊框1dp

        topLeftRadius = a.getDimensionPixelOffset(R.styleable.ShapeTextView_topLeftRadius, 0);
        topRightRadius = a.getDimensionPixelOffset(R.styleable.ShapeTextView_topRightRadius, 0);
        bottomLeftRadius = a.getDimensionPixelOffset(R.styleable.ShapeTextView_bottomLeftRadius, 0);
        bottomRightRadius = a.getDimensionPixelOffset(R.styleable.ShapeTextView_bottomLeftRadius, 0);

        selectedBgColor = a.getColor(R.styleable.ShapeTextView_selectedBgColor, Color.TRANSPARENT);
        selectedStorkeWidth = a.getDimensionPixelOffset(R.styleable.ShapeTextView_selectedStorkeWidth, Utils.dpToPx(1));//默認邊框1dp
        selectedStorkeColor = a.getColor(R.styleable.ShapeTextView_selectedStorkeColor, Color.TRANSPARENT);   //邊框默認透明
        selectedTextColor = a.getColor(R.styleable.ShapeTextView_selectedTextColor, Color.BLACK);       //字體默認為黑色
        selectedStartBgColor = a.getColor(R.styleable.ShapeTextView_selectedBgStartColor, Color.TRANSPARENT);       //字體默認為黑色
        selectedEndBgColor = a.getColor(R.styleable.ShapeTextView_selectedBgEndColor, Color.TRANSPARENT);       //字體默認為黑色
        selectedBgAngle = a.getInteger(R.styleable.ShapeTextView_selectedAngle, 0); //默認邊框1dp

        a.recycle();

        setSelect();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setSelect() {
        normaltextColor = getCurrentTextColor();
        GradientDrawable normal = null;
        GradientDrawable pressed = null;
        GradientDrawable selected = null;

        if (radius != 0) {
            normal = getDrawable(radius, normalBgColor, normalStorkeWidth, normalStorkeColor,
                    normalStartBgColor,normalEndBgColor,normalBgAngle);
            pressed = getDrawable(radius, pressedBgColor, pressedStorkeWidth, pressedStorkeColor,
                    pressedStartBgColor,pressedEndBgColor,pressedBgAngle);
            selected = getDrawable(radius, selectedBgColor, selectedStorkeWidth, selectedStorkeColor,
                    selectedStartBgColor,selectedEndBgColor,selectedBgAngle);
        } else {
            normal = getDrawable(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius,
                    normalBgColor, normalStorkeWidth, normalStorkeColor,normalStartBgColor,normalEndBgColor,normalBgAngle);
            pressed = getDrawable(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius,
                    pressedBgColor, pressedStorkeWidth, pressedStorkeColor,pressedStartBgColor,pressedEndBgColor,pressedBgAngle);
            selected = getDrawable(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius,
                    selectedBgColor, selectedStorkeWidth, selectedStorkeColor,selectedStartBgColor,selectedEndBgColor,selectedBgAngle);
        }

        //沒有設置點擊背景色,表示不設置Selector 只設置Draw
        if (pressedBgColor != 0) {
            StateListDrawable selector = getSelector(normal, pressed, selected);
            setBackground(selector);
        } else {
            setBackground(normal);
        }
    }

    /**
     * 設置是否是被選擇了,這里要手動設置是否選擇的狀態
     */
    public void setTextViewSelect(boolean isSelected) {
        setSelected(isSelected);
        if (isSelected == true) {
            setTextColor(selectedTextColor);
        } else {
            setTextColor(normaltextColor);
        }
    }

    /**
     * 重寫了onclick的監聽
     */
    @Override
    public void setOnClickListener(OnClickListener listener) {
        mOnClickListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (pressedTextColor != 0) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = event.getX();
                    startY = event.getY();
                    setTextColor(pressedTextColor);
                    break;
                case MotionEvent.ACTION_UP:
                    setTextColor(normaltextColor);
                    float endX = event.getX();
                    float endY = event.getY();
                    if (isClick(startX, endX, startY, endY)) {
                        if (mOnClickListener != null) {
                            mOnClickListener.onClick(this);
                        }
                    }
                    break;
            }
            return true;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 設置背景選擇器
     */
    private StateListDrawable getSelector(Drawable normalDraw, Drawable pressedDraw, Drawable selected) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDraw);
        stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, normalDraw);
        stateListDrawable.addState(new int[]{android.R.attr.state_selected}, selected);
        return stateListDrawable;
    }

    /**
     * 設置shape,這里設置一個radius就好了
     */
    private GradientDrawable getDrawable(int radius, int bgColor, int storkeWidth, int strokeColor ,
                                         int startColor, int endColor, int angle) {
        GradientDrawable gradientDrawable;
        if (startColor != 0 && endColor != 0) {
            int[] colors = {startColor, endColor};
            gradientDrawable = new GradientDrawable(getOrientationByAngle(angle),colors);
        }else {
            gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(bgColor);
        }
        gradientDrawable.setCornerRadius(radius);
        gradientDrawable.setStroke(storkeWidth, strokeColor);
        return gradientDrawable;
    }

    /**
     * 通過angle的值,來轉換成枚舉標量
     */
    public GradientDrawable.Orientation getOrientationByAngle(int angle) {
        GradientDrawable.Orientation orientation = GradientDrawable.Orientation.LEFT_RIGHT;
        switch (angle) {
            case 0:
                orientation = GradientDrawable.Orientation.LEFT_RIGHT;
                break;
            case 45:
                orientation = GradientDrawable.Orientation.BL_TR;
                break;
            case 90:
                orientation = GradientDrawable.Orientation.BOTTOM_TOP;
                break;
            case 135:
                orientation = GradientDrawable.Orientation.BR_TL;
                break;
            case 180:
                orientation = GradientDrawable.Orientation.RIGHT_LEFT;
                break;
            case 225:
                orientation = GradientDrawable.Orientation.TR_BL;
                break;
            case 270:
                orientation = GradientDrawable.Orientation.TOP_BOTTOM;
                break;
            case 315:
                orientation = GradientDrawable.Orientation.TL_BR;
                break;
        }
        return orientation;
    }

    /**
     * 設置shape,分別設置所有的邊角
     */
    private GradientDrawable getDrawable(float topLeftRadius, float topRightRadius, float bottomLeftRadius,
                                         float bottomRightRadius, int bgColor, int storkeWidth, int strokeColor,
                                                 int startColor, int endColor, int angle) {
        GradientDrawable gradientDrawable;

        //top-left, top-right, bottom-right, bottom-left
        float[] radius = new float[]{topLeftRadius, topLeftRadius, topRightRadius, topRightRadius,
                bottomLeftRadius, bottomLeftRadius, bottomRightRadius, bottomRightRadius};
        //這個方法傳入的數組長度必須是 > = 8 否則會拋數值下標越界  數組值分別對應 top-left, top-right, bottom-right, bottom-left

        if (startColor != 0 && endColor != 0) {
            int[] colors = {startColor, endColor};
            gradientDrawable = new GradientDrawable(getOrientationByAngle(angle),colors);
        }else {
            gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(bgColor);
        }
        gradientDrawable.setCornerRadii(radius);
        gradientDrawable.setStroke(storkeWidth, strokeColor);
        return gradientDrawable;
    }

    /**
     * 判斷是否移動了,如果沒有怎么移動,抬起按鈕,說明是激活了按鍵消息
     */
    private boolean isClick(float startX, float endX, float startY, float endY) {
        float differenceX = Math.abs(startX - endX);
        float differenceY = Math.abs(startY - endY);
        return !(differenceX > TOUCH_SLOP || differenceY > TOUCH_SLOP);
    }
}

使用

圓角,帶描邊
app:allradius="15dp"
app:normalStorkeWidth="1dp"
app:normalStorkeColor="#ffffff"

漸變
app:normalBgStartColor="#10bfaf"
app:normalBgEndColor="#0897ac"
app:normalAngle="0"
app:allradius="4dp"

圓角,選擇器
app:allradius="15dp"
app:normalStorkeColor="@color/color_80ffffff"
app:normalTextColor="@color/color_80ffffff"
app:pressedStorkeColor="@color/color_40ffffff"
app:pressedTextColor="@color/color_40ffffff"

注意,文字顏色不是默認的黑色的話必須同時設置以下幾個屬性才能保證不會出問題
android:textColor="@color/color_ffffff"
app:normalTextColor="@color/color_ffffff"
app:pressedTextColor="@color/color_ffffff"
app:selectedTextColor="@color/color_ffffff"

2016-03-24


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM