自定義View-繼承系統View

 
1 CircleView類,實現在View上畫一個圓形背景,圓形是空心還是實心,
圓的邊的粗細以及圓的背景都可以在佈局文件中指出 

public class CircleView extends View {

    private Context mContext;
    private Paint mPaint;
    private int mColor;//background of the circle
    private int border;//border width of the circle
    private boolean isStroke;//whether the circle is stroke

    public CircleView(Context context) {
        super(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        //set attr that assigned in the XML
        TypedArray attributes = mContext.obtainStyledAttributes(attrs, R.styleable.CircleView);
        mColor = attributes.getColor(R.styleable.CircleView_circle_Color, Color.GREEN);
        border = attributes.getInt(R.styleable.CircleView_circle_border, 1);
        isStroke = attributes.getBoolean(R.styleable.CircleView_circle_border_stroke, false);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setStyle(isStroke ? Paint.Style.STROKE : Paint.Style.FILL);
        mPaint.setStrokeWidth(border);
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        super.layout(l, t, r, b);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        //create a real dip dimension
        int widthDip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, mContext.getResources().getDisplayMetrics());
        int heightDip = widthDip;
        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthDip, heightDip);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthDip, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, heightDip);
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //handle the padding, if not handle, the padding in the XML won't effect
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        //calculator the real size
        int width = getWidth() - (paddingLeft + paddingRight);
        int height = getHeight() - (paddingTop + paddingBottom);
        int radius = Math.min(width, height) / 2;

        //calculator the absolute position
        int xPoint = paddingLeft + width / 2;
        int yPoint = paddingTop + height / 2;
        canvas.drawCircle(xPoint, yPoint, radius, mPaint);
    }
}


2 定義styleable在XML中使用

<declare-styleable name="CircleView">
    <attr name="circle_Color" format="color"/>
    <attr name="circle_border_stroke" format="boolean"></attr>
    <attr name="circle_border" format="integer"/>
</declare-styleable>

3 佈局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app = "http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity"
                android:gravity="center"
                android:background="@android:color/holo_blue_light"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        android:orientation="vertical"
        android:gravity="center"
        >
        <com.example.venn.costumeview.ui.customer.CircleView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            app:circle_Color="@android:color/holo_orange_light"
            app:circle_border_stroke="false"
            app:circle_border="10"
            />
    </LinearLayout>
</RelativeLayout>


 
發佈了44 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章