`
sunshangfeng
  • 浏览: 11796 次
  • 性别: Icon_minigender_1
文章分类
社区版块
存档分类
最新评论

ImageButton 点击效果实现

 
阅读更多

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

1.在代码里面。

view plain

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文件实现。

view plain

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

使用的时候

view plain

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

 

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

 

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

 

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

 

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

 

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

 

view plain

 

  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;     

  }     

 };     

 

 

 public final static void setButtonFocusChanged(View inView)     

 {     

  inView.setOnTouchListener(buttonOnTouchListener);     

  inView.setOnFocusChangeListener(buttonOnFocusChangeListener);     

 }    

 

使用时,调用方法

public final static void setButtonFocusChanged(View inView)

即可。

 

【原理】

 

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

分享到:
评论

相关推荐

    Android 点击ImageButton时有“按下”的效果的实现

    主要介绍了 Android 点击ImageButton时有“按下”的效果的实现的相关资料,需要的朋友可以参考下

    Android 单击选中的ImageButton[]图像数组用法示例.rar

    Android 单击选中的ImageButton[]图像数组用法示例,类似于Radio的功能,不过用图片表现,这样可使Android UI更加友好,视觉更漂亮一些,用户轻触图片,即可选中该数据项,比Radio用户体验更好,如截图所示的选中...

    Android自定义控件ImageView实现点击之后出现阴影效果

    今天美工 直接给我一张图片,要我实现图片点击之后有阴影效果,当时想到了ImageButton,随即自己写了个Demo,发现ImageButton继承ImageView 会有一个默认的背景样式,而且在布局中设计src(前景) 太丑,于是自己写了...

    使用ViewPager来实现Tab效果

    使用ViewPager来实现Tab效果,解决了ImageButton不可点击的Bug

    asp实现图片动态切换

    图片轮换,图片特效,动态切换,图片轮换,图片特效,动态切换

    Android实现图片点击预览效果(zoom动画)

    1.创建Views 下面的布局包括了你想要zoom的大版本和小版本...3.ImageView一开始是不可见的(invisible),当ImageButton点击后,它会实现zoom动画,就像从ImageButton上扩大显示出来。 &lt;FrameLayout xmlns:android

    自定义按钮

    在实际开发中Android中自带的控件有时无法满足我们的需求,...比如我想使Button有按下和弹起效果还可以写文字,就没有哪个原生的控件能满足我们的需求,在这里我选择重载ImageButton,在ImageButton的基础上添加文字

    嵌入式实验-简单电子相册

    设置3个imagebutton,点击可以分别进入3个不同的相册;浏览图片时,当前浏览图片可以停留在界面中间,且有放大效果;点击图片可以显示当前浏览图片是第几张;给图片加倒影效果,使相册看起来更立体、生动。

    Android 开发技巧

    5.3、IMAGEBUTTON 按下时的动画效果 142 5.4、滚动条显示与隐藏 143 5.5、LISTVIEW 与 SCROLLVIEW 解决办法 144 方法一:(重写ListView) 144 方法二: 150 5.6、3D魔方 151 6、ANDROID UI 动画 160 6.1、四种2D...

    Android开发资料合集-World版!

    5.3、IMAGEBUTTON 按下时的动画效果 142 5.4、滚动条显示与隐藏 143 5.5、LISTVIEW 与 SCROLLVIEW 解决办法 144 方法一:(重写ListView) 144 方法二: 150 5.6、3D魔方 151 6、ANDROID UI 动画 160 6.1、四种2D...

    asp.net知识库

    使用microsoft.web.ui.webcontrols的TabStrip与IFame组件,达到页的切换效果 HttpModule 实现 ASP.Net (*.aspx) 中文简繁体的自动转换,不用修改原有的任何代码,直接部署即可! 服务器自定义开发二之客户端脚本回发 Web...

    ExtAspNet_v2.3.2_dll

    -使得Asp.net的控件ImageButton具有和Asp.net的Button控件类似的行为(Ajax提交)(feedback:261629698)。 +TabStrip增加GetAddTabReference和GetRemoveTabReference两个函数,用来向TabStrip控件动态增加删除Tab。...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    ExtAspNet是一组专业的Asp.net控件库,拥有原生的AJAX支持和丰富的UI效果, 目标是创建没有JavaScript,没有CSS,没有UpdatePanel,没有WebServices的Web应用程序。 支持的浏览器: IE 7.0+, Firefox 3.0+, Chrome ...

Global site tag (gtag.js) - Google Analytics