[转]永久告别Android的背景选择器Selector!无需切很多图了!
package com.zoke.custom.autobg; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Button; import com.zoke.custom.R; /**
* Android按钮显示不同状态无需多张图片的自定义控件
*
* @author Jack 2014-2-8
*
*
*/
public class AutoBgButton extends Button { public AutoBgButton(Context context) {
super(context);
} public AutoBgButton(Context context, AttributeSet attrs) {
super(context, attrs);
} public AutoBgButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setBackgroundDrawable(Drawable d) {
// 替换背景图
AutoBgButtonBackgroundDrawable layer = new AutoBgButtonBackgroundDrawable(
d);
super.setBackgroundDrawable(layer);
} /**
* 按钮所使用的状态图
*/
protected class AutoBgButtonBackgroundDrawable extends LayerDrawable { // 按钮按下时所使用的颜色过滤器
protected ColorFilter _pressedFilter = new LightingColorFilter(
getContext().getResources().getColor(R.color.pressColor), 1);
// 按钮被禁用时的alpha值
protected int _disabledAlpha = 100; public AutoBgButtonBackgroundDrawable(Drawable d) {
super(new Drawable[] { d });
} @Override
protected boolean onStateChange(int[] states) {
boolean enabled = false;
boolean pressed = false; for (int state : states) {
if (state == android.R.attr.state_enabled)
enabled = true;
else if (state == android.R.attr.state_pressed)
pressed = true;
} mutate();
if (enabled && pressed) {
setColorFilter(_pressedFilter);
} else if (!enabled) {
setColorFilter(null);
setAlpha(_disabledAlpha);
} else {
setColorFilter(null);
} invalidateSelf(); return super.onStateChange(states);
} @Override
public boolean isStateful() {
return true;
}
} }
[转]永久告别Android的背景选择器Selector!无需切很多图了!的更多相关文章
- Android:关于背景选择器Selector的item顺序
在使用背景选择器的时候,如果item的顺序不对,会导致不起作用. 1.首先背景选择器的normal选项一定要放在最后. 2.pressed的选择器应该在seclet的前面.我在使用的时候找了半天问题, ...
- android背景选择器selector用法汇总
一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...
- 【Android】Android背景选择器selector用法汇总
一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...
- Android View 背景选择器编写技巧
在项目中选择器的使用是非常多的,以下是本人在项目中的一些常用的背景选择器的写法 带边框下划线背景选择器效果图: 上面布局中放了10个CheckBox,然后设置了CheckBox的背景图片位,背景选择器 ...
- AndroidA——背景选择器selector用法汇总(一)
一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...
- Android selecter背景选择器使用
android:drawable这个属性是必须的,默认时的背景图片. android:state_pressed布尔值.true指当用户点击或者触摸该控件的状态.默认为false android:st ...
- android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现
android selector 背景选择器的使用, button (未点击,点击,选中保持状态)效果实现 首先看到selector的属性: android:state_focus ...
- Android中selector背景选择器
http://blog.csdn.net/forsta/article/details/26148403 http://blog.csdn.net/wswqiang/article/details/6 ...
- 030 Android 第三方开源下拉框:NiceSpinner的使用+自定义Button样式+shape绘制控件背景图+图片选择器(selector)
1.NiceSpinner下拉框控件介绍 Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框 ...
随机推荐
- NodeJS学习:爬虫小探补完计划
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 书接上回,我们需要修改程序以达到连续抓取40个页面的内容.也就是说我们需要输出每篇文章的标题.链接.第一条评论.评论用户和论坛 ...
- Spring+Quartz 整合一:常规整合
步骤一: 定时任务需要一个配置文件(spring-mvc-timeTask.xml 随便起名),将其在web.xml中加载 <context-param> <param-name&g ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- zoj 1610 Count the Colors
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610 Count the Colors Time Limit:2000MS ...
- JEECMS-自定义标签[list]
1.自定义标签类 import static cn.com.yhxl.common.web.freemarker.DirectiveUtils.OUT_LIST; import static free ...
- UI进阶 CocoaPods的安装使用步骤
一. CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具.在开发iOS应用时,会经常使用第三方类库,比如SDWebImage.AFNetworking等等,手动的下载与 ...
- main函数是个什么东西
习惯的main函数有无参和两个参数的版本,那么main函数只能这么写吗? 好奇写了一个bug版本的main,结果是呵呵 #include <iostream> ...
- NOSQL之旅---HBase
最近因为项目原因,研究了Cassandra,Hbase等几个NoSQL数据库,最终决定采用HBase.在这里,我就向大家分享一下自己对HBase的理解. 在说HBase之前,我想再唠叨几句.做互联网应 ...
- VS2015中DataGridView的DataGridViewComBoboxCell列值无效及数据绑定错误的解决方法
在VS2015中练习DataGridView的使用, 发现其中的DataGridViewComBoboxCell列存在着绑定数据库列后出现值无效的提示 根据网上的解决办法,添加了DataError后可 ...
- linux之unlink函数解析
[lingyun@localhost unlink]$ cat unlink.c /********************************************************* ...