View

  • View represents a rectangular area of the screen and is responsible for drawing this area and handling events for this area
  • The View coordinate position is obtained by six methods
View left distance getLeft,View right distance getRight,View above the distance getTop View below the distance getBottom, width getWidth, height getHeightCopy the code

instantiation

1. Construction method

1).View(context); Constructors used in Java code, no values are set, only the default values must be built step by step through the set method. 2).view (Context,AttributeSet): constructor set in XML, used by the view.inflate method. The XML is loaded as a View object by the LayoutInflate, which passes the attribute to the AttributeSet and sets all the attributes that the View recognizes in Android through the View constructor. 3.) the View (the Context, the AttributeSet, int, (int)), and 2 are the same, there is a default theme. If the layout is imported through XML, the onFinishInflate method of the View is called.Copy the code

2. Measure the layout

onMeasure(MODE+width,MODE+height); 1). Fixed size :100dp match_parent EXACTLY 2). Automatic matching :warp_content AT_MOST 3). An error is reported for each unspecified use. UNSPECIFIED MeasureSpec consists of the maximum 2-bit mode and the last 30 bit size. GetSize getMode Creates a MeasureSpec makeMeasureSpecCopy the code

3. Calculate the location

onLayout(boolean,left,top,width,height); A method used to arrange child controls when customizing a layout. Call onSizeChanged when sizing is completeCopy the code

4. Draw the View

Determines how the View will be displayed onDraw(Canvas Canvas)Copy the code

BUG

  • ScrollView nested ListView, GridView, RecyclerView, can display only one data.
  • You have to override the AdapterView, override the onMeasure method.
int height=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, height);
Copy the code

Draw the ontouch

  • All View processing is done in onDraw, which is called only once when the View is loaded
  • If redrawing is required:
invalidate(); Called in the main thread and executed immediately. Child threads are not allowed to modify the UI, so you cannot call postInvalidate(); The child thread is called, the queue executes, and the child thread sends a draw message to the main thread before calling InvalidateCopy the code

Paint brush,

SetDither Anti-jitter setAntiAlias Anti-alialias setColor Set the color -------> RGB 0xffRRGGBB setAlpha Set the transparency ---->alpha 0xaaRRGGBB setStyle Sets the style: FILL,FILL_AND_STORKE fills with a border,STORKE only has a border setStorkeWidth: SetTextSize: Sets the font size. SetTextAlign: Sets the text alignment, drawn from left to right by defaultCopy the code

Canvas Canvas

Canvas is to draw various geometric shapes, paths, pictures, text circles on the screen: Rect(int left,top,right,bottom), RectF(float left,top,right,bottom); Point(int x,int y),PointF(float x,float y); Line: two points startX, statY endX, endY arc: RectF, startAngle, endAngle, useCenter rounded rectangle: RectF, horizontal and vertical filletCopy the code

Draw the path

1. MoveTo moves the brush to a point to start drawing 2. LineTo draws with a straight line 3. QuadTo draws with a curve 4.Copy the code

Bitmap

  • The commonly used method to import a Bitmap: BitmapFactory decodeResource/File/Stream
  • Use all images on behalf of Android
  • Written in XMLsrc="@mipmap/***"Code description is BitmapDrawable –> Bitmap+ map mode
  • Map mode: Three effects (1. Stretch, 2. Tile, 3. Mirror)
Common ways to create a Bitmap: Bitmap.create(Bitmap,source)// Copy an image bitmap. create(int width,int height,Config) // Create a blank image, Create (Bitmap source,int x,int y,int width,int height); Bitmap.create(int colors[],int width,int height,config); bitmap.create (int colors[],int width,int height,config); // Generates an image from an array of colorsCopy the code

Common methods to import a Bitmap:

BitmapFactory. DecodeResource/File/Stream image can be saved. Bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream (file)); // The method parameters are 1. Save format, 2. Save percentage of Bitmap, 3. Saved addressCopy the code
Bitmaps are forcibly reclaimed. Used bitmaps can be reclaimed in advance using this method, rather than waiting for GC to free memory. When an App runs out of OOM, 99% of it is a Bitmap fault.Copy the code
DrawBitmap (android,300,300,null); drawBitmap(android,300,300,null); // Select a rectangle from the target image, Drawing on the screen of a rectangular canvas. DrawBitmap (android, new the Rect (0, 0, android. GetWidth / 2, android. GetHeight / 2) new The Rect (200200300300), null);Copy the code

Brush apply colours to a drawing

1. Use gradients 2. Use images with rounded cornersCopy the code

Share with you

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day