Android在绘图时获得背景色
我试图在绘制CardView元素的画布时更改背景色。所以我试图得到onDraw事件的背景色,但我没有得到 如何获得CardView onDaw活动的背景色Android在绘图时获得背景色,android,canvas,background,draw,Android,Canvas,Background,Draw,我试图在绘制CardView元素的画布时更改背景色。所以我试图得到onDraw事件的背景色,但我没有得到 如何获得CardView onDaw活动的背景色 public class MyCardView extends CardView { public MyCardView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) {
public class MyCardView extends CardView {
public MyCardView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
int bgColor = ???some method???;
if(bgColor == 0) {
setBackgroundColor(Color.RED);
}
super.onDraw(canvas);
}
}
提示:避免在
onDraw()中分配
public class MyCardView extends CardView {
private Drawable background;
private int color = Color.RED;
public MyCardView(Context context) {
super(context);
background = ((View) this.getParent()).getBackground();
if (background instanceof ColorDrawable) {
color = ((ColorDrawable) background).getColor();
}
}
@Override
protected void onDraw(Canvas canvas) {
setBackgroundColor(color);
super.onDraw(canvas);
}
}