한 가지 방법은 이것이다 :
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);
또는 시도, 두 번째 방법, ShapeDrawable
에 드로어 블 캐스팅, Paint
를 별도 작성 등으로 설정할 : castedShapeDrawable.getPaint().set(myNewPaint);
을