android - 如何从 GradientDrawable 获取颜色

标签 android

首先,我将绿色设置为 View mIcon 的背景,

View mIcon = findViewById(R.id.xxx);

GradientDrawable gdraw = (GradientDrawable) mContext.getResources().getDrawable(R.drawable.roundbtn_white_normal);
gdraw.setColor(Color.GREEN);
mIcon.setBackgroundDrawable(gdraw);

然后,我不知道如何从此 View 的背景中获取颜色...没有 getColor() 函数...

最佳答案

到目前为止,以下类(class)对我来说效果很好。

import android.content.res.Resources;
...

// it's the same with the GradientDrawable, just make some proper modification to make it compilable
public class ColorGradientDrawable extends Drawable {
    ...
    private int mColor; // this is the color which you try to get
    ...
    // original setColor function with little modification
    public void setColor(int argb) {
        mColor = argb;
        mGradientState.setSolidColor(argb);
        mFillPaint.setColor(argb);
        invalidateSelf();
    }

    // that's how I get the color from this drawable class
    public int getColor() {
        return mColor;
    }
    ...

    // it's the same with GradientState, just make some proper modification to make it compilable
    final public static class GradientState extends ConstantState {
        ...
    }
}

关于android - 如何从 GradientDrawable 获取颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26501277/

相关文章:

Android 地理编码滞后问题

android - 抽屉导航中 fragment 之间的通信

Android 函数接受字符串和字符串资源

android - 如何在 Android 11 中实现 New Storage API?

Android studio 找不到aidl导入

Android - 自定义 View 可以通过不指定 ID 来绑定(bind)到自己的监听器 - 不起作用

android - 如何在 Kotlin Android 中正确使用 URL

android - 图像未出现在 HTC One M8 上

java.lang.classcastException Linearlayout.layoutParams 无法转换为framelayout.layoutparams

android - 如何将原始类型(String)的集合存储到数据库中