- 浏览: 1481454 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (419)
- XMPP (19)
- Android (180)
- Java (59)
- Network (4)
- HTML5 (13)
- Eclipse (9)
- SCM (23)
- C/C++ (4)
- UML (4)
- Libjingle (15)
- Tools&Softwares (29)
- Linphone (5)
- Linux&UNIX (6)
- Windows (18)
- Google (10)
- MISC (3)
- SIP (6)
- SQLite (5)
- Security (4)
- Opensource (29)
- Online (2)
- 文章 (3)
- MemoryLeak (10)
- Decompile (5)
- Ruby (1)
- Image (1)
- Bat (4)
- TTS&ASR (28)
- Multimedia (1)
- iOS (20)
- Asciiflow - ASCII Flow Diagram Tool.htm (1)
- Networking (1)
- DLNA&UPnP (2)
- Chrome (2)
- CI (1)
- SmartHome (0)
- CloudComputing (1)
- NodeJS (3)
- MachineLearning (2)
最新评论
-
bzhao:
点赞123!
Windows的adb shell中使用vi不乱码方法及AdbPutty -
wahahachuang8:
我觉得这种东西自己开发太麻烦了,就别自己捣鼓了,找个第三方,方 ...
HTML5 WebSocket 技术介绍 -
obehavior:
view.setOnTouchListenerview是什么
[转]android 一直在最前面的浮动窗口效果 -
wutenghua:
[转]android 一直在最前面的浮动窗口效果 -
zee3.lin:
Sorry~~
When I build "call ...
Step by Step about How to Build libjingle 0.4
Android Button background image pressed/highlighted and disabled states without
- 博客分类:
- Android
In Android, if you provide custom background images for buttons, you will lose the pressed and disabled image effects. The common way to fix that is to provide additional images for those states. I’m lazy and I find this inconvenient especially during the prototyping phase of app development.
I’ve always liked the way iOS automatically handles pressed and disabled states for custom button backgrounds so I made a Button
subclass that automatically darkens the background image when the
button is pressed, and makes the background transparent when it is
disabled. This is done by using a custom LayerDrawable
for the button that contains the original background Drawable
. The LayerDrawable
has to be stateful
and should change the background properties depending on the current state in onStateChange()
. The full source explains it better:
package net.shikii.widgets; import android.content.Context; 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.widget.Button; /** * Applies a pressed state color filter or disabled state alpha for the button's background * drawable. * * @author shiki */ public class SAutoBgButton extends Button { public SAutoBgButton(Context context) { super(context); } public SAutoBgButton(Context context, AttributeSet attrs) { super(context, attrs); } public SAutoBgButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setBackgroundDrawable(Drawable d) { // Replace the original background drawable (e.g. image) with a LayerDrawable that // contains the original drawable. SAutoBgButtonBackgroundDrawable layer = new SAutoBgButtonBackgroundDrawable(d); super.setBackgroundDrawable(layer); } /** * The stateful LayerDrawable used by this button. */ protected class SAutoBgButtonBackgroundDrawable extends LayerDrawable { // The color filter to apply when the button is pressed protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1); // Alpha value when the button is disabled protected int _disabledAlpha = 100; public SAutoBgButtonBackgroundDrawable(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; } } }
To use this, just replace your original button declarations like this:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_blue_bg" android:text="Button with background image" />
To this:
<net.shikii.widgets.SAutoBgButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_blue_bg" android:text="Button with background image" />
Here’s a sample output using this custom button:
The code is also available on GitHub .
Reference:
发表评论
-
[Android] 为Android安装BusyBox —— 完整的bash shell
2013-12-27 10:19 1409http://www.cnblogs.com/xiaowen ... -
Windows的adb shell中使用vi不乱码方法及AdbPutty
2013-12-27 10:17 7391http://www.veryhuo.com/down/ht ... -
AppMobi推出新XDK,可创建测试PhoneGap项目
2012-09-03 13:39 2527AppMobi今天发布了一个新的工具PhoneGap Mobi ... -
Sencha
2012-09-03 12:59 1136http://www.sencha.com/ Se ... -
jQuery Mobile学习
2012-09-01 12:33 1625使用Jquery Mobile设计Android通讯录 ... -
BackBone
2012-09-01 12:34 1218Backbone.js 是一种重量级javascript M ... -
jQTouch
2012-08-30 15:57 934A Zepto/jQuery plugin for mobil ... -
SwiFTP
2012-08-30 15:43 1241SwiFTP is a FTP server that run ... -
kWS
2012-08-30 15:41 1144kWS is a lightweight and fast W ... -
jQuery Mobile
2012-08-30 15:07 974http://jquerymobile.com/ -
PhoneGap
2012-08-30 15:07 992http://phonegap.com/ -
[AndriodTips]Image, saved to sdcard, doesn't appear in Android's Gallery app
2012-08-04 16:15 1109http://stackoverflow.com/questi ... -
Voice detection for Android
2012-07-23 11:39 2292Here it is, my fist JAVA applic ... -
[AndroidTip]local reference table overflow (max=512)的错误解决
2012-07-22 22:56 5949JNI层coding经常会遇到ReferenceTable o ... -
[AndroidTip]EditText如何初始状态不获得焦点?
2012-07-22 15:35 1181最简单的办法是在EditText前面放置一个看不到的Linea ... -
[AndroidTip]android textview滚动条
2012-07-21 14:29 1257本来是想做一个显示文字信息的,当文字很多时View的高度不能超 ... -
Google公布Android 4.1完整功能
2012-07-16 09:48 3137http://www.android.com/about/je ... -
Android开发:使用AudioTrack播放PCM音频数据【附源码】
2012-07-13 15:20 20770http://www.linuxidc.com/Linux/2 ... -
Android上的行车记录仪
2012-07-11 22:31 1963MyCar Recorder DailyRoads -
Google hired one of Nuance soft engineers to help work around all Nuance patents
2012-07-10 14:33 1046很有趣的消息: http://forums.macrumor ...
相关推荐
1.背景选择器(位于res/drawable/,使用方法:android:background=”@drawable/XXX”) 代码如下: <?xml version=”1.0″ encoding=”utf-8″?> <selectorxmlns android=”...
按键按下判断,3种方法都可行,适用于薄膜按钮\手柄按键\脚踏按键
本文实例讲述了Android编程实现改变控件背景及形态的方法。分享给大家供大家参考,具体如下: 1. 改变背景 ...item android:state_pressed=true android:drawable=@drawable/search_bar_edit_pressed /
<item android:state_pressed="true" android:drawable="@drawable/bg3" /> <!-- 普通无焦点状态 --> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/bg" />...
本文实例为大家分享了Android实现弹钢琴效果展示的具体代码,供大家参考,具体内容如下 ... <item android:drawable=@drawable/button_pressed android:state_pressed=true></item> <item android
效果图如下所示: 一、shape 样式:(在drawable新建–》new–》Drawable resource file 在父级标签...<item android:state_pressed=true> <shape android:shape=rectangle> <solid android:color=#73c4f3> <
Editable Style of Tag, Background/Pressed Color, Radius effect, Custom Background, Delete mode. Listener of tag selecting and deleting. Can be created from XML file or Java code. Sample A
It registers as app launcher (that means it is called when the phone boots and the home button is pressed). It let's the user decide if he wants to call someone or wants to watch images It reads the ...
Keep the button pressed to open the camera release the button to capture the photo :) This library uses CameraKit-Android Demo Install Add the dependecy in build.gradle(module: app) repositories {...
本文实例讲述了Android开发实现的圆角按钮、文字阴影按钮效果。分享给大家供大家参考,具体如下: ... <item android:state_pressed=true> <shape xmlns:android=http://schemas.android.com/apk/res/
pressed : Boolean , // Whether the button has been pressed/released note : Number , // Integer between [0-127] which represents a midi note frequency : Number , // The frequency to play the ...
本文实例讲述了Android编程使用自定义shape实现shadow阴影效果的方法。... <item android:state_pressed=true> <layer-list> <!-- 相当于padding --> <item android:left=4dp android
Android TagView Android TagView-HashTagView Xamarin version, written by @fernandolopes ...Simple android view to ...Editable Style of Tag, Background/Pressed Color, Radius effect, Custom Background, Del
WARNING! FURTHER DEVELOPMENT AND SUPPORT IS DISCONTINUED. FloatingActionButton Yet another implementation of Floating Action Button for Android with ...Option to set custom normal/pressed/ripple colors
android:background="@drawable/home_pressed" android:clickable="false" /> android:id="@+id/tv_page_home" android:textColor="#ffd100" android:layout_width="wrap_content" android:layout_height=...
// Assume `sendBtnClicked` emits whenever a send button is pressed and // `messageText` is a stream of the current content of an input field. // Then `sendMessage` emits the content of the text field...
在平时开发中如Button我们给它加上selector分别呈现pressed以及normal效果能给我们的用户体验上大大增色不少,可是我们当我们是用ImageView来”当作”一个一个”Button”的时候发现直接设置selector却不起作用,当然...
react-native-home-pressed 入门 $ npm install react-native-home-pressed --save 自动安装(无需进一步设置) $ react-native link react-native-home-pressed 手动安装 Android 打开 android/app /src/main/java/...
MSP430G系列带触摸参考代码, Basic 8-button input using the built-in pin ...// After each scan, one UART byte identifying the key# being pressed is // transmitted via TimerA UART at port pin P1.1.
Android中的Selector的用法 ... <item android:state_pressed=true android:drawable=@drawable/bg_selected> </item> <item android:drawable=@drawable/bg_unselect> </item> </selector> 在