Android ImageButton custom button press effect code implementation method sharing

  • 2020-05-24 06:08:30
  • OfStack

In order to make the user have the effect of "pressing" when using Button, there are two implementations:
1. In the code.


imageButton.setOnTouchListener(new OnTouchListener(){  

                        @Override  
                        public boolean onTouch(View v, MotionEvent event) {  
                                if(event.getAction() == MotionEvent.ACTION_DOWN){  
                                        // Change to the background image when pressed   
                                        v.setBackgroundResource(R.drawable.pressed);  
                                }else if(event.getAction() == MotionEvent.ACTION_UP){  
                                        // Change the picture when lifting   
                                        v.setBackgroundResource(R.drawable.released);  
                                }  
                                return false;  
                        }  

                });  

2. Implement with XML file.


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

This file is located under the drawable directory. Named button_add_x xml
When you use it


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

I fumbled and found that although the process of such realization is universal, it is very troublesome. One button needs more pictures and even one layout to realize the effect.
What if a game has hundreds of buttons?
Thus: the following code is brewed:


/**  
   *  Press this button to filter the color   
   */  
  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 };   

  /**  
   *  Button to restore the original color filter   
   */  
  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 };   

  /**  
   *  Button focus change   
   */  
  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());   
   }   
  }   
 };   

  /**  
   *  Button touch press effect   
   */  
 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;   
  }   
 };   

 /**  
  *  Set the image button to get the focus change state   
  * @param inImageButton  
  */  
 public final static void setButtonFocusChanged(View inView)   
 {   
  inView.setOnTouchListener(buttonOnTouchListener);   
  inView.setOnFocusChangeListener(buttonOnFocusChangeListener);   
 }  

When used, the method is called
public final static void setButtonFocusChanged(View inView)
Can.
[principle]
The setColorFilter method of Drawable class is used to filter the image color shift.


Related articles: