我正在尝试用特定颜色绘制圆角矩形,但我什么也没得到。我已经做了很多谷歌搜索,发现了一些这样的问题并阅读了它们。但是,他们都没有解决我的问题。为什么我的onDraw
方法永远不会被调用?
任何帮助表示赞赏。
public class RoundedTagItem extends RelativeLayout {
Context ctx;
String colorString;
int color;
public RoundedTagItem(Context context) {
super(context);
this.ctx = context;
color = Color.WHITE;
this.setWillNotDraw(false);
this.setPadding(10, 0, 10, 0);
}
public RoundedTagItem(Context context, String color) {
super(context);
this.ctx = context;
this.colorString = color;
this.setWillNotDraw(false);
this.setPadding(10, 0, 10, 0);
}
@Override
protected void onDraw(Canvas canvas) {
if(colorString != null)
color = Color.parseColor("#" + colorString);
int rectValue = 35;
Log.d("Rounded", "rectValue: " + rectValue);
RectF rect1 = new RectF(0, 0, rectValue, rectValue);
Paint paint = new Paint();
paint.setStrokeWidth(1);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(color);
canvas.save();
canvas.drawRoundRect(rect1, 10, 10, paint);
canvas.restore();
super.onDraw(canvas);
}
public void ChangeBackgroundColor(String colorString) {
color = Color.parseColor(colorString);
invalidate();
}
}
然后我在我的主类的这个自定义视图上放置了图标:
// add category items to view
LinearLayout categories = (LinearLayout) findViewById(R.id.detailTagImagesLayout);
for(int i = 0; i < item.getCategoryItems().size(); i++) {
RoundedTagItem v = null;
if(i == 0)
v = new RoundedTagItem(DetailActivity.this,
item.getCategoryItems().get(i).getColorCode());
else
v = new RoundedTagItem(DetailActivity.this);
ImageView img = new ImageView(DetailActivity.this);
img.setImageDrawable(Drawable.createFromPath(
item.getCategoryItems().get(i).getLightIconUrl()));
v.addView(img);
v.setTag(i);
v.setOnClickListener(new TagClickListener());
categories.addView(v);
}