Android ImageButton自定义按钮的按下效果的代码实现方法分享
发布时间:2021-11-20 13:52:07
标签:ImageButton,自定义按钮,按下效果
使用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方法对图片进行颜色偏移过滤处理。


猜你喜欢
- 前言目前Google已经发布了Android13的正式版,虽然国内的手机能用上Android13还有一段时间,不过开发者们可以通过模拟器来体
- 首先是.select在MP查询中,默认查询所有的字段,如果有需要也可以通过select方法进行指定字段。其中要注意的细节:wrapper.s
- eclipse配置tomcattomcat资源下载如果打开没有servers请查看这篇文章:eclipse创建项目没有dynamic web
- 本文实例讲述了java自动生成ID号的方法。分享给大家供大家参考。具体实现方法如下:import java.util.UUID;public
- 目录1 HttpClient简介2 代码实现2.1 服务端2.1.1 新建控制器2.1.2 新建启动器2.2 客户端2.2.1 添加依赖2.
- 本文实例讲述了基于WebClient实现Http协议的Post与Get对网站进行模拟登陆和浏览的方法。分享给大家供大家参考。具体分析如下:一
- 一.内容抽象类当编写一个类时,常常会为该类定义一些方法,这些方法用于描述这个类的行为。但在某些情况下只需要定义出一些方法,而不需要具体的去实
- 前言在这个 Spring Security 教程中,我很乐意与您分享如何通过在 Java Web 应用程序中为用户添加角色来实现授权&
- 一、项目简述功能: 主页显示商品; 所有蛋糕商品展示,可进行商品搜索; 点击商品进入商品详情页,具有立即购买功能,可增减购买商品数量亦可手动
- 如下所示:package com.lcn.day05;import java.util.Scanner;public class Array
- 摘要本文主要介绍基于SpringBoot定时任务ScheduledTaskRegistrar的动态扩展,实现定时任务的动态新增和删除。Sch
- java解析json数组最简单的json数组[ { &quo
- 在C语言中一般用typedef来为回调函数定义别名(参数名)。 别名通过宏定义typedef来实现,不是简单的宏替换。可以用作同
- 在 Eclipse 里新建好工程后,默认会有一个assets目录,在 Eclipse 中直接将准备好的 SQLite 数据库复制到该目录中,
- 今天想和小伙伴们来聊一聊 Spring Security 中的角色继承问题。角色继承实际上是一个很常见的需求,因为大部分公司治理可能都是金字
- 在线程中有两种常用的方法,能够通过数组实现相应的功能,但除此之外在区别上也是很明显的。本篇就其中的代表方法ArrayList和Vector进
- 本文开始做一个网上商城的项目,首先从搭建环境开始,一步步
- 演示 - 初始化销毁顺序/* 初始化和销毁的执行顺序 */@SpringBootApplicationpublic
- 业务需要动态给图片增加文字(书本的封面图片),修改字体大小、字体、颜色、控制位置测试代码:string path = @"E:\c
- 引看懂这张图,方法调用方法,栈开新栈,递归尾结束要回到main栈,必须一级一级返回,每一次返回都是调用整个方法,调用完成栈被释放,直至回到栈