public class MyView extends View { private String TAG="MyView"; private Paint mPaint; //画笔 private RectF oval; public MyView(Context context) { super(context); init(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化 */ private void init() { mPaint=new Paint(); // 设置Paint为无锯齿 mPaint.setAntiAlias(true); oval=new RectF(); } //测量 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode=MeasureSpec.getMode(widthMeasureSpec); int widthSize=MeasureSpec.getSize(widthMeasureSpec); int heightMode=MeasureSpec.getMode(heightMeasureSpec); int heightSize=MeasureSpec.getSize(heightMeasureSpec); switch (widthMode){ case MeasureSpec.AT_MOST: //子容器可以是声明大小内的任意大小. break; case MeasureSpec.EXACTLY: //父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间. break; case MeasureSpec.UNSPECIFIED: //父容器对于子容器没有任何限制,子容器想要多大就多大. break; } Log.e(TAG, "onMeasure--widthSize-->" + widthSize); Log.e(TAG, "onMeasure--heightMode-->" + heightMode); Log.e(TAG, "onMeasure--heightSize-->" + heightSize); } //画的位置 @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); Log.e(TAG, "onLayout"); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(Color.BLUE); int with=getWidth(); int height=getHeight(); mPaint.setStyle(Paint.Style.FILL); // 画一个正放形 前面两个是左上角坐标 后面两个是右下角坐标 canvas.drawRect(0,20,30,50,mPaint); }
android自定义控件--学习
最新推荐文章于 2018-10-31 16:06:33 发布