Android Path Animation production

Source: Internet
Author: User

1. Preface

Today's project to use a similar to send the DA, the kind of screen floating expression function, so the analysis and research, the technology used should be the path animation, do not know that this is not correct, anyway, is to draw a path line, and then the object according to the path to move. So I called him the path animation.

The first problem to be solved in path animation is how to draw this path? Then how do you take the coordinates of all the points on the path after the path is drawn?

This solves both problems by looking at a class Pathmeasure this class receives a path object and can then get the coordinate value of the length scale based on the Pathmeasure.getpostan (). These two problems are directly taken care of. Using path to draw a path and then take a point, moving the object dynamically, it becomes the path animation. is not very simple.


2, look at the effect




3. Core code


/** * * * * * * * * * * @author yd * knowledge points used: 1, Android Property Animation 2, the path path to draw 3, Bezier curve * * * * * * * * * */public class flloweranimation Ext Ends View implements Animatorupdatelistener {/** * animation changed attribute value */private float phase1 = 0f;private float Phase2 = 0f;private float PHASE3 = 0f;/** * Ball set */private list<fllower> fllowers1 = new arraylist<fllower> ();p rivate List<fllo wer> fllowers2 = new arraylist<fllower> ();p rivate list<fllower> fllowers3 = new arraylist<fllower> ();/** * The time of the animation playback */private int times = 4000;/** * Animation interval */private int delay = 500;/** * Resource id *///private int resId = R.drawa Ble.fllower_love;public flloweranimation (Context context) {super (context); init (context);//this.resid = ResId;} @SuppressWarnings ("deprecation") private void init (context context) {WindowManager wm = (WindowManager) Context.getsystemservice (context.window_service); width = Wm.getdefaultdisplay (). getwidth (); height = (int) ( Wm.getdefaultdisplay (). GetHeight () * 3/2f) Mpaint = new Paint (); Mpaint.Setantialias (True); Mpaint.setstrokewidth (2); Mpaint.setcolor (Color.Blue); Mpaint.setstyle (Style.stroke); Pathmeasure = new Pathmeasure (); Builderfollower (Fllowercount, fllowers1); Builderfollower (Fllowercount, fllowers2); Builderfollower (Fllowercount, FLLOWERS3);} /** * Width */private int width = 0;/** * height */private int height = 0;/** * Curve height number split */private int quadcount = 10;/** * curvature */ Private float intensity = 0.2f;/** * First batch number */private int fllowercount = 4;/** * Create flower */private void builderfollower (int cou NT, list<fllower> fllowers) {int max = (int) (width * 3/4f); int min = (int) (width/4f); Random random = new random (), for (int i = 0; i < count; i++) {int s = random.nextint (max)% (Max-min + 1) + min; Path PATH = new Path (); CPoint CPoint = new CPoint (s, 0); List<cpoint> points = Builderpath (CPoint);d Rawfllowerpath (path, points); Fllower fllower = new Fllower (); Fllower.setpath (path); Fllowers.add (Fllower);}} /** * Draw Curve * @param path * @param points */private void Drawfllowerpath(Path path, list<cpoint> points) {if (points.size () > 1) {for (int j = 0; J < Points.size (); j + +) {CPoint point = Points.get (j); if (j = = 0) {CPoint n ext = Points.get (j + 1);p Oint.dx = ((next.x-point.x) * intensity);p Oint.dy = ((NEXT.Y-POINT.Y) * intensity);} else if (j = = Points.size ()-1) {CPoint prev = points.get (j-1);p oint.dx = ((point.x-prev.x) * intensity);p Oint.dy = ( (POINT.Y-PREV.Y) * intensity);} else {CPoint next = Points.get (j + 1); CPoint prev = points.get (j-1);p oint.dx = ((next.x-prev.x) * intensity);p Oint.dy = ((NEXT.Y-PREV.Y) * intensity);} Create the Cubic-spline pathif (j = = 0) {Path.moveto (point.x, point.y);} else {CPoint prev = points.get (j-1);p Ath.cub Icto (prev.x + PREV.DX, (prev.y + prev.dy), POINT.X-POINT.DX, (Point.y-point.dy), Point.x, Point.y);}}} /** * Curve swing amplitude */private int range = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip,, getresources (). Getdisplaymetrics ());/** * Draw Path * * @param point * @return */privateList<cpoint> Builderpath (CPoint point) {list<cpoint> points = new arraylist<cpoint> (); Random random = new random (), for (int i = 0, i < Quadcount; i++) {if (i = = 0) {points.add (point);} else {CPoint TMP = n EW CPoint (0, 0), if (Random.nextint ()% 2 = = 0) {tmp.x = Point.x + random.nextint (range);} else {tmp.x = Point.x-rando M.nextint (range);} TMP.Y = (int) (Height/(float) quadcount * i);p oints.add (TMP);}} return points;} /** * Brush */private Paint mpaint;/** * Coordinates location of the measurement path */private pathmeasure pathmeasure = null; @Overrideprotected void OnDraw (Ca Nvas canvas) {super.ondraw (canvas);d rawfllower (canvas, fllowers1);d rawfllower (canvas, fllowers2);d Rawfllower ( Canvas, FLLOWERS3);} /** * Height upward offset, move the start point out of the top of the screen */private float dy = typedvalue.applydimension (typedvalue.complex_unit_dip,40, Getresources () . Getdisplaymetrics ());/** * * @param canvas * @param fllowers */private void drawfllower (canvas canvas, LIST&LT;FLLOWER&G T Fllowers) {for (Fllower fllower:fllowers) {float[] pos = nEW Float[2];canvas.drawpath (Fllower.getpath (), Mpaint);p Athmeasure.setpath (Fllower.getpath (), false); Pathmeasure.getpostan (Height * fllower.getvalue (), POS, null); Canvas.drawcircle (Pos[0], pos[1], ten, mpaint);//bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), resId);//canvas.drawbitmap (bitmap, pos[0], pos[1]-dy, null);// Bitmap.recycle ();}} public void Startanimation () {Objectanimator MAnimator1 = Objectanimator.offloat (This, "Phase1", 0f,1f); Manimator1.setduration (time); Manimator1.addupdatelistener (this); Manimator1.start (); Manimator1.setinterpolator ( New Accelerateinterpolator (1f)); Objectanimator MAnimator2 = Objectanimator.offloat (This, "Phase2", 0f,1f); Manimator2.setduration (time); Manimator2.addupdatelistener (this); Manimator2.start (); Manimator2.setinterpolator ( New Accelerateinterpolator (1f)); Manimator2.setstartdelay (delay); Objectanimator MAnimator3 = Objectanimator.offloat (This, "Phase3", 0f,1f); manimator3.setduration (time); Manimator3.addupdatelistener (this); Manimator3.staRT (); Manimator3.setinterpolator (new Accelerateinterpolator (1f)); Manimator3.setstartdelay (delay * 2);} /** * With the new ball position * * @param value * @param fllowers */private void Updatevalue (float value, list<fllower> fllowers) {f or (Fllower fllower:fllowers) {fllower.setvalue (value);}} /** * Animation Change callback */@Overridepublic void onanimationupdate (Valueanimator arg0) {Updatevalue (GetPhase1 (), fllowers1); Updatevalue (GetPhase2 (), fllowers2); Updatevalue (GetPhase3 (), FLLOWERS3); LOG.I (tag, getPhase1 () + ""); invalidate ();} public float getPhase1 () {return phase1;} public void SetPhase1 (float phase1) {this.phase1 = Phase1;} public float GetPhase2 () {return phase2;} public void SetPhase2 (float phase2) {this.phase2 = Phase2;} public float getPhase3 () {return phase3;} public void SetPhase3 (float phase3) {this.phase3 = Phase3;} Private String tag = This.getclass (). Getsimplename ();p Rivate class CPoint {public float x = 0f;public float y = 0f;/** x-a XIs distance */public Float dx = 0f;/** y-axis distance */public float DY = 0f;public CPoint (float x, float y) {this.x = X;this.y = y;}}} 

4. Project Address

http://download.csdn.net/detail/hxc2008q/8473053




Android Path Animation production

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.