`
marine8888
  • 浏览: 540861 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Android——ImageButton按下效果设计

阅读更多

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

1.Java代码实现

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

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

 3.采用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);  
 }  

 使用时,调用方法:public final static void setButtonFocusChanged(View inView)即可。

 

FROM: http://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx

分享到:
评论

相关推荐

    安卓Android源码——ImageButton.rar

    安卓Android源码——ImageButton.rar

    好的-Android2.2 API中文文档——ImageButton.doc

    Android2.2 API中文文档——ImageButton.doc

    安卓Android源码——(ImageButton图片按钮).zip

    安卓Android源码——(ImageButton图片按钮).zip

    安卓Android源码——(ImageButton图片按钮).rar

    安卓Android源码——(ImageButton图片按钮).rar

    Android2.2 API 中文文档系列(7) —— ImageButton

    Android2.2 API 中文文档,注意这里只有ImageButton的,请关注http://over140.cnblogs.com/的更新。

    ImageButton Android开发实例

    Android开发实例——ImageButton源代码,图像修饰按钮的一个例子,Andorid手机上的程序界面设计都挺漂亮,这一个ImageButton也是很不错的例子。例子中包括所用到的图像资源,很不错。

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——...

    带图片的按钮ImageButton源码

    陆陆续续完成了ImageView和Button控件的讲解之后,我们设想在Android中有没有...答案是——有的,本节内容我就来介绍这个ImageView和Button的和产物ImageButton。课程见:http://www.ourunix.org/android/post/126.html

    Google Android SDK开发范例大全的目录

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右...

    Android2.2 API中文文档

    (7) —— ImageButton (8) —— QuickContactBadge (9) —— ZoomButton (10) —— CheckBox (11) —— RadioButton (12) —— Button (13) —— ToggleButton (14) —— ViewStub (15)—— ...

    android开发入门与实战(下)

    第1章 掀起你的盖头来——初识Android 1.1 认识Android 1.2 Android的背景 1.2.1 Android的历史 1.2.2 Android的发展 1.3 我的Android我做主 1.3.1 开发基于Android平台的应用 1.3.2 参加Android开发者大赛 1.3.3 ...

    Google+Android+SDK开发范例大全

    ——具选择功能的对话框 3.21 Android变脸——主题(Theme)实现 第4章 史上超豪华的手机控件 4.1 EditText与TextView共舞——setOnKeyListener事件 4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给...

    Google Android SDK 开发范例大全01

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——...

    Google Android SDK 开发范例大全02

    4.2 设计具有背景图的按钮——ImageButton的焦点及事件处理 4.3 给耶诞老人的信息——Toast对象的使用 4.4 我同意条款——CheckBox的isChecked属性 4.5 消费券采购列表——多选项CheckBox的应用 4.6 向左或向右——...

    Android编程入门很简单.(清华出版.王勇).part1

    5.2.7 使用图片按钮——ImageButton 5.2.8 使用复选框——CheckBox 5.2.9实例——请同意本协议 5.2.10 使用单选框——RadioGroup 5.2.11 实例——请选择性别 5.2.12使用下拉列表框——Spinner 5.2.13实例——请选择...

Global site tag (gtag.js) - Google Analytics