Android Button Drawable Tint

前端 未结 7 1633
自闭症患者
自闭症患者 2020-12-04 18:13

Is it possible to tint the drawableLeft in an android button? I have a black drawable I\'d like to tint white. I know how to achieve this with an image view (image on the le

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 18:20

    As @Boris suggested, you can use the support library.

    I have extended the AppCompatButton with some draft code. The TintAppCompatButton should have default behaviour but TintAppCompatButtonCustom was made to demo custom coloring.

    I’ll see if I can raise a PR to get that into the official support library.

    The code of interest is:

    private void init() {
        PorterDuff.Mode tintMode = PorterDuff.Mode.SRC_IN;
        Drawable[] ds = getCompoundDrawables();
        tint(ds[LEFT], Color.RED, tintMode);
        tint(ds[TOP], Color.YELLOW, tintMode);
        tint(ds[RIGHT], Color.GREEN, tintMode);
        tint(ds[BOTTOM], Color.BLUE, tintMode);
    }
    
    private void tint(Drawable d, int color, PorterDuff.Mode tintMode) {
        TintInfo ti = new TintInfo();
        ti.mTintMode = tintMode;
        ti.mTintList = ColorStateList.valueOf(color);
        ti.mHasTintList = true;
        ti.mHasTintMode = true;
    
        TintManager.tintDrawable(d, ti, new int[]{0});
    }
    

提交回复
热议问题