First, the core idea of the algorithm
1, each interpolation requires four base points (tentatively assumed to be a, B, C, D).
2, according to the known four base points, the interpolation algorithm can only achieve a smooth curve between the middle two points (here is the B and C points).
Second, the project code
1, "Catmull_rom interpolation algorithm" to draw the class of smooth curve (Catmull_rom.java)
</pre><pre name= "code" class= "java" >package com.example.test;import Java.util.arraylist;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.graphics.path;import Android.util.attributeset;import Android.view.view;public class Catmull_rom extends View {private final Paint Mgesturep aint = new Paint (); Private final Path MPath = new Path (); Private arraylist<point> point = new arraylist<point> (); Private arraylist<point> save = new arraylist<point> (); Public Catmull_rom (Context context) {super (context); } public Catmull_rom (context context, AttributeSet Attrs) {Super (context, attrs); Mgesturepaint.setantialias (TRUE); Mgesturepaint.setstyle (Style.stroke); Mgesturepaint.setstrokewidth (5); Mgesturepaint.setcolor (Color.White); } public Catmull_rom (context context, AttributeSet attrs, int defstyle) {super (COntext, Attrs, Defstyle); } @Override protected void OnDraw (canvas canvas) {//TODO auto-generated Method stub super.ondraw (canvas); Point.add (new point (0, 0)); Point.add (New Point (1, 1)); Point.add (New Point (80, 100)); Point.add (New Point (160, 60)); Point.add (new Point (240, 120)); Point.add (new Point (320, 30)); Point.add (New Point (400, 200)); Point.add (new Point (401, 201)); Function_catmull_rom (point, +, save, MPath); Canvas.drawpath (MPath, mgesturepaint); } public void Function_catmull_rom (arraylist<point> point, int cha, arraylist<point> Save, Path path) {if (Point.size () < 4) {return; } path.moveto (Point.get (0). x, Point.get (0). Y); Save.add (point.get (0)); for (int index = 1, index < Point.size ()-2; index++) {Point p0 = Point.get (index-1); Point P1 = Point.get (index); Point P2 = point.get (index + 1); Point p3 = point.get (index + 2); for (int i = 1; I <= cha; i++) { float T = i * (1.0f/cha); Float TT = t * t; float TTT = tt * t; Point pi = new Point (); Intermediate point pi.x = (float) (0.5 * (2 * p1.x + (p2.x-p0.x) * t + (2 * p0.x-5 * p1.x + 4 * p2.x-p3.x) * TT + (3 * p1.x-p0.x-3 * p2.x + p3.x) * ttt)); PI.Y = (float) (0.5 * (2 * p1.y + (P2.Y-P0.Y) * t + (2 * p0.y-5 * P1.Y + 4 * p2.y-p3.y) * TT + (3 * p1.y-p0.y-3 * P2.Y + p3.y) * ttt)); Path.lineto (pi.x, PI.Y); Save.add (PI); PI = null; }} path.lineto (Point.get (Point.size ()-1). x, Point.get (Point.size ()-1). Y); Save.add (Point.get (Point.size ()-1)); }}2. Main activity (Mainactivity.java)
Package Com.example.test;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); }}
3. Main layout
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= "com.example.test.MainActivity" > <com.example.test.catmull_rom android:id= "@+ Id/path " android:layout_width=" wrap_content " android:layout_height=" Wrap_content "/></ Relativelayout>
4. Coordinate Class (Point.java)
Package Com.example.test;public class Point {public float x; public float y; Public Point () { } public point (float x, float y) { super (); this.x = x; This.y = y; } public float GetX () { return x; } public void SetX (float x) { this.x = x; } public float GetY () { return y; } public void sety (float y) { this.y = y; }}
Third, the realization is as follows:
Android uses catmull_rom interpolation algorithm to draw smooth graphs