I am not busy recently, so I look back on what I have learned and contacted in this year. I suddenly feel that blogging is a good way, and I hope I can stick to it.
Custom View process
- Create a custom class to inherit the View, and override the constructor. There are four constructors in total. We only need to inherit the first two for now
public CircleView(Context context) { super(context); } public CircleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }
The first construction method is used when you dynamically add a custom View
The second construction method is called when used in xml
<example.com.testactivity.CircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/>
- Override onDraw() method
/** * Override the method of drawing * @param canvas */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.parseColor("#018172 "); / / 1. Set the background color float radius=100; //radius float x=100; float y=100; Paint paint=new Paint(); //Instantiate a paint object brush paint.setColor(Color.WHITE); // 2. Set brush color paint.setStyle(Paint.Style.STROKE); //Void, default is solid canvas.drawCircle(x,y,radius,paint); //Specifies the shape drawn circle }
paint.setStyle(Paint.Style.STROKE) sets the style of the brush. You can specify whether the center of the circle is solid or hollow. This method is mainly used for rectangle and circle
-
Style.FILL: solid
-
Style.fill'and'strike: show both black and white
-
Style.STROKE: void
-
Override onMeasure method
/** - Function: if you want to achieve adaptive size in xml, you need to rewrite this method to give the View a raw size value - @param widthMeasureSpec - @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width=200; int height=200; //Set the original size of View adaption int HEIGHT_MODE=MeasureSpec.getMode(heightMeasureSpec); // 1. Extract the mode (one of the following three modes) according to the provided measurement value (format) int HEIGHT_SIZE=MeasureSpec.getSize(heightMeasureSpec); //2. Extract the size value according to the provided measurement value (format) (this size is what we usually call the size) int WIDTH_MODE=MeasureSpec.getMode(widthMeasureSpec); int WIDTH_SIZE=MeasureSpec.getSize(widthMeasureSpec); if (HEIGHT_MODE==MeasureSpec.EXACTLY){ //If the measurement mode is accurate, use the obtained value directly height=HEIGHT_SIZE; }else if (HEIGHT_MODE==MeasureSpec.AT_MOST){ height=Math.min(height,HEIGHT_SIZE); //If wrap content is set in xml, the minimum value is taken } if (WIDTH_MODE==MeasureSpec.EXACTLY){ width=WIDTH_SIZE; }else if (WIDTH_MODE==MeasureSpec.AT_MOST){ width=Math.min(width,WIDTH_SIZE); } setMeasuredDimension(width,height); //Use this method to set the real size after measurement }
Note: measurespec.getmode (highmeasurespec) means to get the high mode set in xml
There are three modes
- Exact mode when the set length is match [parent] or given a specific size
- At most when set to wrap content
- UNSPECIFIED this mode is rarely used
Reference link