当前位置:网站首页>Gradientdrawable get a single color

Gradientdrawable get a single color

2022-07-05 09:33:00 Xu Jiajia 233

summary

GradientDrawable Although it's powerful , In normal use , There are many scenes that only use shape related functions , For example, it is used to draw a circle or a rounded rectangle , The general code is as follows :

        View vTest = findViewById(R.id.v_main_test);
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(0xFFFF0000);
        gradientDrawable.setCornerRadius(20);
        vTest.setBackground(gradientDrawable);

Get a single color

If only GradientDrawable A single color is set in ,GradientDrawable There is no specially obtained interface in , The relevant logic needs to be implemented according to the corresponding interface , as follows :

        if (vTest.getBackground() instanceof GradientDrawable) {
    
            GradientDrawable gradientDrawable = (GradientDrawable) vTest.getBackground();
            ColorStateList colorStateList = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    
                colorStateList = gradientDrawable.getColor();
                int singleColor = colorStateList.getColorForState(gradientDrawable.getState(), Color.WHITE);
                Log.i("test_log", "singleColor:" + singleColor);
            }
        }
原网站

版权声明
本文为[Xu Jiajia 233]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140538084807.html