[Android] 代码设计中动态设置button的Pressed态图片
2016-08-08 22:33
856 查看
【需求说明】
在代码中动态设置某一个ImageButton的press态图片。对于Button的press态逻辑设计,前面的文章中曾经介绍过 用Selector设置pressed态方式
但是现在有一个新的需求:在代码中动态设置ImageButton的Press态动画。既然可以用selector直接设置button的press态和normal态,那为何还要在代码中动态调整button的
pressed态呢?
这恰好来自于目前所做项目中的一个需求:所有的Activity都继承自一个AppBarActivity,但是对于不同的Activity,对应的AppBar的右边的ImageButton需要有不同的图片显示,
且都具有pressed状态。如果还是以selector方式去做,那么每一个Activity都需要在xml中配置appbar.xml的内容,显然这样没有做到很好的功能复用。而有了代码动态设置Button
的pressed态后,这个问题就很好解决了。只需要在创建各个Activity时传入各自的nomal和press资源ID,在appBar中动态监测imageButton的pressed态,然后设置相应的背景图片即可。
【实现说明】
实现此功能的核心是:ImageButton监听touch事件,当接收到点击按下动作时,设置相应的press资源,点击弹起后,恢复默认的图片资源。
final Button btn = (Button) findViewById(R.id.btn);
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
btn.setBackgroundResource(R.mipmap.btn_status_pressed);
btn.setTextColor(getResources().getColor(R.color.text_pressed));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
btn.setBackgroundResource(R.mipmap.btn_status_normal);
btn.setTextColor(getResources().getColor(R.color.text_normal));
}
return false;
}
});
【效果展示】
一句话总结:Button响应setOnTouchListener事件,在点击按下动作时,设置pressed态图片,当弹起后,设置normal图片
在代码中动态设置某一个ImageButton的press态图片。对于Button的press态逻辑设计,前面的文章中曾经介绍过 用Selector设置pressed态方式
但是现在有一个新的需求:在代码中动态设置ImageButton的Press态动画。既然可以用selector直接设置button的press态和normal态,那为何还要在代码中动态调整button的
pressed态呢?
这恰好来自于目前所做项目中的一个需求:所有的Activity都继承自一个AppBarActivity,但是对于不同的Activity,对应的AppBar的右边的ImageButton需要有不同的图片显示,
且都具有pressed状态。如果还是以selector方式去做,那么每一个Activity都需要在xml中配置appbar.xml的内容,显然这样没有做到很好的功能复用。而有了代码动态设置Button
的pressed态后,这个问题就很好解决了。只需要在创建各个Activity时传入各自的nomal和press资源ID,在appBar中动态监测imageButton的pressed态,然后设置相应的背景图片即可。
【实现说明】
实现此功能的核心是:ImageButton监听touch事件,当接收到点击按下动作时,设置相应的press资源,点击弹起后,恢复默认的图片资源。
final Button btn = (Button) findViewById(R.id.btn);
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
btn.setBackgroundResource(R.mipmap.btn_status_pressed);
btn.setTextColor(getResources().getColor(R.color.text_pressed));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
btn.setBackgroundResource(R.mipmap.btn_status_normal);
btn.setTextColor(getResources().getColor(R.color.text_normal));
}
return false;
}
});
【效果展示】
一句话总结:Button响应setOnTouchListener事件,在点击按下动作时,设置pressed态图片,当弹起后,设置normal图片
相关文章推荐
- android 读取sd卡(sdcard)图片设置为按钮背景(Button背景设置)代码
- Android apk代码中动态设置背景图片
- Android代码中动态设置图片的大小(自动缩放),位置
- Android给Button设置drawableRigh 在代码中动态改变
- Android 代码里动态设置TextView/Button等的文字颜色Seletor
- Android代码中动态设置图片的大小(自动缩放),位置
- Android在代码中动态设置文本的图片的显示
- android 代码设置PNG图片失真问题
- android 动态实现点击ImageButton更换图片
- Android代码code设置9.png/9-patch 图片背景后,此view中的控件显示不正常
- Android Layout 之 RelativeLayout,代码实现相对布局&android的button设置透明
- android设置图片变化的四种效果代码
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- android中用xml文件设置button的各个状态的图片。
- android 使用.9图片的background的button的text设置问题
- 获取android 手机屏幕的大小以及动态设置背景图片的两种方法;
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- Android 在代码里动态设置ProgressBar的颜色
- android Button,textView 等控件设置控件内嵌周围图片使用方法