Android ImageButton自訂按鈕的按下效果的代碼實現方法

來源:互聯網
上載者:User

 

使用Button時為了讓使用者有“按下”的效果,有兩種實現方式:
1.在代碼裡面。

imageButton.setOnTouchListener(new OnTouchListener(){                        @Override                        public boolean onTouch(View v, MotionEvent event) {                                if(event.getAction() == MotionEvent.ACTION_DOWN){                                        //更改為按下時的背景圖片                                        v.setBackgroundResource(R.drawable.pressed);                                }else if(event.getAction() == MotionEvent.ACTION_UP){                                        //改為抬起時的圖片                                        v.setBackgroundResource(R.drawable.released);                                }                                return false;                        }                                        });
2.用XML檔案實現。
                
這個檔案放在drawable目錄下面。命名為button_add_x.xml
使用的時候

 

我自己摸索摸索,發現這樣的實現過程雖然通用性好,但是很麻煩,一個按鈕實現效果需要多張圖片甚至再加一個布局…
那一個遊戲要是有幾百個按鈕怎麼辦呢?
於是:以下代碼被醞釀出來了:

 

/**     * 按下這個按鈕進行的顏色過濾     */    public final static float[] BT_SELECTED=new float[] {          2, 0, 0, 0, 2,          0, 2, 0, 0, 2,          0, 0, 2, 0, 2,          0, 0, 0, 1, 0 };          /**     * 按鈕恢複原狀的顏色過濾     */    public final static float[] BT_NOT_SELECTED=new float[] {          1, 0, 0, 0, 0,          0, 1, 0, 0, 0,          0, 0, 1, 0, 0,          0, 0, 0, 1, 0 };          /**     * 按鈕焦點改變     */    public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener() {          @Override    public void onFocusChange(View v, boolean hasFocus) {      if (hasFocus) {       v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));       v.setBackgroundDrawable(v.getBackground());      }      else     {       v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));        v.setBackgroundDrawable(v.getBackground());      }     }    };         /**     * 按鈕觸碰按下效果     */   public final static OnTouchListener buttonOnTouchListener=new OnTouchListener() {     @Override    public boolean onTouch(View v, MotionEvent event) {      if(event.getAction() == MotionEvent.ACTION_DOWN){       v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));       v.setBackgroundDrawable(v.getBackground());       }       else if(event.getAction() == MotionEvent.ACTION_UP){        v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));        v.setBackgroundDrawable(v.getBackground());       }      return false;     }    };        /**    * 設定圖片按鈕擷取焦點改變狀態    * @param inImageButton    */   public final static void setButtonFocusChanged(View inView)    {     inView.setOnTouchListener(buttonOnTouchListener);     inView.setOnFocusChangeListener(buttonOnFocusChangeListener);    }  

 

使用時,調用方法
public final static void setButtonFocusChanged(View inView)
即可。
【原理】
利用Drawable類的setColorFilter方法對圖片進行顏色位移過濾處理。


 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.