自定義圓形
public class Custom extends View {
private int mColor;
private int mRadius;
public Custom(Context context) {
super(context);
}
public Custom(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Custom);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i <indexCount ; i++) {
int index = typedArray.getIndex(i);
switch (index){
case R.styleable.Custom_mColor:
mColor = typedArray.getColor(index, Color.RED);
break;
case R.styleable.Custom_mRadius:
mRadius = typedArray.getInt(index, 50);
break;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(mColor);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(100f,100f,mRadius,paint);
}
}
attrs中的配置
<resources>
<declare-styleable name="Custom">
<attr name="mColor" format="color"/>
<attr name="mRadius" format="integer"/>
</declare-styleable>
</resources>