maoruibin / maoruibin/maoruibin.github.com

动态设置按钮背景

Open
#32 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

trick
Dominant language
HTML
Stars
22
Forks
4
PR merge metrics
No merged PRs in 30d

Description

通过代码使用一份资源动态设置按钮按下效果

原理

使用 StateListDrawable 动态设置背景,并利用 normal 状态的 background 动态调整其 alpha 值,并将其设置为 pressed 的 drawable

设置背景为一个 Drawable

 public static void bkAutoAlpha(View view, @DrawableRes int resBackground, @FloatRange(from = 0, to = 1) float alpha) {
        final StateListDrawable stateListDrawable = new StateListDrawable();
        Drawable normal = view.getContext().getResources().getDrawable(resBackground);
        normal.mutate();
        Drawable pressed = view.getContext().getResources().getDrawable(resBackground);
        pressed.setAlpha(getAlpha(alpha));

        stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
        stateListDrawable.addState(new int[]{}, normal);

        view.setBackgroundDrawable(stateListDrawable);
        view.setClickable(true);
    }

    public static int getAlpha(@FloatRange(from = 0, to = 1) float alpha) {
        return (int) (255 * alpha);
    }

设置背景为颜色,并自动调整 Alpha

public static void bkAutoAlphaColor(View view,int resBackgroundColor,float alpha) {
    ColorDrawable colorDrawableNormal = new ColorDrawable();
    ColorDrawable colorDrawablePressed = new ColorDrawable();

    int normalColor = getColor(view.getContext(),resBackgroundColor);
    int pressedColor = alphaColor(normalColor, convertAlphaToInt(alpha));

    colorDrawableNormal.setColor(normalColor);
    colorDrawablePressed.setColor(pressedColor);

    final StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, colorDrawablePressed);
    stateListDrawable.addState(new int[]{}, colorDrawableNormal);

    view.setBackgroundDrawable(stateListDrawable);
    view.setClickable(true);
}

private static int getColor(Context context,int resBackgroundColor){
    return context.getResources().getColor(resBackgroundColor);
}

private static int alphaColor(int color,int alpha){
    return Color.argb(alpha,Color.red(color),Color.green(color),Color.blue(color));
}

TODO

  • 简单支持 enable 状态

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Java snippets in this issue and Android's StateListDrawable state handling. The TODO requests support for the enabled state, but its expected appearance and exact scope are not specified; done should mean that enabled-state behavior is supported alongside the existing pressed and normal states.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.