自定義狀態(tài)切換按鈕

最近在做一個項目,一個界面的按鈕UI給畫成了這樣(默認(rèn)狀態(tài)是藍(lán)色的然后觸摸后變成灰色的)

UI效果

然后本著給低版本系統(tǒng)APP適配的職業(yè)素養(yǎng)(其實是不想畫這種按鈕),想讓UI兄弟給將圖標(biāo)改成整個按鈕效果的圖片,可是。。。人說人也很忙不給俺做咋辦。。。。沒辦法只好自己寫了一個滿足這種需求的按鈕了。。。

職業(yè)素養(yǎng)

整體流程如下

實現(xiàn)流程

下面說一下主要功能的實現(xiàn)代碼

○動態(tài)的修改控件大小,這里沒什么難處,主要是動態(tài)修改控件大小的代碼要放在onLayout方法里,之前試了onMeasure方法里設(shè)置然并卵。

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    int width = getWidth();
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
    layoutParams.width= (int) (width*0.5);
    layoutParams.height=(int) (width*0.5);
    layoutParams.topMargin=(int) (width*0.05);
    textView.setTextSize((float) (width*0.09));
}

○重寫onTouchEvent,實現(xiàn)點擊效果,這里是通過判斷按下、抬起來動態(tài)的設(shè)置顯示效果

 @Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:;
            if(pressImage!=0){
                imageView.setImageResource(pressImage);
            }
            break;
        case MotionEvent.ACTION_UP:
            textView.setTextColor(textcolorDefault);
            if(defaultImage!=0){
                imageView.setImageResource(defaultImage);
            }
            break;
    }
    return true;
}

○添加點擊事件,這里選擇在抬起的時候執(zhí)行點擊事件

//點擊事件接口
public interface MyStateButtonClickListener {
    void onClick(View view);
}
 @Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:;
            textView.setTextColor(textcolorPress);
            if(pressImage!=0){
                imageView.setImageResource(pressImage);
            }
            break;
        case MotionEvent.ACTION_UP:
            textView.setTextColor(textcolorDefault);
            if(defaultImage!=0){
                imageView.setImageResource(defaultImage);
            }
            //抬起時執(zhí)行點擊事件
            if(myStateButtonClickListener!=null){
                myStateButtonClickListener.onClick(view);
            }
            break;
    }
    return true;
}

○通過代碼添加邊框,并添加自定義屬性(默認(rèn)圖片、點擊圖片、默認(rèn)文字顏色、點擊文字顏色、邊框?qū)挾?、邊框圓角大小、默認(rèn)邊框顏色、點擊邊框顏色)

○attrs文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyStateButton">
    <attr name="defaultImage" format="reference"/>
    <attr name="pressImage" format="reference"/>
    <attr name="text" format="string"/>
    <attr name="defaultTextColor" format="color"/>
    <attr name="pressTextColor" format="color"/>
    <attr name="defaultBorderColor" format="color"/>
    <attr name="pressBorderColor" format="color"/>
    <attr name="cornerRadius" format="float"/>
    <attr name="borderWidth" format="integer"/>
</declare-styleable>
</resources>

○獲取屬性值

//        初始化GradientDrawable用于顯示邊框
    defaultDrawable = new GradientDrawable();
    pressDrawable = new GradientDrawable();
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyStateButton);
    if(typedArray!=null){
//            默認(rèn)圖片
        defaultImage = typedArray.getResourceId(R.styleable.MyStateButton_defaultImage, R.mipmap.one);
//            點擊圖片
        pressImage = typedArray.getResourceId(R.styleable.MyStateButton_pressImage, R.mipmap.one_press);
//            設(shè)置文字
        String text = typedArray.getString(R.styleable.MyStateButton_text);
//            默認(rèn)文字顏色
        textcolorDefault = typedArray.getColor(R.styleable.MyStateButton_defaultTextColor, Color.parseColor("#567DBF"));
//            點擊文字顏色
        textcolorPress = typedArray.getColor(R.styleable.MyStateButton_pressTextColor, Color.parseColor("#BFBFBF"));
        imageView.setImageResource(defaultImage);
        textView.setTextColor(textcolorDefault);
        textView.setText(text);
//            默認(rèn)邊框顏色
        borderColorDefault = typedArray.getColor(R.styleable.MyStateButton_defaultBorderColor, Color.parseColor("#567DBF"));
//            點擊邊框顏色
        borderColorPress = typedArray.getColor(R.styleable.MyStateButton_pressBorderColor, Color.parseColor("#BFBFBF"));
//            邊框?qū)挾?        borderWidth = typedArray.getInteger(R.styleable.MyStateButton_borderWidth, 2);
//            邊框圓角大小
        cornerRadius = typedArray.getFloat(R.styleable.MyStateButton_cornerRadius, 8);
        defaultDrawable.setStroke(borderWidth,borderColorDefault);
        pressDrawable.setStroke(borderWidth,borderColorPress);
        defaultDrawable.setCornerRadius(cornerRadius);
        pressDrawable.setCornerRadius(cornerRadius);
        setBackground(defaultDrawable);
    }

最終效果

最終效果

附上源碼地址https://github.com/myml666/MyStateButton

個人博客https://myml666.github.io

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,210評論 3 119
  • 做人就要有好脾氣,學(xué)會方圓。生意人遇到客戶投訴的時候應(yīng)該理想的解決問題,控制情緒,避其鋒芒;跟家人相處應(yīng)該吃虧是福...
    星光微語閱讀 731評論 0 0
  • 第五十二章 鳳九聞言猶猶豫豫伸出一只爪子,時不時還看一眼帝君,盼望著他老人家能松口,她委實不想喝這苦得慘絕人寰的藥...
    若莎Elsa閱讀 17,300評論 32 103

友情鏈接更多精彩內(nèi)容