respect the original, welcome reprint, reproduced please specify: from Ga_studio http://blog.csdn.net/tianjian4592
Note: Due to part of the reason, this article mainly explains the dynamic effect analysis idea, does not provide the source code to download, please forgive me ...
The previous article only talked about the Drawbitmap method in the canvas, and also said this method seems very greasy harm, can make a lot of good effect appearance, the next article is just as a small chestnut in the previous article, and further expand the use of Drawbitmap to complete the idea of dynamic effect!
Well, first the distortion can no longer be distorted:
Let's start by analyzing the effect above:
Suppose this is you just from the UE or the dynamic effect of the chicken wet hand to get above the dynamic design diagram, greeted by the boundless sky, floating planet created deep, vast universe, good, not many BB, for you will think of what kind of implementation plan?
1. Some students may think of creating a corresponding number of ImageView, and then for each imageview use animation or animator to do the corresponding movement effect;
2. Using their own way of drawing to achieve, is not the floating planet, we all to draw out;
Needless to say, the second scenario above is certainly preferable, and the first scenario has several drawbacks:
1. The number of view created is too large, the performance is too poor;
2. Poor flexibility, such as a UE or a product to increase or decrease the number of planets, will be a problem;
3. Do mobile animation for each view, the overhead is too large to be controlled or modified;
In view of this, we unswervingly walk ourselves to draw the line to complete the floating effect;
If you want to draw, you have to get the bitmap of the planet first, and get all the bitmaps according to the type of planet:
/** * Init Bitmap info * * private void Initbitmapinfo () { Mbackbitmap = ((bitmapdrawable) Mresources.getdrawable (R.drawable.back)) . Getbitmap (); Mbackwidth = Mbackbitmap.getwidth (); Mbackheight = Mbackbitmap.getheight (); Mstarone = ((bitmapdrawable) mresources.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 (); }
we have the background and the three types of planets, based on the above effect, we analyze, what are the characteristics of the data:
1. The same kind of planet is very small;
2. There is a difference in transparency between each other;
3. The direction of the float is not the same;
4. The speed of floating is not the same;
5. Each planet must have its own place;
We are only analyzing so much for the moment, based on which we abstract the planet object:
/** * Planet * @author Ajian * /Private class StarInfo { //zoom float sizepercent; X position int xlocation; Y position int ylocation; Transparency float Alpha; Floating direction int direction; Floating velocity int speed; }
In order to get some of the above data, we first write some data or methods:
1. In order to initialize the position of the planet, we use the array to define the position of a number of planets (based on the view width of the ratio), of course, you can also randomly, but randomly may appear in the situation:
private static final float[][] star_location = new float[][] { {0.5f, 0.2f}, {0.68f, 0.35f}, {0.5f, 0.05f}, {0.15f , 0.15f}, {0.5f, 0.5f}, {0.15f, 0.8f}, {0.2f, 0.3f}, {0.77f, 0.4f}, {0.75f, 0.5f}, {0.8f, 0.55f}, { 0 .1f, 0.7f}, { 0.1f, 0.1f}, {0.7f, 0.8f}, {0.5f, 0.6f} };
2. Ways to get the size of the planet (based on the original bitmap scaling scale):
/** * Get planet size * /private float getstarsize (float start, float end) { float nextfloat = (float) math.random (); if (Start < nextfloat && Nextfloat < end) { return nextfloat; } else { //If it is not in the desired data segment, then randomly, because no Break Recursive risk return (float) math.random ();} }
3. Define the three different speeds of the float:
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 ());
4. Ways to get the direction of the planet floating:
/** * Initialize the planet's direction of operation * /private int getstardirection () { random random = new random (); int randomint = Random.nextint (4); 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; Default: Break ; } return direction; }
With the above data and methods, we first initialize a certain number of planet data:
/** * Initialize Planet information */private void Initstarinfo () {StarInfo StarInfo = null; Random random = new random (); for (int i = 0; i < Mstarcount; i++) {//Get planet size ratio float starsize = Getstarsize (0.4f, 0.9f); Initialize the size of the planet float[] starlocation = star_location[i]; StarInfo = new StarInfo (); 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 Planet transparency STARINFO.ALpha = Getstarsize (0.3f, 0.8f); Initialize Planet position starinfo.xlocation = (int) (starlocation[0] * mtotalwidth); starinfo.ylocation = (int) (starlocation[1] * mtotalheight); Log ("xlocation =" + starinfo.xlocation + "--ylocation =" + starinfo.ylocation); Log ("stonesize =" + starsize + "---stonealpha =" + Starinfo.alpha); Initialize Planet Position starinfo.direction = Getstardirection (); Mstarinfos.add (StarInfo); } }
with this data, we can already draw the planet on the screen:
private void drawstardynamic (int count, StarInfo StarInfo, canvas canvas, paint paint) {float Staralph A = 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 (); if (count% 3 = = 0) {bitmap = Mstarone; Srcrect = Mstaronesrcrect; Destrect.set (Xlocation, ylocation, Xlocation + mstaronewidth, ylocation + MS Taroneheight); } 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 (); }
the next thing to consider is how to make the planet move, with the above data and ideas, I believe you let the planet move is not difficult, just according to the direction of the planet movement, each time the redrawing of the planet's X, y increase or decrease the corresponding size can be:
private void Resetstarfloat (StarInfo StarInfo) { switch (starinfo.direction) {case left : Starinfo.xlocation-= starinfo.speed; break; Case right: starinfo.xlocation + = starinfo.speed; break; Case TOP: starinfo.ylocation-= starinfo.speed; break; Case BOTTOM: starinfo.ylocation + = starinfo.speed; break; Default: Break ; } }
At this time some of the students may say, a little, the planet directly moved out of the screen how to do, this problem believe that we can solve, just a value of judgment and re-repair, no longer speak;
Finally, for this kind of dynamic effect small talk about, in fact, a large part of the effect and the above is similar to the dynamic effect, do not believe? I'll give you a few chestnuts:
1. Snow Falling effect
It's so lame! Snowflakes like this? Snowflakes fly down from the top, and also rotate;
For this, we only need to draw the object when the rotation angle and rotation speed (for rotation problems, you can refer to a brilliant loading dynamic analysis and implementation!) As for the problem of flying from the top down, we only need to modify the X, y update strategy;
2. Many desktop applications of the petals falling, lost its blossom effect;
Basic is the above analysis and implementation of the principle, it is important for data extraction and flexible use, and other is based on the specific needs of the dynamic update needs to update the data, such as location, size, transparency and so on;
Therefore, from the above view, this kind of effect is not complex, we need to do is to decompose the complex dynamic effect, extraction, and then find every small point of the most suitable implementation mode, large dynamic effect small, and then one by one break;
I write the process will be as far as possible to describe the ideas clearly, because in my opinion, the most important thing is the effect of dismantling, cohesion, the solution of the idea, the idea is clear, the solution is generally clear;
The analysis and implementation of the dynamic effect of Android floating class!