自定義View

自定義圓形

	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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章