FadingColorButton

来源:互联网 发布:淘宝人工投诉客服电话 编辑:程序博客网 时间:2024/06/13 23:41


之前实现了 ColorFulButton ,但又见到过类似这种渐变的按钮,又正好学到了 TransitionDrawable, 所以也实现了下。

实现思路想到两种,添加 ToucnListener 侦听 DOWN 和 UP 或者利用内部 onStateChange ,最后用的是后者。

public class FadingDrawable extends TransitionDrawable {    public FadingDrawable(Drawable[] layers) {        super(layers);    }    @Override    public boolean isStateful() {        // 需要 return true,否则 setState 不会被调用,具体看 View 源码。        return true;    }    @Override    protected boolean onStateChange(int[] state) {        // 检测是否包含 statePressed        if (isStatePressed(state)) {            // 开始渐变            startTransition(300);        } else {            // 重置(也可以改成 reverseTransition,看个人喜好了)            resetTransition();        }        return true;    }    private boolean isStatePressed(int[] states) {        for (int s : states) {            if (s == android.R.attr.state_pressed) {                return true;            }        }        return false;    }}
public class FadingColorButton extends TextView {    public FadingColorButton(Context context) {        super(context);    }    //    public FadingColorButton(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerTextView);        float dp = typedArray.getDimension(R.styleable.RoundCornerTextView_rc_corner_radius, 0.0f);        // 最终绘图的单位是像素        int cornerRadius = dp2px(context, dp);        typedArray.recycle();        // 替换 background        ColorDrawable originalBackground = (ColorDrawable) getBackground();        if (originalBackground != null) {            GradientDrawable unpressed = new GradientDrawable();            unpressed.setCornerRadius(cornerRadius);            int bgColor = originalBackground.getColor();            unpressed.setColor(bgColor);            GradientDrawable pressed = new GradientDrawable();            pressed.setCornerRadius(cornerRadius);            int bgColorPressed = colorBurn(bgColor);            pressed.setColor(bgColorPressed);            Drawable[] d = new Drawable[]{unpressed, pressed};            FadingDrawable background = new FadingDrawable(d);            setBackground(background);            setTextColor(Color.WHITE);            setClickable(true);        }    }    //    protected int dp2px(Context context, float dp) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (dp * scale + 0.5f);    }    /**     * 颜色加深处理     *     * @param RGBValues RGB的值,由alpha(透明度)、red(红)、green(绿)、blue(蓝)构成,     *                  Android中我们一般使用它的16进制,     *                  例如:"#FFAABBCC",最左边到最右每两个字母就是代表alpha(透明度)、     *                  red(红)、green(绿)、blue(蓝)。每种颜色值占一个字节(8位),值域0~255     *                  所以下面使用移位的方法可以得到每种颜色的值,然后每种颜色值减小一下,在合成RGB颜色,颜色就会看起来深一些了     * @return     */    private int colorBurn(int RGBValues) {        //int alpha = RGBValues >> 24;        int red = RGBValues >> 16 & 0xFF;        int green = RGBValues >> 8 & 0xFF;        int blue = RGBValues & 0xFF;        red = (int) Math.floor(red * (1 - 0.2));        green = (int) Math.floor(green * (1 - 0.2));        blue = (int) Math.floor(blue * (1 - 0.2));        return Color.rgb(red, green, blue);    }}






0 0
原创粉丝点击