1 つのアプローチは次のとおりです。
public class MyDrawable extends ShapeDrawable{
private Paint mFillPaint;
private Paint mStrokePaint;
private int mColor;
@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
shape.drawPaint(mFillPaint, canvas);
shape.drawPaint(mStrokePaint, canvas);
super.onDraw(shape, canvas, paint);
}
public MyDrawable() {
super();
// TODO Auto-generated constructor stub
}
public void setColors(Paint.Style style, int c){
mColor = c;
if(style.equals(Paint.Style.FILL)){
mFillPaint.setColor(mColor);
}else if(style.equals(Paint.Style.STROKE)){
mStrokePaint.setColor(mColor);
}else{
mFillPaint.setColor(mColor);
mStrokePaint.setColor(mColor);
}
super.invalidateSelf();
}
public MyDrawable(Shape s, int strokeWidth) {
super(s);
mFillPaint = this.getPaint();
mStrokePaint = new Paint(mFillPaint);
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setStrokeWidth(strokeWidth);
mColor = Color.BLACK;
}
}
使用法:
MyDrawable shapeDrawable = new MyDrawable(new RectShape(), 12);
//whenever you want to change the color
shapeDrawable.setColors(Style.FILL, Color.BLUE);
または、Drawable を にキャストする 2 番目のアプローチを試して、ShapeDrawable
別のものを作成し、次のPaint
ように設定します。castedShapeDrawable.getPaint().set(myNewPaint);