自定义控件

来源:互联网 发布:蜂群算法和蚁群算法 编辑:程序博客网 时间:2024/05/26 07:27

这几天在看关于自定义控件的资料,虽然网上demo已经很多了,但自己动手写一遍记忆还是会比较深刻滴

这里只是简单的绘制了一个圆

package com.example.circleviewactivity;import com.example.circleviewactivity.R.style;import com.example.circleviewactivity.R.styleable;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class CircleView extends View {    private int mColor = Color.RED;    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    public CircleView(Context context) {        super(context);        init();        // TODO Auto-generated constructor stub    }    public CircleView(Context context, AttributeSet attrs) {        super(context, attrs);        init();        // TODO Auto-generated constructor stub    }    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView);        mColor = a.getColor(styleable.CircleView_circle_color, Color.RED);        a.recycle();        init();        // TODO Auto-generated constructor stub    }    private void init() {        // TODO Auto-generated method stub        mPaint.setColor(mColor);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        // TODO Auto-generated method stub        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);        int heightSpecSize =MeasureSpec.getSize(heightMeasureSpec);        if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){            setMeasuredDimension(200, 200);        }else if(widthSpecMode == MeasureSpec.AT_MOST){            setMeasuredDimension(200, heightSpecSize);        }else if(heightSpecMode == MeasureSpec.AT_MOST){            setMeasuredDimension(widthSpecSize, 200);        }    }    @Override    protected void onDraw(Canvas canvas) {        // TODO Auto-generated method stub        super.onDraw(canvas);        //必须在这里设置padding的控制,不然自定义view的padding是无效的        final int paddingLeft = getPaddingLeft();        final int paddingRight = getPaddingRight();        final int paddingTop = getPaddingTop();        final int paddingBottom = getPaddingBottom();        int width = getWidth() - paddingLeft - paddingRight;        int height = getHeight() - paddingTop - paddingBottom;        int radius = Math.min(width, height)/2;        //在画布中划出这个圆,重点是onDraw这个函数        canvas.drawCircle(paddingLeft + width/2, paddingTop + height/2, radius, mPaint);    }}
<com.example.circleviewactivity.CircleView       android:id="@+id/circleView1"       android:layout_width="wrap_content"       android:layout_height="100dp"       android:background="#000000"       android:layout_margin="20dp"       android:padding="20dp"       />

其实自定义控件没有想象中复杂,如果想自定义布局的,可以参考另一篇文章自定义控件–titlebar

0 0
原创粉丝点击