获取ImageView的背景颜色

CjS*_*CjS 7 android colors imageview

我有一个普通的ImageView对象colorSquare.

通过调用设置对象的颜色很简单.

colorSquare.setBackgroundColor(color);
Run Code Online (Sandbox Code Playgroud)

但是我该怎么做反过来,即检索ImageView背景的颜色?

Viv*_*wal 15

你能做的是

从ImageView获取ColorDrawable.

ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground();
Run Code Online (Sandbox Code Playgroud)

现在

drawable.getColor()

会给你颜色.

这只有在你设置了Color的情况下才有效,否则你会得到ClassCastException