On the custom view, I believe most developers are already very familiar with the network of examples are also very many, a variety of cool hanging day custom view is also endless. This article is only a primary learning course, which is of reference value to beginners.
The following formally enters the subject.
This article uses the custom view to implement the Histogram Bargraphview, realizes the basic function of the histogram, because it is to learn the custom view, so the extensibility is poor, can only be used as the reference of the custom view.
On
The view is displayed on the screen mainly through these three processes
(1) Measure (measurement)
First, the view needs to measure its size, including its length and width. when the member function of the View class measure decides to re-measure the width and height of the current view, it calls another member function onmeasure to actually perform the operation of measuring width and height. Therefore, most custom view needs to overwrite the Onmeasure method to measure the size of the view. The Onmeasure method is as follows:
@Override public void onmeasure (int widthmeasurespec, int heightmeasurespec) { super.onmeasure ( Widthmeasurespec, Heightmeasurespec); }
where Widthmeasurespec and heightMeasurespec Two parameters are the recommended size for the parent view. The value of the parameter measurespec is actually composed of two parts, the highest 2 bits represents a measurement mode, while the low 30 bit represents a width value or height value, the measurement mode has three kinds, respectively is measurespec.exactly, Measurespec.unspecified and Measurespec.at_most, there is no way to tell the difference between the three modes. The two values will be referenced in Bargraphview to determine the size of the Bargraphview.
(2) Layout (layouts)
This process is only useful in the view container (ViewGroup and its subclasses), because the position of the non-container Class View on the screen is determined by the parent control, so there is no need to overwrite the OnLayout () method.
(3) Draw (Drawing)
Finally, the process of drawing the view, in this process, mainly through the paint object on the canvas to draw the corresponding image, and finally to display the view on the screen. For a custom view, it is often necessary to overwrite the OnDraw () method to draw the view.
@Override public void OnDraw (canvas canvas) { }
This method provides a canvas where we only need to create a brush to draw the pattern on the canvas.
Well, knowing the process, let's start implementing Bargraphview.
The Bargraphview main code concentrates on the draw process and draws the chart to the screen by means of the OnDraw method.
After analysis, the column chart is divided into the following parts
1. Horizontal/Vertical axes
2. Horizontal/Vertical axis tick marks
3. Horizontal/Vertical axis scale values
4. Horizontal/Vertical Axis arrows
5. Title
6. Bar chart
Use DrawLine () (Draw straight lines), DrawText () (Draw text), DrawPath (Draw polygons), and DrawRect () (draw rectangles) to draw the corresponding patterns for different parts.
Here is the Bargraphview class code, which can be viewed directly from the comment.
Package Com.eleven.demo.widget;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.Path; Import Android.util.attributeset;import android.view.view;import com.eleven.demo.r;/** * Created by eleven on 2015/5/3. */public class Bargraphview extends View {private final String TAG = BarGraphView.class.getName (); Brush private paint mpaint; Title Private String title; Title Color private int titlecolor; Title size private float titlesize; X axis maximum value private float Maxaxisvaluex = 900; X axis number of tick marks private int axisdividesizex = 9; Y axis maximum value private float Maxaxisvaluey = 700; Y axis number of tick marks private int axisdividesizey = 7; View width private int width; View height private int height; Coordinates origin location private final int originx = 100; Private final int originy = 800; Histogram data private int columninfo[][]; Public BargraphvieW (context context, AttributeSet Attrs) {Super (context, attrs); Create Brush mpaint = new Paint (); Gets the configured property value TypedArray Marray = Context.obtainstyledattributes (Attrs, R.styleable.bargraphview); title = Marray.getstring (R.styleable.bargraphview_bargraph_title); Titlecolor = Marray.getcolor (R.styleable.bargraphview_bargraph_titlecolor, Color.Black); Titlesize = Marray.getdimension (r.styleable.bargraphview_bargraph_titlesize, 36); }/** * Sets the maximum and number of tick marks on the x-axis (including 0-coordinate scale) * * @param maxValue X-Axis maximum * @param dividesize number of tick marks * * public void Setaxisx (float maxValue, int dividesize) {Maxaxisvaluex = MaxValue; Axisdividesizex = dividesize; }/** * Sets the maximum and number of tick marks on the y-axis (including 0-coordinate scale) * * @param maxValue y-Axis maximum * @param dividesize number of tick marks * * public void Setaxisy (float maxValue, int dividesize) {maxaxisvaluey = MaxValue; Axisdividesizey = dividesize; }/** * Set column chart data * * @param columninfo */public void SetColumnInfo (int[][] columninfo) {this.columninfo = Columninfo; } @Override public void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, Heightmeasurespec); width = measurespec.getsize (widthmeasurespec)-200; Height = measurespec.getsize (heightmeasurespec)-800; } @Override public void OnDraw (canvas canvas) {DRAWAXISX (canvas, mpaint); Drawaxisy (canvas, mpaint); Drawaxisscalemarkx (canvas, mpaint); Drawaxisscalemarky (canvas, mpaint); DRAWAXISARROWSX (canvas, mpaint); Drawaxisarrowsy (canvas, mpaint); Drawaxisscalemarkvaluex (canvas, mpaint); Drawaxisscalemarkvaluey (canvas, mpaint); Drawcolumn (canvas, mpaint); Drawtitle (canvas, mpaint); }/** * Draw horizontal axis (x axis) * * @param canvas * @param paint */private void Drawaxisx (canvas canvas, Pai NT paint) {Paint. SetColor (Color.Black); Set the brush width paint.setstrokewidth (5); Sets the brush anti-aliasing Paint.setantialias (true); Draw horizontal axis (X) canvas.drawline (Originx, Originy, Originx + width, originy, paint); /** * Draw ordinate axis (Y axis) * * @param canvas * @param paint */private void Drawaxisy (canvas canvas, Pai NT paint) {//Draw vertical axis (Y) canvas.drawline (Originx, Originy, Originx, originy-height, paint);//Parameter description: Starting point left X, y, end sit Mark X, y, brush}/** * Draw horizontal axis tick marks (x axis) * @param canvas * @param paint */private void Drawaxisscalem Arkx (canvas canvas, paint paint) {float cellwidth = Width/axisdividesizex; for (int i = 0; i < axisDivideSizeX-1; i++) {Canvas.drawline (Cellwidth * (i + 1) + Originx, Originy, cell Width * (i + 1) + Originx, originY-10, paint); }}/** * Draw vertical axis tick marks (Y axis) * * @param canvas * @param paint */private void Drawaxisscalemarky (C Anvas canvas, Paint paint) {Float cellheight = Height/axisdividesizey; for (int i = 0; i < axisDivideSizeY-1; i++) {Canvas.drawline (Originx, (Originy-cellheight * (i + 1)), O Riginx + ten, (Originy-cellheight * (i + 1)), paint); }}/** * Draw the horizontal axis scale value (x axis) * * @param canvas * @param paint */private void Drawaxisscalemarkval Uex (canvas canvas, paint paint) {//sets the brush drawing text Properties Paint.setcolor (Color.gray); Paint.settextsize (28); Paint.setfakeboldtext (TRUE); float cellwidth = Width/axisdividesizex; float cellvalue = Maxaxisvaluex/axisdividesizex; for (int i = 1; i < Axisdividesizex; i++) {Canvas.drawtext (string.valueof (Cellvalue * i), cellwidth * i + O riginX-35, Originy +, paint); }}/** * Draw the vertical axis scale value (Y axis) * * @param canvas * @param paint */private void Drawaxisscalemarkval Uey (canvas canvas, paint paint) {Float cellheight = HEIGHT/AXISDIVidesizey; float cellvalue = Maxaxisvaluey/axisdividesizey; for (int i = 1; i < Axisdividesizey; i++) {Canvas.drawtext (string.valueof (Cellvalue * i), originX-80, Ori Giny-cellheight * i + ten, paint); }}/** * Draw a horizontal Axis arrow (x axis) * * @param canvas * @param paint */private void drawaxisarrowsx (Canva s canvas, Paint paint) {//Draw triangles (X-axis arrows) path MPATHX = new Path (); Mpathx.moveto (Originx + width +, originy);//Start point Mpathx.lineto (Originx + width, originY-10);//Next point Mpathx . LineTo (Originx + width, originy + 10);//Next point mpathx.close (); Canvas.drawpath (mpathx, paint); /** * Draw a vertical Axis arrow (Y axis) * * @param canvas * @param paint */private void drawaxisarrowsy (canvas can VAS, Paint paint) {//Draw triangles (Y-axis arrows) path MPATHX = new Path (); Mpathx.moveto (Originx, originy-height-30);//starting point Mpathx.lineto (originX-10, originy-height);//Next pointMpathx.lineto (Originx + ten, originy-height);//Next point mpathx.close (); Canvas.drawpath (mpathx, paint); }/** * Draw histogram * * @param canvas * @param paint */private void Drawcolumn (canvas canvas, paint p Aint) {if (Columninfo = = null) return; float cellwidth = Width/axisdividesizex; for (int i = 0; i < columninfo.length; i++) {Paint.setcolor (columninfo[i][1]); float lefttopy = originy-height * columninfo[i][0]/maxaxisvaluey; Canvas.drawrect (Originx + cellwidth * (i + 1), lefttopy, Originx + cellwidth * (i + 2), Originy, mpaint);//upper left corner x, y, x, y, pen }}/** * Draw title * * @param canvas * @param paint */private void Drawtitle (canvas canv As, paint paint) {//Draw caption if (title! = NULL) {//Set brush drawing Text Properties Mpaint.setcolor (titlecolor ); Mpaint.settextsize (titlesize); Mpaint.setfakeboldtext (TRUE); Canvas.drawtext (title, Originy +, paint); } }}
Mainactivity Layout file:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " xmlns:eleven=" Http://schemas.android.com/apk/res-auto " android:layout_width = "Match_parent" android:layout_height= "match_parent" android:paddingleft= "@dimen/activity_horizontal_ Margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen/activity_ Vertical_margin " android:paddingbottom=" @dimen/activity_vertical_margin " tools:context=". Mainactivity "> <com.eleven.demo.widget.bargraphview android:id=" @+id/custom_view " android: Layout_width= "600DP" android:layout_height= "wrap_content" eleven:bargraph_title= "Custom histogram" Eleven : bargraph_titlesize= "24sp" eleven:bargraph_titlecolor= "@android: Color/black"/></relativelayout>
Use in Mainactivity
public class Mainactivity extends Actionbaractivity { private bargraphview mbargraphview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Initview (); } private void Initview () { Mbargraphview = (bargraphview) Findviewbyid (R.id.custom_view); MBARGRAPHVIEW.SETAXISX (9); Mbargraphview.setaxisy (700,7); int columninfo[][] = new int[][]{{600, color.blue},{500, color.green},{400, color.red},{300, Color.Blue}, {500, color.yellow},{300, color.ltgray},{200, Color.Blue}}; Mbargraphview.setcolumninfo (Columninfo); }
The custom view implementation uses a custom attribute to define three property values, referring to how the Attrs.xml file is used http://blog.csdn.net/jiangwei0910410003/article/details/17006087
Android Custom view------column charts