`

Android Button background image pressed/highlighted and disabled states without

 
阅读更多

http://shikii.net/blog/android-button-background-image-pressedhighlighted-and-disabled-states-without-using-multiple-images/

 

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:

http://stackoverflow.com/questions/6705304/how-to-set-alpha-value-for-drawable-in-a-statelistdrawable

 

分享到:
评论

相关推荐

    android 自定义各种风格button

    android:background="@color/button_color_selector"/&gt; ``` 3. **渐变色**:可以使用`GradientDrawable`来自定义渐变背景。在代码中或XML中都可以实现。 代码示例: ```java GradientDrawable gradient = new ...

    Android带动态效果的Button(按钮)

    &lt;item android:drawable="@drawable/button_pressed" android:state_pressed="true" /&gt; &lt;!-- 按下状态 --&gt; &lt;item android:drawable="@drawable/button_focused" android:state_focused="true" /&gt; &lt;!-- 获得焦点...

    android button 颜色变化 按钮状态变化

    android:background="@drawable/button_color_selector" /&gt; ``` 通过这种方式,Button就会根据其当前状态自动显示相应的颜色。当用户触摸按钮时,它会变为`button_pressed_color`,手指离开后恢复为`button_...

    android button shape Toast

    &lt;item android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/button_normal_shape"/&gt; &lt;!-- 按钮按下状态 --&gt; &lt;item android:state_pressed="true" android:drawable="@...

    Android用drawable实行属性按钮3种状态demo

    android:background="@drawable/button_states" /&gt; ``` 当按钮的状态改变时,系统会自动根据`button_states.xml`中的规则选择相应的`drawable`,从而改变按钮的外观。这个方法不仅限于颜色变化,还可以用于显示...

    Android设置button背景selector和字体selector

    &lt;item android:state_pressed="true" android:drawable="@drawable/button_pressed" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/button_focused" /&gt; &lt;!-- focused --&gt; ...

    按钮点击背景色变化、文字颜色变化

    &lt;item name="android:background"&gt;@drawable/button_background_pressed&lt;/item&gt; &lt;/style&gt; ``` 这里,我们定义了一个名为`CustomButton`的样式,其父样式为`Widget.AppCompat.Button`,这样可以继承系统默认的按钮...

    Android中Button一边圆角一边直角

    &lt;item android:state_pressed="true" android:drawable="@drawable/switch_button_left_checked"/&gt; &lt;item android:state_focused="true" android:drawable="@drawable/switch_button_left_checked"/&gt; &lt;item ...

    Android自定义Button按钮显示样式

    在 Android 中, Button 控件的样式可以通过修改其 Background 属性来实现。首先,需要创建一个 XML 文件,类型选 Drawable,根结点选 selector,文件名可以任意命名,例如 button_style。在这个文件中,需要定义三...

    5000多个ANDROID按钮图片

    android:background="@drawable/button_image" /&gt; ``` 在代码中,可以使用`setBackgroundResource(int resid)`方法: ```java Button myButton = findViewById(R.id.myButton); myButton.setBackgroundResource(R...

    ListView Button ImageView 里应用selector选择器切换图片并保持住

    &lt;item android:drawable="@drawable/button_pressed" android:state_pressed="true" /&gt; &lt;item android:drawable="@drawable/button_focused" android:state_focused="true" /&gt; &lt;item android:drawable="@drawable...

    android使用xml实现一些常用的背景图

    &lt;item android:state_pressed="true" android:drawable="@drawable/button_pressed"/&gt; &lt;item android:state_focused="true" android:drawable="@drawable/button_focused"/&gt; &lt;item android:drawable="@drawable/...

    android获取焦点后按钮变大

    &lt;item name="android:background"&gt;@drawable/button_background&lt;/item&gt; &lt;item name="android:padding"&gt;8dp&lt;/item&gt; &lt;item name="android:focusable"&gt;true&lt;/item&gt; &lt;item name="android:focusableInTouchMode"&gt;true...

    按钮按下效果演示

    &lt;item android:state_pressed="true" android:drawable="@drawable/button_pressed" /&gt; &lt;!-- 按下状态 --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/button_focused" /&gt; &lt;!-- 聚焦状态 --...

    Android控件点击变色

    &lt;item android:state_pressed="true" android:drawable="@color/button_pressed_color" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_focused="true" android:drawable="@color/button_focused_color" /&gt; &lt;!-- ...

    Android绘图机制Selector

    android:background="@drawable/selector_button" /&gt; ``` Selector不仅限于图片,还可以结合颜色或者渐变效果,使得UI设计更加灵活。例如,我们可以使用ColorStateList来根据按钮状态改变其背景颜色: ```xml ...

    androidbutton背景随心搭配借鉴.pdf

    &lt;item android:state_pressed="true" android:drawable="@drawable/button_highlight_ctrl" /&gt; &lt;item android:drawable="@drawable/button_normal_ctrl" /&gt; &lt;/selector&gt; ``` 当按钮被按下时,它将显示`@drawable/...

    安卓系统下button的样式

    例如,可以在`res/layout`目录下的布局文件中定义一个Button,通过`android:text`属性设置按钮文本,`android:background`属性指定背景资源,`android:layout_width`和`android:layout_height`定义按钮尺寸。...

    andorid 美化 扁平按钮美化

    &lt;item android:state_pressed="true" android:drawable="@drawable/button_pressed" /&gt; &lt;!-- 按下状态 --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/button_focused" /&gt; &lt;!-- 获得焦点...

    android > 按钮Button 按下效果

    要应用这些资源到Button控件上,我们需要在布局XML文件中定义Button,并通过`android:background`属性指定背景资源。例如: ```xml &lt;Button android:id="@+id/myButton" android:layout_width="wrap_content" ...

Global site tag (gtag.js) - Google Analytics