:
Realize the source code:
/** * functioncurveview.java * @author Lanfog * @datetime a01b-6-ab pm B :38:01 */package me.lanfog.myandroid.widget;import android.content.context;import android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import android.util.attributeset;import android.view.view;/** * function Graph */public class Curveview extends view {private paint mpaint;private curveunit mcurveunit = new curveunit () {@Overridepublic int getcentery () {// TODO Auto-generated method stubreturn getheight ()/2;} @Overridepublic int getcenterx () {// todo auto-generated method stubreturn getwidth ()/2;} @Overridepublic float function (float x) {// todo auto-generated method stubreturn (float) math.sin (x);}; public Curveview (context context, attributeset attrs, int defstyleattr) {super (Context, &NBSP;ATTRS,&NBSP;DEFSTYLEATTR);// todo auto-generated constructor stubinit ();} Public curveview (context context, attributeset attrs) {super (context, attrs);// todo auto-generated constructor stubinit ();} Public curveview (Context context) {super (Context);// todo auto-generated Constructor stubinit ();} Private void init () {mpaint = new paint (); Mpaint.setantialias (true); MPaint.setStyle ( Paint.Style.STROKE);} @Overrideprotected void onmeasure (Int widthmeasurespec, int heightmeasurespec) { Int measuredwidth = getdefaultsize (Getsuggestedminimumwidth (), widthmeasurespec);int Measuredheight = getdefaultsize (Getsuggestedminimumheight (), heightmeasurespec); Setmeasureddimension (measuredwidth, Measuredheight);} @Overrideprotected void ondraw (Canvas canvas) {int w = getwidth (); // Control width int h = getheight (); // control height int p = mcurveunit.getpadding (); // control Inner Fill Int cx = mcurveunit.getcenterx (); // Center horizontal int cy = mcurveunit.getcentery (); // Center ordinate mpaint.setstrokewidth (2); Canvas.drawline (p, cy, w - p, cy, mpaint); Canvas.drawline (Cx, p, cx, h - p, mpaint); Float pr = mcurveunit.getprecision (); // scale accuracy float sp = Mcurveunit.getspacing (); // scale interval float x, y;mpaint.setstrokewidth (1); MPaint.setTextSize (12) ;int a = 3; // Short-term scale length int b = 8; // long line scale length// Medium Canvas.drawtext ("0", cx + 15, cy + 15, mpaint);// left for (int i=-1;; i--) {X = cx&nbsP;+ i * sp;if (x < p) break;if (i % 5 == 0) { // Draw a number Canvas.drawline (X, cy, x, cy - b, mpaint) per 5 ticks; Canvas.drawtext ("" + i * pr, x, cy + 15, mpaint);} Elsecanvas.drawline (x, cy, x, cy - a, mpaint);} ON for (int i=1;; i++) {y = cy - i * sp;if (y < p) break;if (i % 5 = = 0) {canvas.drawline (cx, y, cx + b, y, mpaint); Canvas.drawText ("" + i * pr, cx + 10, y, mpaint);} Elsecanvas.drawline (cx, y, cx + a, y, mpaint);} Right for (int i=1;; i++) {x = cx + i * sp;if (x > w - p) break;if (i % 5 == 0) {canvas.drawline (x, cy, x, cy - b, mpaint); Canvas.drawTExt ("" + i * pr, x, cy - 10, mpaint); Elsecanvas.drawline (x, cy, x, cy - a, mpaint);} Under for (int i=-1;; i--) {y = cy - i * sp;if (y > h - p) break;if (i % 5 == 0) {canvas.drawline (cx, y, cx + b, y, mpaint); Canvas.drawtext (" + i * pr, cx + 10, y, mpaint"); Elsecanvas.drawline (cx, y, cx + a, y, mpaint);} Mpaint.setcolor (color.red);boolean s = false; // is the starting point float tx, ty; / / Temporary Save coordinate point float lx = 0f, ly = 0f; // last save coordinate point// negative for (int i=0;; i--) {if (cx + i * sp < p) break;tx = x = i * pr;ty = y = mcurveunit.function (x); X = cx + i *&nbsP;sp;y = cy - y / pr * sp;if (i % 10 == 0) // Draw One (x, y) value per 10 ticks Canvas.drawtext ("(" + string.format ("%.2f", tx) + "," + string.format ("%.2f", ty) + ")", x, y, mpaint), if (s) canvas.drawline (LX, ly, x, y, mpaint); else s = true;lx = x;ly = y;} s = false;// Positive for (int i=0;; i++) {if (cx + i * sp > w - p) break;tx = x = i * pr;ty = y = mcurveunit.function (x); x = cx + i * sp;y = cy - y / pr * sp;if (i % 10 == 0) Canvas.drawtext ("(" + string.format ("%.2f", tx) + "," + string.format ("%. 2f ", ty) + "), x, y, mpaint), if (s) canvas.drawline (LX,&NBSP;LY,&NBSp;x, y, mpaint); else s = true;lx = x;ly = y;}} public static abstract class curveunit {/** * function Implementation */public Abstract float function (float x);/** * gets the horizontal position of the center */public abstract int getcenterx ();/** * gets the vertical position of the center */public abstract int getcentery ();/** * Get scale accuracy */public float getprecision () {return 0.2f;} /** * Get tick interval */public float getspacing () {return 10f;} /** * Get internal fill */public int getpadding () {return 5;}} Public curveunit getcurveunit () {return mcurveunit;} Public void setcurveunit (Curveunit mcurveunit) {this.mcurveunit = mcurveunit;}}
Customizing the Android Function graph control