溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

編寫可復(fù)用的自定義按鈕

發(fā)布時間:2020-07-25 08:21:02 來源:網(wǎng)絡(luò) 閱讀:285 作者:huangmeicai 欄目:移動開發(fā)

編寫可復(fù)用的自定義按鈕

現(xiàn)在有個正在開發(fā)的Android項目,里面已經(jīng)有了一些不合理的UI實現(xiàn)方式。比如按鈕是一張圖:

 編寫可復(fù)用的自定義按鈕

可以看出,應(yīng)該用編程的方式來實現(xiàn)這個按鈕,比如xml聲明drawable,一個矩形框,四個邊是圓角,要有個很細(xì)的邊框,黑色的,背景色使用漸進(jìn)色效果。登錄使用文字而不是在圖形里。

這樣的好處很多:

· 自由的在不同分辨率屏幕下做適配,不必考慮圖形的長寬比;

· 當(dāng)文字改動后,不必喊上美工一起加班處理;

· 文字的國際化。

本文方案的基本思路是,還是用這個圖,但是增加復(fù)用性,開發(fā)者只需在布局中使用自定義按鈕,就可以讓已經(jīng)存在的這種布局具備點擊后高亮的效果,而不必準(zhǔn)備多張圖,寫冗長的xml文件做selector。

實現(xiàn)后的效果,在手指觸碰到該按鈕的時候:

 編寫可復(fù)用的自定義按鈕


抬起或者移動到按鈕外區(qū)域恢復(fù)原來的樣子。

這里布局還是在xml中,類似這樣:

<com.witmob.CustomerButton
    android:id=”@+id/login”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_alignParentLeft=”true”
    android:layout_centerVertical=”true”
    android:layout_marginLeft=”26dp”
    android:background=”@drawable/login_login_but”/>

實現(xiàn)的按鈕代碼:

package com.witmob;

import android.content.Context;
import android.graphics.LightingColorFilter;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class CustomerButton extends Button {
   
    public CustomerButton(Context context) {
        super(context);
        this.init();
    }

    public CustomerButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.init();
    }

    public CustomerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }
   
    private void init(){
        this.setOnTouchListener(new OnTouchListener() {
           
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action=event.getActionMasked();
                switch (action) {
                case MotionEvent.ACTION_DOWN:
                    getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x000000FF));
                    getBackground().invalidateSelf();
                    break;
                case MotionEvent.ACTION_UP:
                    getBackground().clearColorFilter();
                    getBackground().invalidateSelf();
                    break;
                case MotionEvent.ACTION_MOVE:
                    Rect rect=new Rect();
                    v.getDrawingRect(rect);
                   
                    if(!rect.contains((int)event.getX(),(int)event.getY())){
                        getBackground().clearColorFilter();
                        getBackground().invalidateSelf();
                    }
                    break;
                default:
                    break;
                }
                return false;
            }
        });
    }

}

代碼要點:

· 需要使用OnTouchListener,處理手指按下,抬起和移動到區(qū)域外的處理;

· 使用ColorFilter,獲取背景色的Drawable對象,增加顏色過濾;

· 操作Rect,結(jié)合手指坐標(biāo),判斷是否在區(qū)域內(nèi)部;

· 另外,需要返回false,在OnTouchListener,否則按鈕的OnClickListener將不能生效。

 


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI