gpt4 book ai didi

android - DrawableCompat.unwrap 在 Lollipop 之前不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:13:42 27 4
gpt4 key购买 nike

我正在使用 DrawableCompat.wrap 在 Lollipop 之前的可绘制对象上设置色调,并且工作正常。DrawableCompat.unwrap 在 Lollipop 之前不起作用。我无法在着色前获得原始可绘制对象。

例如:

 if (v.isSelected()){
Drawable normalDrawable = getResources().getDrawable(R.drawable.sample);
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.sample_color));
imageButton.setImageDrawable(wrapDrawable);
}else{
Drawable normalDrawable = imageButton.getDrawable();
Drawable unwrapDrawable = DrawableCompat.unwrap(normalDrawable);
imageButton.setImageDrawable(unwrapDrawable);
}

在 Lollipop 之前的设备中,DrawableCompact.unwrap 返回带有色调的可绘制对象,而不是原始的

最佳答案

如果你想清除色调,调用DrawableCompat.setTintList(drawable, null)

Unwrap 不是破坏性函数,它只是为了让您访问原始 Drawable。

以下是示例代码:

Drawable drawable = (Drawable) ContextCompat.getDrawable(getContext(), R.drawable.google_image);
if (condition) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.grey700));
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SCREEN);
mImageView.setImageDrawable(drawable);
} else {
drawable = DrawableCompat.unwrap(drawable);
DrawableCompat.setTintList(drawable, null);
mLoginStatusGoogleImageView.setImageDrawable(drawable);
}

您的情况中,代码应该是:

if (v.isSelected()) {
Drawable normalDrawable = getResources().getDrawable(R.drawable.sample);
Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), R.color.sample_color));
DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.sample_color));
imageButton.setImageDrawable(wrapDrawable);
} else {
Drawable normalDrawable = imageButton.getDrawable();
Drawable unwrapDrawable = DrawableCompat.unwrap(normalDrawable);
DrawableCompat.setTintList(unwrapDrawable, null);
imageButton.setImageDrawable(unwrapDrawable);
}

关于android - DrawableCompat.unwrap 在 Lollipop 之前不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30945490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com