如何在Android中获得可绘制图像的颜色
在我的代码的一部分中,我有以下代码:如何在Android中获得可绘制图像的颜色,android,drawable,Android,Drawable,在我的代码的一部分中,我有以下代码: linearLayout.setBackgroundColor(Color.RED); 在另一部分中,我想获得设置的背景色,类似于: linearLayout.getBackgroundColor(); 但是没有这样的方法,只有这个方法: Drawable drawable = linearLayout.getBackground(); 我怎样才能得到一个可拉丝的颜色?(我不想扩展线性布局并手动存储背景色等。)尝试以下操作: ((ColorDrawab
linearLayout.setBackgroundColor(Color.RED);
在另一部分中,我想获得设置的背景色,类似于:
linearLayout.getBackgroundColor();
但是没有这样的方法,只有这个方法:
Drawable drawable = linearLayout.getBackground();
我怎样才能得到一个可拉丝的颜色?(我不想扩展线性布局并手动存储背景色等。)尝试以下操作:
((ColorDrawable)linearLayout.getBackground()).getColor();
尝试:
顺便说一句,我通过查看以下源代码了解到了这一点:
您可以在类似以下内容中使用ColorDrawable:
Drawable background = linearLayout.getBackground();
if (background instanceof ColorDrawable) {
Color color = ((ColorDrawable) background).getColor();
}
试试这个:
int color = Color.TRANSPARENT;
Drawable bg = view.getBackground();
if (bg instanceof ColorDrawable){
color = ((ColorDrawable) bg).getColor();
}
Drawable background = linearLayout.getBackground();
if (background instanceof ColorDrawable) {
Color color = ((ColorDrawable) background).getColor();
}
int color = Color.TRANSPARENT;
Drawable bg = view.getBackground();
if (bg instanceof ColorDrawable){
color = ((ColorDrawable) bg).getColor();
}