A standardized custom View-Android development art exploration note
An nonstandard custom View
This custom View is very simple, that is, to draw a circle and implement a custom View with a circular effect.
First, let's look at how an nonstandard custom View is made.
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(); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint.setColor(mColor); } @Override protected void onDraw(Canvas canvas) { int width = getWidth(); int height = getHeight(); int radius = Math.min(width, height) / 2; canvas.drawCircle(width / 2, height / 2, radius, mPaint); }}
Corresponding xml
In this way, although a circle can be drawn, this is not a standard custom View, mainly has the following problems:
Android: the padding attribute cannot be used. wrap_content is equivalent to a standard custom View using match_partent.
To solve the preceding problems, you need to override the onMeasure and onDraw methods of the View.
The complete code is as follows:
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(); } public CircleView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mColor = a.getColor(R.styleable.CircleView_circle_color, Color.RED); a.recycle(); init(); } private void init() { mPaint.setColor(mColor); } @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); 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) { super.onDraw(canvas); 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; canvas.drawCircle(paddingLeft + width / 2, paddingTop + height / 2, radius, mPaint); }}
Add Custom Attributes
Add attrs. xml under the values folder
The custom attribute set CircleView defines only one attribute circle_color in the format of color in this attribute set.
Parse custom attributes in the View Constructor
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleView); mColor = a.getColor(R.styleable.CircleView_circle_color, Color.RED); a.recycle(); init(); }
Use custom attributes in the layout File
When using custom attributes, you must declare in schemas: xmlns: app = "timeout ".
Note: The margin attribute in the Custom View can be used, because it is controlled by the parent container to directly inherit the View or ViewGroup, you need to process the wrap_content View by yourself. You need to process the padding in the onDraw method, viewGroup needs to process the post method in padding and margin View in onMeasure and onLayout, which can replace handler to stop the animation in onDetachedFromWindow of View. When preventing Memory leakage and sliding nesting, pay attention to sliding conflicts.
It is not easy to customize a beautiful View. Only multi-read, multi-write, and multi-test can we better master it. Create a wheel by yourself, and then compare the mature wheels to find gaps and deficiencies.
Welcome to reprint, reprint please indicate the original link http://blog.csdn.net/l664675249/article/details/50787973