日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

ImageButton自定義按鈕的按下效果的高效實(shí)現(xiàn)方

系統(tǒng) 2034 0
通常情況下,我們可以采用如下方式實(shí)現(xiàn):
    <?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> 
  

把這個(gè)文件放在drawable目錄下面。命名為button_add_x.xml
使用的時(shí)候:
    
<ImageButton    
                        android:id="@+id/ImageButton"    
                        android:layout_width="wrap_content"    
                        android:layout_height="wrap_content"    
                        android:background="#00000000"    
                        android:src="@drawable/button_add_x" /> 

  

這樣的實(shí)現(xiàn)過程雖然通用性好,但是很麻煩,一個(gè)按鈕實(shí)現(xiàn)效果需要多張圖片甚至再加一個(gè)布局…
那一個(gè)游戲要是有幾百個(gè)按鈕怎么辦呢?
于是:以下代碼被醞釀出來了:
    
/**  
   * 按下這個(gè)按鈕進(jìn)行的顏色過濾  
   */  
  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 };   
     
  /**  
   * 按鈕恢復(fù)原狀的顏色過濾  
   */  
  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 };   
     
  /**  
   * 按鈕焦點(diǎn)改變  
   */  
  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;   
  }   
 };   
    
 /**  
  * 設(shè)置圖片按鈕獲取焦點(diǎn)改變狀態(tài)  
  * @param inImageButton  
  */  
 public final static void setButtonFocusChanged(View inView)   
 {   
  inView.setOnTouchListener(buttonOnTouchListener);   
  inView.setOnFocusChangeListener(buttonOnFocusChangeListener);   
 }  

  

使用時(shí),調(diào)用方法public final static void setButtonFocusChanged(View inView)即可。
【原理】
利用Drawable類的setColorFilter方法對(duì)圖片進(jìn)行顏色偏移過濾處理。

以下為效果圖,登陸按鈕此時(shí)為獲取焦點(diǎn)狀態(tài)。
ImageButton自定義按鈕的按下效果的高效實(shí)現(xiàn)方法(非一般)
代碼可以適當(dāng)修改實(shí)現(xiàn)3個(gè)不同的狀態(tài):正常,獲取焦點(diǎn),點(diǎn)擊。

http://blog.csdn.net/sytzz/article/details/5673662

ImageButton自定義按鈕的按下效果的高效實(shí)現(xiàn)方法(非一般)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 和林格尔县| 修水县| 武穴市| 乐陵市| 中宁县| 上杭县| 揭阳市| 宜良县| 龙州县| 上犹县| 怀来县| 云龙县| 利津县| 哈密市| 元朗区| 鹰潭市| 和平县| 开鲁县| 泸溪县| 新疆| 德保县| 铜川市| 灵寿县| 深州市| 惠东县| 平陆县| 金湖县| 仙游县| 射阳县| 会昌县| 方山县| 家居| 永昌县| 雷州市| 张家界市| 平泉县| 邮箱| 望城县| 武宁县| 科技| 江安县|