Android ImageButton变色

来源:互联网 发布:无标度网络应用 编辑:程序博客网 时间:2024/03/28 23:57

首先看看网上的2种方法:

【以下为引用网络,来源:http://www.eoeandroid.com/thread-7931-1-1.html

使用Button时为了让用户有“按下”的效果,有两种实现方式:

1.在代码里面。

 

view plaincopy to clipboardprint?
  1. imageButton.setOnTouchListener(new OnTouchListener(){     
  2.                         @Override    
  3.                         public boolean onTouch(View v, MotionEvent event)     
  4.                                 if(event.getAction() == MotionEvent.ACTION_DOWN){     
  5.                                         //更改为按下时的背景图片     
  6.                                         v.setBackgroundResource(R.drawable.pressed);     
  7.                                 }else if(event.getAction() == MotionEvent.ACTION_UP){     
  8.                                         //改为抬起时的图片     
  9.                                         v.setBackgroundResource(R.drawable.released);     
  10.                                     
  11.                                 return false    
  12.                             
  13.                 });    
  14. imageButton.setOnTouchListener(new OnTouchListener(){  
  15.                         @Override  
  16.                         public boolean onTouch(View v, MotionEvent event)  
  17.                                 if(event.getAction() == MotionEvent.ACTION_DOWN){  
  18.                                         //更改为按下时的背景图片  
  19.                                         v.setBackgroundResource(R.drawable.pressed);  
  20.                                 }else if(event.getAction() == MotionEvent.ACTION_UP){  
  21.                                         //改为抬起时的图片  
  22.                                         v.setBackgroundResource(R.drawable.released);  
  23.                                  
  24.                                 return false 
  25.                           
  26.                 });   
  

 

 

2.XML文件实现。

 

 

view plaincopy to clipboardprint?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">    
  3.     <item           android:state_pressed="false"  android:drawable="@drawable/button_add" />    
  4.     <item           android:state_pressed="true"   android:drawable="@drawable/button_add_pressed" />    
  5.     <item           android:state_focused="true"    android:drawable="@drawable/button_add_pressed" />    
  6. <item           android:drawable="@drawable/button_add" />    
  7. </selector>    
  8.   
  9. <?xml version="1.0" encoding="UTF-8"?>  
  10. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  11. <item           android:state_pressed="false"  android:drawable="@drawable/button_add" />  
  12.   
  13.     <item            android:state_pressed="true"  android:drawable="@drawable/button_add_pressed" />  
  14.     <item            android:state_focused="true"  android:drawable="@drawable/button_add_pressed" />  
  15.     <item             android:drawable="@drawable/button_add" />  
  16. </selector>   
  

 

 

这个文件放在drawable目录下面。命名为button_add_x.xml

使用的时候

 

 

view plaincopy to clipboardprint?
  1. <ImageButton    
  2.                         android:id="@+id/ImageButton"    
  3.                         android:layout_width="wrap_content"    
  4.                         android:layout_height="wrap_content"    
  5.                         android:background="#00000000"    
  6.                         android:src="@drawable/button_add_x" >    
  7. </ImageButton>    
  8. <ImageButton  
  9.                         android:id="@+id/ImageButton"  
  10.                         android:layout_width="wrap_content"  
  11.                         android:layout_height="wrap_content"  
  12.                         android:background="#00000000"  
  13.                         android:src="@drawable/button_add_x" >  
  14. </ImageButton>   

 

 

【以上为引用网络,来源:http://www.eoeandroid.com/thread-7931-1-1.html

 

【以下为原创,转载请注明出处:http://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx

 

我自己摸索摸索,发现这样的实现过程虽然通用性好,但是很麻烦,一个按钮实现效果需要多张图片甚至再加一个布局…

 

那一个游戏要是有几百个按钮怎么办呢?

 

于是:以下代码被酝酿出来了:

 

 

 

view plaincopy to clipboardprint?
  1.     
  2.   public final static float[] BT_SELECTED=new float[]      
  3.       20002     
  4.       02002     
  5.       00202     
  6.       00010 };     
  7.        
  8.       
  9.   public final static float[] BT_NOT_SELECTED=new float[]      
  10.       10000     
  11.       01000     
  12.       00100     
  13.       00010 };     
  14.        
  15.       
  16.   public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener()     
  17.        
  18.   @Override    
  19.   public void onFocusChange(View v, boolean hasFocus)     
  20.    if (hasFocus)     
  21.     v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));     
  22.     v.setBackgroundDrawable(v.getBackground());     
  23.        
  24.    else    
  25.        
  26.     v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));     
  27.      v.setBackgroundDrawable(v.getBackground());     
  28.        
  29.       
  30.  };     
  31.       
  32.       
  33.  public final static OnTouchListener buttonOnTouchListener=new OnTouchListener()     
  34.   @Override    
  35.   public boolean onTouch(View v, MotionEvent event)     
  36.    if(event.getAction() == MotionEvent.ACTION_DOWN){     
  37.     v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));     
  38.     v.setBackgroundDrawable(v.getBackground());     
  39.         
  40.     else if(event.getAction() == MotionEvent.ACTION_UP){     
  41.      v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));     
  42.      v.setBackgroundDrawable(v.getBackground());     
  43.         
  44.    return false    
  45.       
  46.  };     
  47.       
  48.      
  49.  public final static void setButtonFocusChanged(View inView)     
  50.      
  51.   inView.setOnTouchListener(buttonOnTouchListener);     
  52.   inView.setOnFocusChangeListener(buttonOnFocusChangeListener);     
  53.     
  54.   
  55.   public final static float[] BT_SELECTED=new float[]   
  56.       20002  
  57.       02002  
  58.       00202  
  59.       00010 };  
  60.     
  61.     
  62.   public final static float[] BT_NOT_SELECTED=new float[]   
  63.       10000  
  64.       01000  
  65.       00100  
  66.       00010 };  
  67.     
  68.     
  69.   public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener()  
  70.     
  71.   @Override  
  72.   public void onFocusChange(View v, boolean hasFocus)  
  73.    if (hasFocus)  
  74.     v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));  
  75.     v.setBackgroundDrawable(v.getBackground());  
  76.     
  77.    else  
  78.     
  79.     v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));  
  80.      v.setBackgroundDrawable(v.getBackground());  
  81.     
  82.    
  83.  };  
  84.    
  85.     
  86.  public final static OnTouchListener buttonOnTouchListener=new OnTouchListener()  
  87.   @Override  
  88.   public boolean onTouch(View v, MotionEvent event)  
  89.    if(event.getAction() == MotionEvent.ACTION_DOWN){  
  90.     v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));  
  91.     v.setBackgroundDrawable(v.getBackground());  
  92.      
  93.     else if(event.getAction() == MotionEvent.ACTION_UP){  
  94.      v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));  
  95.      v.setBackgroundDrawable(v.getBackground());  
  96.      
  97.    return false 
  98.    
  99.  };  
  100.    
  101.    
  102.  public final static void setButtonFocusChanged(View inView)  
  103.   
  104.   inView.setOnTouchListener(buttonOnTouchListener);  
  105.   inView.setOnFocusChangeListener(buttonOnFocusChangeListener);  
  106.   
  

 

使用时,调用方法

public final static void setButtonFocusChanged(View inView)

即可。

 

【原理】

 

利用Drawable类的setColorFilter方法对图片进行颜色偏移过滤处理。

 

 

以下为效果图,登陆按钮此时为获取焦点状态。

 

 

 

 

代码可以适当修改实现3个不同的状态:正常,获取焦点,点击。

原创粉丝点击