[转]永久告别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原生提供的下拉框 ...
随机推荐
- Hadoop 2.6.0 POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- JAVA——利用wait和notify实现生产者和消费者
经典的消费者和生产者的的实现: 注意事项: 1:在循环里面用wait(),因为当线程获得了锁,但是有可能还没有满足其他条件: 2:公用的缓冲池要用锁机制: package demo; import j ...
- MSSql得到表的结构和字段
得到数据库中所有的表 select name from sysobjects where xtype='u' and name='{0}' 1.获取表的基本字段属性 --获取SqlServer中表结构 ...
- JDBC学习笔记(3)——复习和练习
复习和练习 复习部分 一.获取数据库连接 1)方式一 // 获取数据库连接 @Test public void testGetConnection() throws Exception { // 1. ...
- Gym 100507A About Grisha N. (水题)
About Grisha N. 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/A Description Grisha N. t ...
- CodeForces 567A Gerald is into Art
http://codeforces.com/problemset/problem/567/A A. Lineland Mail time limit per test 3 seconds memory ...
- [iOS 多线程 & 网络 - 2.5] - 小文件上传
A.文件上传 思路: 发送文件数据给服务器 使用post请求 必须手动设置请求头: 内容大小Content-Length & 内容类型 Content-Type 请求体:文件数据 文件上传的格 ...
- jquery.loadmask.js
Quick Start 下载之后的目录结构如下图所示: 使用此插件非常简单,如下步骤所示: 1. 引用jquery,1.2.3以上版本 <script type="text/java ...
- HDU 5776 sum (思维题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5776 题目让你求是否有区间的和是m的倍数. 预处理前缀和,一旦有两个数模m的值相同,说明中间一部分连续 ...
- Javascript高级篇-面向对象的特性
一.创建对象 1.1初始化器 var any={ name:"some", age:10, action:function(){ alert(this.name+":&q ...