How to use android to draw function Curve
This article mainly introduces how to use android to draw function curves, the article is very detailed, has a certain reference value, interested friends must read it!
/ / layout file / / MainActivity class package com.avi.myapplication5.app;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);}} / / DrawActivity class package com.avi.myapplication5.app;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet Import android.view.MotionEvent;import android.view.View;/** * Created by Administrator on 14-4-18. * / public class DrawActivity extends View implements View.OnTouchListener {float (float) Math.PI of float PI= (float) Math.PI;//PI=3.1415 is too troublesome, customize one. Float canvasWidth,canvasHeight;// canvas width, height float width,height;// Custom length and width float left,up;// Custom position in the upper left corner public DrawActivity (Context context, AttributeSet attrs) {super (context, attrs);} @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); / / be sure to call the Init () initialization parameter first. There are no default values for my custom parameters. Init (canvas); Render (canvas);} @ Override public boolean onTouch (View view, MotionEvent event) {return false;} / / initialize global parameters. / / it is recommended not to change the parameters initialized here in other functions, so as not to get the correct result void Init (Canvas canvas) {/ / get canvas width and height canvasWidth= (float) canvas.getWidth (); canvasHeight= (float) canvas.getHeight (); / / Custom length and width width=2*PI;height=2*PI*canvasHeight/canvasWidth / / customize the position of the upper left corner left=-width/2;up=height/2;} / / render the screen. / / the drawing steps are here. Void Render (Canvas canvas) {/ / create a new brush Paint paint=new Paint (); / / draw gridlines. Paint.setARGB (255,255,0,0); DrawGrid (canvas,0.3f,0.3f,paint); / / draw coordinate lines. Paint.setARGB; DrawCoord (canvas,paint); / / displays the names of horizontal and vertical axes and origin. (important note: here is what you want) paint.setARGB (255100100100); DrawCoordName (canvas,paint, "t", "f (t)", "O"); / / draw a curve DrawCurve (canvas,paint);} / draw gridlines. / / draw the grid before the coordinates, otherwise the grid lines will cover the coordinate lines so that you can't see the coordinate lines / / void DrawGrid (Canvas canvas,float dx,float dy,Paint paint) {/ / draw the vertical grid lines / / the left float x lines; while (x > left) {canvas.drawLine (PX (x), PY (up), PX (x), PY (up-height), paint) Xmurf while;} / / on the right side of the box (x)