Android ImageButton自定义按钮的按下效果的代码实现方法分享
时间:2021-11-20 13:52:07
使用Button时为了让用户有“按下”的效果,有两种实现方式:
1.在代码里面。
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>
这个文件放在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>
我自己摸索摸索,发现这样的实现过程虽然通用性好,但是很麻烦,一个按钮实现效果需要多张图片甚至再加一个布局…
那一个游戏要是有几百个按钮怎么办呢?
于是:以下代码被酝酿出来了:
/**
* 按下这个按钮进行的颜色过滤
*/
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)
即可。
【原理】
利用Drawable类的setColorFilter方法对图片进行颜色偏移过滤处理。
标签:ImageButton,自定义按钮,按下效果
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springcloud配置ssh的问题及解决方法
2023-11-03 15:48:11
![](https://img.aspxhome.com/file/2023/7/58757_0s.jpg)
Java命令设计模式优雅解耦命令和执行提高代码可维护性
2023-11-23 06:25:46
![](https://img.aspxhome.com/file/2023/1/59861_0s.png)
JavaWeb实现简单文件上传功能
2022-11-19 04:15:41
![](https://img.aspxhome.com/file/2023/1/86791_0s.jpg)
java实现给图片加铺满的网格式文字水印
2023-07-30 05:22:12
![](https://img.aspxhome.com/file/2023/9/57809_0s.jpg)
SpringMvc web.xml配置实现原理过程解析
2023-04-14 01:42:27
java多线程CountDownLatch与线程池ThreadPoolExecutor/ExecutorService案例
2021-06-21 12:29:50
![](https://img.aspxhome.com/file/2023/8/64388_0s.jpg)
SpringBoot整合Thymeleaf小项目及详细流程
2021-08-13 16:32:08
![](https://img.aspxhome.com/file/2023/0/80890_0s.png)
Intellij IDEA 2018配置Java运行环境的方法步骤
2023-06-08 09:56:50
![](https://img.aspxhome.com/file/2023/6/62456_0s.png)
java序列化和java反序列化示例
2023-11-24 04:01:03
java微信公众号开发(搭建本地测试环境)
2021-07-07 05:32:40
![](https://img.aspxhome.com/file/2023/6/63076_0s.png)
C#多线程等待所有子线程结束的示例
2021-07-02 23:04:57
Unity中的PostProcessScene实用案例深入解析
2021-06-09 04:57:28
SpringCloud Zuul基本使用方法汇总
2023-11-14 15:21:54
maven中下载jar包源码和javadoc的命令介绍
2023-07-27 04:41:01
![](https://img.aspxhome.com/file/2023/3/61033_0s.png)
idea无法切换分支报错问题及解决
2023-12-14 22:39:26
![](https://img.aspxhome.com/file/2023/2/64552_0s.png)
java基础的详细了解第六天
2021-11-05 16:18:49
![](https://img.aspxhome.com/file/2023/8/65348_0s.png)
Java接口继承和使用接口操作示例
2023-11-29 05:08:43
java秒杀之redis限流操作详解
2022-07-08 09:26:57
挑战4道Java试题
2022-04-12 04:41:02
SpringBoot+Elasticsearch实现数据搜索的方法详解
2023-04-17 02:45:46