android png上色,android – 如何使用appcompat 23.3.0为Button的drawable着色?

我正面临着新的AppCompat 23.3.x和drawables的几个问题.首先,我不得不回去删除:

vectorDrawables.useSupportLibrary = true

因为AppCompat现在还原了,我的应用程序再次生成PNG.好吧,但是,我以一种完全停止工作的方式(对于M之前的设备)给一个按钮(drawableTop)着色.

这是我使用的方法:

private void toggleState(boolean checked) {

Drawable[] drawables = getCompoundDrawables();

Drawable wrapDrawable = DrawableCompat.wrap(drawables[1]);

if (checked) {

DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),

R.color.colorPrimary));

setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));

} else {

DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),

R.color.icon_light_enabled));

setTextColor(ContextCompat.getColor(getContext(), R.color.text_primary_light));

}

}

问题是,我有一个可检查的自定义Button类,如果选中,drawableTop和text有不同的颜色,如果没有选中它.

这样做是有效的(使用appcompat 23.2.0),但现在,它不是(在Android M下面).信不信由你,但这样做,当它点击setTint时,图标就不再可见了.

为了使它工作,我必须做如下:

DrawableCompat.setTint(wrapDrawable.mutate(), ContextCompat.getColor(getContext(),R.color.colorPrimary));

setCompoundDrawablesWithIntrinsicBounds(null, wrapDrawable, null, null);

所以每当我给它们着色时,我都要再次调用setCompoundDrawablesWithIntrinsicBounds.这样做可以让一切恢复正常.但是,我有点担心每次设置drawable的性能.基本上,我想知道是否有更好的方法或我缺少的东西.

我知道一个Button有setCompoundDrawableTintList,这将是奇妙的,但它的最小API是23级.

解决方法:

没有足够的声誉来发表评论,但我也遇到了这个问题.我今天抓住它而不是在发布之后真的很糟糕和好事.

/**

* Tinting drawables on Kitkat with DrawableCompat

* requires wrapping the drawable with {@link DrawableCompat#wrap(Drawable)} prior to tinting it

* @param view The view containing the drawable to tint

* @param tintColor The color to tint it.

*/

public static void tintViewBackground(View view, int tintColor) {

Drawable wrapDrawable = DrawableCompat.wrap(view.getBackground());

DrawableCompat.setTint(wrapDrawable, tintColor);

view.setBackground(wrapDrawable);

}

/**

* Tinting drawables on Kitkat with DrawableCompat

* requires wrapping the drawable with {@link DrawableCompat#wrap(Drawable)} prior to tinting it

* @param drawable The drawable to tint

* @param tintColor The color to tint it.

*/

public static void tintDrawable(Drawable drawable, int tintColor) {

Drawable wrapDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTint(wrapDrawable, tintColor);

}

甚至更奇怪的是这只发生在Lollipop上,在Kitkat和API 23上可以看到正常的行为.

标签:android,android-drawable,appcompat,appcompat-v7-r23

来源: https://codeday.me/bug/20190519/1135283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值