国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android ImageButton自定義按鈕的按下效果的代碼實現方法分享

2020-04-11 11:55:10
字體:
來源:轉載
供稿:網友

使用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文件實現。

復制代碼 代碼如下:

<?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
使用的時候

復制代碼 代碼如下:

<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>

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

復制代碼 代碼如下:

/** 
   * 按下這個按鈕進行的顏色過濾 
   */ 
  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方法對圖片進行顏色偏移過濾處理。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐平市| 托克托县| 白河县| 旌德县| 祁阳县| 黄陵县| 尉犁县| 香格里拉县| 理塘县| 八宿县| 蒙山县| 邢台市| 仁寿县| 马边| 土默特右旗| 咸阳市| 齐齐哈尔市| 惠州市| 海城市| 枣强县| 澜沧| 宁明县| 南召县| 拜泉县| 玛纳斯县| 陇川县| 曲阳县| 通道| 武陟县| 波密县| 中宁县| 时尚| 万荣县| 南投市| 元朗区| 库尔勒市| 太康县| 苗栗市| 辽中县| 青神县| 葵青区|