Android ImageButton

我怕爱的太早我们不能终老 提交于 2019-12-02 03:27:22
public class
ImageButton
extends ImageView
java.lang.Object
   ↳	android.view.View
 	   ↳	android.widget.ImageView
 	 	   ↳	android.widget.ImageButton
直接子类
ZoomButton

ImageButton是上面带有图片的Button,与Button只是类似,按钮表面上的图像的定义是在xml文件中通过android:src或者是在java代码中通过imagebutton组件对象调用setImageResource(int a)方法来定义的。

在xml中实现:

<?xml version="1.0" encoding="utf-8"?>
<ImageButton 
	android:id="@+id/login_btn_id"
	android:layout_height="wrap_content"
	android:src="@drawable/button_2" //不是系统图片
    或者	Android:src="@android:drawable/sym_call_incoming" //系统自带的图片
		  
android:layout_margin="5px"
/>

在代码中实现:

//drawable下的图片 

m_ImageButton.setImageDrawable(getResources().getDrawable(R.drawable.my_button)); 

//系统自带的图片 
 m_ImageButton.setImageDrawable(getResources().getDrawable(Android.R.drawable.sym_call_incoming));

ImageButton设置图片方式,有以下三种:


setImageBitmap(Bitmap bm) 
setImageDrawable(Drawable drawable) 
setImageResource(int resId)
设置透明背景可以通过设置background完成

android:background="#00000000"即可

半透明 

android:background="#7F000000"即可。

表示不同的按钮状态,你能够为每一个状态定义一个相对应的图片,一个很简单的方式就是通过xml绘制“selector

例如:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/button_pressed" /> <!-- pressed 按下去-->
     <item android:state_focused="true"
           android:drawable="@drawable/button_focused" /> <!-- focused 焦点停留-->
     <item android:drawable="@drawable/button_normal" /> <!-- default正常默认 -->
 </selector>

元素的顺序是很重要的,因为他们是按顺序执行的,这就是为什么normal按钮放在在最后了,因为在android:state_pressed and android:state_focused评估失效之后normal才被应用的。

state_enabled 是否有效
state_focused 是否聚焦
state_pressed 是否被按下
其中state_focused 和 state_pressed 可自由有如下4种组合

android:state_focused="true" android:state_pressed="true"
android:state_focused="true" android:state_pressed="false"
android:state_focused="false" android:state_pressed="true"
android:state_focused="false" android:state_pressed="false"

方法:

protected boolean onSetAlpha (int alpha);//设置透明度,参数alpha的取值范围是0到255,如果视图可以得出制定的alphe就会返回一个true

imagebutton.setVisibility(View.GONE);//隐藏ImageButton1
imagebutton.setVisibility(View.VISIBLE);//显示ImageButton2
imagebutton.setImageResource(R.drawable.icon2);//设置内容

ImageButton按下和释放背景图片改变事件:

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;  
                        }   
                });   

设置背景色:

m_ll.setBackgroundColor(Color.rgb(127,127,127)); 

m_ll.setBackgroundColor(Color.TRANSPARENT); 

三:以上方式比较简单,但是需要很多的图片和布局文件,如果项目中的图片按钮比较多,那就很浪费资源。下面的方式使用矩阵颜色滤镜。

颜色过滤矩阵是一个4x5的矩阵, 四行分别是 红色通道值,绿色通道值,蓝色通道值和alpha通道值。五列分别是 对应通道的红色值,绿色值,蓝色值,alpha值和偏移量。

RGBAlpha的终值计算方法如下:

Red通道终值 = a[0] * srcR + a[1] * srcG + a[2] * srcB + a[3] * srcA + a[4]

Green通道终值 = a[5] * srcR + a[6] * srcG + a[7] * srcB + a[8] * srcA + a[9]

Blue通道终值 = a[10] * srcR + a[11] * srcG + a[12] * srcB + a[13] * srcA + a[14]

Alpha通道终值 = a[15] * srcR + a[16] * srcG + a[17] * srcB + a[18] * srcA + a[19]

备注:

srcR为原图Red通道值,srcG为原图Green通道值,srcB为原图Blue通道值,srcA为原图Alpha通道值。

每个通道的源值和终值都在0255的范围内。即使计算结果大于255或小于0,值都将被限制在0255的范围内。

实现代码如下:

采用Drawable的颜色过滤:

/**   
   * 按下这个按钮进行的颜色过滤   
   */    
  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 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);  
 }  


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!