Android中ImageButton自定義按鈕的按下效果的代碼實現方法,附網上2種經典解決方法。 .

首先看看網上的2種方法:

【以下爲引用網絡,來源:http://www.eoeandroid.com/thread-7931-1-1.html

使用Button時爲了讓用戶有“按下”的效果,有兩種實現方式:

1.在代碼裏面。

  1. imageButton.setOnTouchListener(new OnTouchListener(){     
  2.                         @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;                              }                      });     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;                           }                    });   
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; } }); 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文件實現。

  1. <?xml version=\"1.0\" encoding=\"UTF-8\"?>    
  2. <selector xmlns:android=\"http://schemas.android.com/apk/res/android\">    

    <item           android:state_pressed=\"false\"  android:drawable=\"@drawable/button_add\" />         <item           android:state_pressed=\"true\"   android:drawable=\"@drawable/button_add_pressed\" />         <item           android:state_focused=\"true\"    android:drawable=\"@drawable/button_add_pressed\" />     <item           android:drawable=\"@drawable/button_add\" />     </selector>        <?xml version=\"1.0\" encoding=\"UTF-8\"?>   <selector xmlns:android=\"http://schemas.android.com/apk/res/android\">   <item           android:state_pressed=\"false\"  android:drawable=\"@drawable/button_add\" />          <item            android:state_pressed=\"true\"  android:drawable=\"@drawable/button_add_pressed\" />       <item            android:state_focused=\"true\"  android:drawable=\"@drawable/button_add_pressed\" />       <item             android:drawable=\"@drawable/button_add\" />   </selector>   <?xml version=\"1.0\" encoding=\"UTF-8\"?> <selector xmlns:android=\"http://schemas.android.com/apk/res/android\"> <item android:state_pressed=\"false\" android:drawable=\"@drawable/button_add\" /> <item android:state_pressed=\"true\" android:drawable=\"@drawable/button_add_pressed\" /> <item android:state_focused=\"true\" android:drawable=\"@drawable/button_add_pressed\" /> <item android:drawable=\"@drawable/button_add\" /> </selector> <?xml version=\"1.0\" encoding=\"UTF-8\"?> <selector xmlns:android=\"http://schemas.android.com/apk/res/android\"> <item android:state_pressed=\"false\" android:drawable=\"@drawable/button_add\" /> <item android:state_pressed=\"true\" android:drawable=\"@drawable/button_add_pressed\" /> <item android:state_focused=\"true\" android:drawable=\"@drawable/button_add_pressed\" /> <item android:drawable=\"@drawable/button_add\" /> </selector>   

 

這個文件放在drawable目錄下面。命名爲button_add_x.xml

使用的時候

  1. <ImageButton    
  2.                         android:id=\"@+id/ImageButton\"    

                        android:layout_width=\"wrap_content\"                             android:layout_height=\"wrap_content\"                             android:background=\"#00000000\"                             android:src=\"@drawable/button_add_x\" >     </ImageButton>     <ImageButton                           android:id=\"@+id/ImageButton\"                           android:layout_width=\"wrap_content\"                           android:layout_height=\"wrap_content\"                           android:background=\"#00000000\"                           android:src=\"@drawable/button_add_x\" >   </ImageButton>   <ImageButton android:id=\"@+id/ImageButton\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:background=\"#00000000\" android:src=\"@drawable/button_add_x\" > </ImageButton> <ImageButton android:id=\"@+id/ImageButton\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" android:background=\"#00000000\" android:src=\"@drawable/button_add_x\" > </ImageButton>

 

【以上爲引用網絡,來源:http://www.eoeandroid.com/thread-7931-1-1.html

 

【以下爲原創,轉載請註明出處:http://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx

 

我自己摸索摸索,發現這樣的實現過程雖然通用性好,但是很麻煩,一個按鈕實現效果需要多張圖片甚至再加一個佈局…

 

那一個遊戲要是有幾百個按鈕怎麼辦呢?

 

於是:以下代碼被醞釀出來了:

 

  1. /**   
  2.    * 按下這個按鈕進行的顏色過濾   
  3.    */    
  4.   public final static float[] BT_SELECTED=new float[] {      

      20002,             02002,             00202,             00010 };                /**       * 按鈕恢復原狀的顏色過濾       */       public final static float[] BT_NOT_SELECTED=new float[] {             10000,             01000,             00100,             00010 };                /**       * 按鈕焦點改變       */       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 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方法對圖片進行顏色偏移過濾處理。

 

 

以下爲效果圖,登陸按鈕此時爲獲取焦點狀態。

 

 \'\'

 

代碼可以適當修改實現3個不同的狀態:正常,獲取焦點,點擊。

 

【以上爲原創,轉載請註明出處:http://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx

 

附docx文件下載】需要收費,您的支持就是我的動力,謝謝啦

 http://www.docin.com/DocinViewer-60345763-144.swf

發佈了66 篇原創文章 · 獲贊 9 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章