Due to work needs recently in the study of animation, the following code from http://blog.csdn.net/tianjian4592/article/details/45157787, the original source, but the blogger has given the key code, this person is very cow, Animation drawing this piece of writing is very fine, easy to understand, suggest to see, their own slight modification, adjust, as notes, to solve other people's ideas, to achieve the work needs; First look at the effect:
1. First to understand the next Canvas.drawbitmap (Mbitmap, Msrcrect, Mdestrect, Mbitpaint);
When drawing the picture, use, the parameter respectively, the picture bitmap, draws the bitmap own area, draws the bitmap on the mobile phone to display the position area, the brush;
Msrcrect,Mdestrect are all objects of rect (int left, int top, int right, int bottom);
2. Ideas
A. Floating pictures, may be small balls, stars, snowflakes and the like, according to demand, choose a number of different little pictures, the first name of their floating stars;
B. According to the effect, floating picture settings actually related data, such as coordinates, size, transparency, moving speed of water, moving direction, etc.;
C. Then initialize the above data, generate a batch of small balls, to draw;
D. Create a thread or handle to cause a timer to constantly refresh, while modifying the floating star properties,
3. Code
A. Define a class that inherits the view, initializes the brush, requires several stars, sets a different speed
private void Initpaint () {paint = new paint (Paint.anti_alias_flag); Anti-jitter Paint.setdither (true); Turn on image filtering Paint.setfilterbitmap (true); }
/** * Set animation target, Three different sizes, style varies, for aesthetics * init bitmap info */private void Initbitmapinfo () {Mstarone = ((bitmapdrawable) mRes Ources.getdrawable (R.DRAWABLE.STAR2)). Getbitmap (); Mstaronewidth = Mstarone.getwidth (); Mstaroneheight = Mstarone.getheight (); Mstartwo = ((bitmapdrawable) mresources.getdrawable (R.DRAWABLE.STAR1)). Getbitmap (); Mstartwowidth = Mstartwo.getwidth (); Mstartwoheight = Mstartwo.getheight (); Mstarthree = ((bitmapdrawable) mresources.getdrawable (R.DRAWABLE.STAR3)). Getbitmap (); Mstarthreewidth = Mstarthree.getwidth (); Mstarthreeheight = Mstarthree.getheight (); }
Define three different speed floating speeds private void initdata (context context) { mresources = getresources (); Displaymetrics DM = Context.getapplicationcontext (). Getresources (). Getdisplaymetrics (); Mtotalwidth = Dm.widthpixels; Mtotalheight = Dm.heightpixels; LOG.I (TAG, "mtotalwidth=" + mtotalwidth + "--1--mtotalheight=" + mtotalheight); Set three speed values of different sizes mfloattranslowspeed = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 0.5f, Mresources.getdisplaymetrics ()); Mfloattransmidspeed = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 0.75f, Mresources.getdisplaymetrics ()); Mfloattransfastspeed = (int) typedvalue.applydimension (Typedvalue.complex_unit_dip, 1f, Mresources.getdisplaymetrics ()); }
B. Initialize the star, because after the initialization of some basic data in the construction method, and then the measurement, I put the initialization of the Star method
Onmeasure method.
/** * Initialize star information */private void Initstarinfo () {StarInfo StarInfo = null; Random random = new random (); for (int i = 0; i < Mfloatcount; i++) {//Get star size ratio float starsize = Getstarsize (0.4f, 0.8f); The coordinates of the small ball float[] starlocation = star_location[i]; StarInfo = new StarInfo (); Initialize star size starinfo.sizepercent = starsize; Initialize the float speed int randomspeed = Random.nextint (3); Switch (randomspeed) {case 0:starinfo.speed = mfloattranslowspeed; Break Case 1:starinfo.speed = mfloattransmidspeed; Break Case 2:starinfo.speed = mfloattransfastspeed; Break Default:starInfo.speed = Mfloattransmidspeed; Break }//Initialize the transparency of the stars Starinfo.alpha = Getstarsize (0.3f, 0.8f); Initialize star position starinfo.xlocation = (int) (starlocation[0] * mtotalwidth); starinfo.ylocation = (int) (starlocation[1] * mtotalheight); LOG.I (TAG, "xlocation =" + starinfo.xlocation + "--ylocation =" + starinfo.ylocation); LOG.I (TAG, "stonesize =" + starsize + "---stonealpha =" + Starinfo.alpha); Initialize star Position starinfo.direction = Getstardirection (); Mstarinfos.add (StarInfo); } }Star_location[] Array of artificial to determine the number of stars in the screen size of the mobile phone, the location of their own tried randomly generated some data, but sometimes get together, you should find a mobile phone screen randomly do not repeat the generation of point coordinates algorithm, is thinking, there will be, give me the next, learn
C. Set the direction of movement of the stars, here is just the normal right and left, diagonal direction, you can add other trajectory direction
/** * Different moving tracks, in addition to the left and right, can also define other directions, such as diagonal, curve and so on * Initialize the star direction of operation * /private int getstardirection () { int Randomint; Random random = new random (); if (floattyep==100) { randomint = Random.nextint (3); } else { randomint=floattyep; } int direction = 0; Switch (randomint) {case 0: direction = left; break; Case 1: direction = right; break; Case 2: direction = TOP; break; Case 3: direction = BOTTOM; break; Case 4: direction = Free_point; break; Default: Break ; } return direction; }
D. When repeating the drawing, modify the direction of the motion trajectory of the ball, add case type, such as some positive cosine trajectory, diamond running on the phone, polyline trajectory, etc.
private void Resetstarfloat (StarInfo StarInfo) {switch (starinfo.direction) {case left: if (Starinfo.xlocation < -20) {starinfo.xlocation = Mtotalwidth; } else {starinfo.xlocation-= starinfo.speed; } break; Case Right:if (Starinfo.xlocation > mtotalwidth+20) {starinfo.xlocation = 0; } else {starinfo.xlocation + = Starinfo.speed; } break; Case Top:if (Starinfo.ylocation < -20) {starinfo.ylocation = Mtotalheight; } else {starinfo.ylocation-= starinfo.speed; } break; Case Bottom:if (Starinfo.ylocation > mtotalheight+30) {starinfo.ylocation = 0; } else {StarINfo.ylocation + = Starinfo.speed; } break; Case Free_point:if (Starinfo.ylocation > mtotalheight+30) {starinfo.ylocation = 0; } else {starinfo.ylocation + = Starinfo.speed; } if (Starinfo.xlocation < -20) {starinfo.xlocation = Mtotalwidth; } else {starinfo.xlocation-= starinfo.speed; } break; Default:break; } }
Above the 20,30 is casually add, is to let the stars run outside the phone screen, and then re-enter the interface, or directly move to the screen boundary, start again, will flash a bit, the effect is not good;
E. Making a drawing
@Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); for (int i = 0; i < mstarinfos.size (); i++) {StarInfo StarInfo = Mstarinfos.get (i); Drawstardynamic (i, starInfo, canvas, paint); }} private void drawstardynamic (int count, StarInfo StarInfo, canvas canvas, Paint Paint) {resetstarfloat (starInfo); float Staralpha = Starinfo.alpha; int xlocation = starinfo.xlocation; int ylocation = starinfo.ylocation; float sizepercent = starinfo.sizepercent; xlocation = (int) (xlocation/sizepercent); ylocation = (int) (ylocation/sizepercent); Bitmap Bitmap = null; Rect srcrect = null; Rect destrect = new rect (); Mstaronesrcrect = new Rect (0, 0, mstaronewidth, mstaroneheight); if (count% 3 = = 0) {bitmap = Mstarone; Srcrect = Mstaronesrcrect; Destrect.set (Xlocation, ylocation, Xlocation + mstaronewidth, ylocation + mstaroneheight); } else if (count% 2 = = 0) {bitmap = Mstarthree; Srcrect = Mstarthreesrcrect; Destrect.set (Xlocation, ylocation, Xlocation + mstarthreewidth, ylocation + mstarthreeheight); } else {bitmap = Mstartwo; Srcrect = Mstartwosrcrect; Destrect.set (Xlocation, ylocation, Xlocation + mstartwowidth, ylocation + mstartwoheight); } paint.setalpha ((int) (STARALPHA * 255)); Canvas.save (); Canvas.scale (Sizepercent, sizepercent); Canvas.drawbitmap (Bitmap, srcrect, destrect, paint); Canvas.restore (); }F. Time-lapse, realize the effect of moving
Handler handler=new Handler () { @Override public void Handlemessage (Message msg) { super.handlemessage ( msg); if (isruning) { postinvalidate (); Handler.sendmessagedelayed (Message.obtain (), ());}} ;
public void Startanimationfloat () { isruning=true; Handler.sendmessage (Message.obtain ()); } public void Stopanimationfloat () { isruning=false; } public void Restartanimationfloat () { startanimationfloat (); }
Basically this, then use the Floatview in the activity layout, set the different motion direction trajectory;
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_float); int typekey = Getintent (). Getintextra ("Type_key", 0); Floatview startbtn = (floatview) Findviewbyid (R.ID.FLOAT_BTN); Startbtn.setfloattype (Floatview.free_point); if (typekey==1) {startbtn.setfloattype (floatview.default_type); }else if (typekey==2) {startbtn.setfloattype (floatview.free_point); }else if (typekey==3) {startbtn.setfloattype (floatview.top); }else if (typekey==4) {startbtn.setfloattype (floatview.bottom); }else if (typekey==5) {startbtn.setfloattype (floatview.left); }else if (typekey==6) {startbtn.setfloattype (floatview.right); } startbtn.startanimationfloat (); }
Source Code
Floating animation of Android, snow animation