Reprint Please specify source : http://blog.csdn.net/forwardyzk/article/details/42493641
We see in an interface, the text can fly out from the inside out, also can fly from the outward, we will study this effect.
Ideas:
1. Set the text that you want to show up to date.
2. Set the animation of the View
(1) Set the current view disappears
If you are flying in, set the current view animation
The gradient animation: opaque to transparent
Stretch animation: Zoom out
pan animation: Translate inward
If it's a fly-out
&nb Sp gradient animation: from opaque to transparent
scaling animation: by magnification
&N Bsp Pan animation: Pan out
(2) show the latest view
If it is a fly-in, set the current animation
Gradient animations: transparent to opaque
Scaling Animation: Zoom in and out normal
Pan: Move inward
If fly out, set current animation
gradient animation: transparent to opaque
Stretch animations: Never show
pan: Move Outward
3. When the animation of the currently displayed view ends, it is removed from the current form.
4. Displays the new view.
4.1. The coordinates of each view are randomly generated and cannot be duplicated.
4.2. Randomly generate the color and size of the display text, between the maximum and the minimum.
4.3. After setting the text, adjust the x-coordinate of the view, the left border and the upper boundary size according to the contents of the text.
Prevents the text in the current view from appearing incomplete or displaying multiple lines
Prevent display on the leftmost side.
4.4. If the position in the y direction is less than the maximum number of view/, reposition
4.5. According to the center position of the y direction, divided into the upper and lower parts of the view, to the central location of the size of the distance to sort. It is then added to the current parent Form view at a time,
and set the corresponding animation for the added view.
Main code:
Sets all the current view disappears.
Main do:
Remove the view that is not visible at the end of the previous animation
Gets the current view's coordinates, left spacing, top spacing, and view amount width. In order to set the motion animation for you, calculate the destination coordinates.
When the animation is finished, so that it does not display, GONE
private void Disapper () {int size = Getchildcount (); for (int i = size-1; I >= 0; i--) {final TextView txt = (TextView ) Getchildat (i); if (txt.getvisibility () = = View.gone) {removeview (TXT); continue;} Framelayout.layoutparams layparams = (layoutparams) txt.getlayoutparams (); int[] xy = new int[] {layparams.leftmargin, LA Yparams.topmargin,txt.getwidth ()}; Animationset Animset = Getanimationset (xy, (width >> 1), (height >> 1), txtanimouttype); Txt.startanimation ( Animset); Animset.setanimationlistener (new Animationlistener () {public void Onanimationstart (Animation Animation) {} public void Onanimationrepeat (Animation Animation) {}public void Onanimationend (Animation Animation) { Txt.setonclicklistener (null); txt.setclickable (false); txt.setvisibility (View.gone);}});}}
Show the latest view
Create a new view, and then randomly calculate its coordinates, non-repeatable
The text color is randomly set, and the coordinates of the Y axis are compared with the Y-direction coordinates in the middle of the screen, which are divided into two parts.
Adjusts the x-axis coordinates of the view based on the text content of the setting.
Private Boolean Show () {if (width > 0 && height > 0 && veckeywords! = null&& Veckeywords.siz E () > 0 && enableshow) {enableshow = False;laststartanimationtime = System.currenttimemillis ();//Find center point int xce nter = width >> 1, ycenter = height >> 1;//number of keywords. int size = Veckeywords.size (); int xitem = width/size, Yitem = height/size; LOG.D ("Android_lab", "--------------------------width=" + width+ "height=" + height + "xitem=" + Xitem + "yitem=" + yIt EM + "---------------------------"); linkedlist<integer> listx = new linkedlist<integer> (), listy = new linkedlist<integer> (); for (int i = 0; i < size; i++) {//Prepare random candidate numbers corresponding to X/Y axis position listx.add (i * xitem); Listy.add (i * yitem + (Yitem >> 2)); LOG.E ("Search", "LISTX:" + (i * xitem) + "#listY:" + (I * yitem + (Yitem >> 2));} textview[] Txtarr = new Textview[size]; linkedlist<textview> listtxttop = new linkedlist<textview> (); Linkedlist<textview> LisTtxtbottom = new linkedlist<textview> (); for (int i = 0; i < size; i++) {String keyword = veckeywords.get (i);//random color int rancolor = 0xff000000 | Random.nextint (0X0077FFFF);//random position, rough value int xy[] = Randomxy (random, Listx, listy, xitem);//random font size int txtsize = Text_size_mi n+ Random.nextint (text_size_max-text_size_min + 1);//Instantiate textviewfinal TextView txt = new TextView (GetContext ()); txt.se Tonclicklistener (Itemclicklistener); txt.settext (keyword); Txt.settextcolor (Rancolor); Txt.settextsize ( TYPEDVALUE.COMPLEX_UNIT_SP, txtsize); Txt.setshadowlayer (1, 1, 1, 0xdd696969); txt.setgravity (Gravity.CENTER);// Txt.setbackgroundcolor (color.red);//Gets the text length of paint paint = txt.getpaint (); int strwidth = (int) Math.ceil ( Paint.measuretext (keyword)); Xy[idx_txt_length] = strwidth;//First FIX: Fixed X-coordinate if (xy[idx_x] + strwidth > Width-(Xitem > > 1) {int BaseX = width-strwidth;//reduce the probability of the right edge of text xy[idx_x] = Basex-xitem + random.nextint (xitem >> 1);} else if (xy[idx_x] = = 0) {//reduce the same probability as the left edge of the text xy[idx_x] = MAth.max (Random.nextint (Xitem), XITEM/3);} Xy[idx_dis_y] = Math.Abs (xy[idx_y]-ycenter), Txt.settag (XY), if (xy[idx_y] > Ycenter) {listtxtbottom.add (TXT);} else {listtxttop.add (TXT);}} Attach2screen (Listtxttop, Xcenter, Ycenter, Yitem); Attach2screen (Listtxtbottom, Xcenter, YCenter, YItem); return true;} return false;}
After comparison, adjust the view axis coordinates to prevent the view's y-coordinate and the ycenter difference, the view minimum height. The movement effect is not obvious.
The upper and lower view cases are sorted by the distance size of the center location. It is then added to the current interface, with animation effects.
private void Attach2screen (linkedlist<textview> listtxt, int xcenter,int ycenter, int yitem) {int size = Listtxt.siz E (); Sortxylist (listtxt, size); for (int i = 0; i < size; i++) {TextView txt = listtxt.get (i); int[] IXY = (int[]) txt.get Tag ();//LOG.D ("Android_lab", "fix[" + txt.gettext () + "] x:" +//ixy[idx_x] + "y:" + ixy[idx_y] + "r2="//+ IXY[IDX _dis_y]);//second correction: fixed Y-coordinate int ydistance = ixy[idx_y]-ycenter;//for the closest to the center point, the value will not be greater than yitem<br/>//for can descend to the center point, The value is also its size <br/>int Ymove = Math.Abs (ydistance), inner:for (int k = i-1; k >= 0; k--) {int[] KXY = (int[]) Lis Ttxt.get (k). Gettag (), int startX = Kxy[idx_x];int EndX = StartX + kxy[idx_txt_length];//y-axis with center point as divider, on same side if (Ydistance * (KX Y[idx_y]-Ycenter) > 0) {//LOG.D ("Android_lab", "Compare:" +//listtxt.get (k). GetText ()); if (Isxmixed (StartX, EndX, I Xy[idx_x], ixy[idx_x]+ ixy[idx_txt_length])) {int tmpmove = Math.Abs (ixy[idx_y]-kxy[idx_y]); if (Tmpmove > YItem) {yMo ve = Tmpmove;} else if (Ymove > 0) {//cancel default value. Ymove = 0;} LOG.D ("Android_lab", "break"); break Inner;}}} LOG.D ("Android_lab", Txt.gettext () + "ymove=" + ymove); if (Ymove > Yitem) {int maxmove = Ymove-yitem;int Randommo ve = Random.nextint (maxmove); int realmove = Math.max (Randommove, Maxmove >> 1) * Ydistance/math.abs (ydistance); IXY [Idx_y] = ixy[idx_y]-realmove;ixy[idx_dis_y] = Math.Abs (ixy[idx_y]-ycenter);//has been adjusted before I need to sort again sortxylist (listtxt, i + 1 );} Framelayout.layoutparams layparams = new Framelayout.layoutparams (FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); layparams.gravity = Gravity.left | Gravity.top;layparams.leftmargin = Ixy[idx_x];layparams.topmargin = Ixy[idx_y];addview (txt, layParams);// Animation Animationset Animset = Getanimationset (IXY, Xcenter, Ycenter,txtanimintype); Txt.startanimation (AnimSet);}}
There are several key ways to do this:
Feedkeyword (String keyword): Fills the displayed text content
Rubkeywords (): Removes the text content displayed by the current interface
go2show (int animtype): starts to show the interface content and is accompanied by animations.
Animation_in: Fly-in effect animation_out: fly-out effect
Steps to use:
Find the full name of the automatic control (including the package name), my location is: Com.example.inoutanimationdemo.KeywordsFlow
Activity_main.xml
<linearlayout 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 "android:backg Round= "@android: Color/white" android:orientation= "vertical" tools:context= " Com.example.inoutanimationdemo.MainActivity "> <com.example.inoutanimationdemo.keywordsflow android:id=" @+ Id/fly_view "android:layout_width=" match_parent "android:layout_height=" 0DP "android:layout_weight=" 1 "> </com.example.inoutanimationdemo.KeywordsFlow> <linearlayout android:layout_width=" Match_pare NT "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <button Android:id= "@+id/in" android:layout_width= "0DP" android:layout_height= "Wrap_content" Android oid:layout_weight= "1" android:text= "in"/> <button anDroid:id= "@+id/out" android:layout_width= "0DP" android:layout_height= "Wrap_content" Andro id:layout_weight= "1" android:text= "Out"/> </LinearLayout></LinearLayout>
Mainactivity.java
public class Mainactivity extends Activity implements Onclicklistener {public static final string[] keywords = {"Phone", "QQ "," Animation "," APK "," GFW "," pencil ",//" SMS "," Desktop Genie "," MacBook Pro "," tablet "," Estee Lauder ",//" Base "," Notebook "," SPY Mouse "," Thinkpad E4 0 "," Fishing talent ",//" Memory cleanup "," Maps "," navigation "," alarms "," themes ",//" Contacts "," Players "," CSDN leak "," Security "," Animation ",//" Beauty "," Weather "," 4743G "," Dell "," United Think ",//" Open gate "," browser "," Angry Birds "," mmshow "," NetEase Open Class ",//" Iciba "," oil-water relationship "," online game app "," Internet "," 365 Calendar ",//" Face Recognition "," Chrome "," Safari "," China Version Siri "," Apple ",//" iphone5s "," Moto ME525 "," Meizu MX3 "," Xiaomi "};p rivate keywordsflow keywordsflow;private Button btnin, btnout; @Ov errideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initview ();} public void Initview () {btnin = (button) Findviewbyid (r.id.in); btnout = (Button) Findviewbyid (r.id.out); Btnin.setonclicklistener (This), Btnout.setonclicklistener (this), Keywordsflow = (keywordsflow) Findviewbyid ( R.id.fly_view); Keywordsflow.setduratiOn (1000l); Keywordsflow.setonitemclicklistener (this);//Add Feedkeywordsflow (keywordsflow, keywords); Keywordsflow.go2show (keywordsflow.animation_in);//Start animation} @Overridepublic void OnClick (View v) {switch (V.getid ()) { Case R.id.in:keywordsflow.rubkeywords ();//Remove the currently displayed text content Feedkeywordsflow (keywordsflow, keywords);// Add the displayed text to the View keywordsflow.go2show (keywordsflow.animation_in);//Turn on animation break;case R.id.out:keywordsflow.rubkeywords (); Feedkeywordsflow (keywordsflow, keywords); keywordsflow.go2show (keywordsflow.animation_out); Break;case R.id.fly _view:toast.maketext (Getapplicationcontext (), "clicked View", 0). Show (); break;default:break;}} /** * @param keywordsflow * @param arr * filled text */private static void Feedkeywordsflow (Keywordsflow keywordsflow , string[] arr) {Random random = new random (); for (int i = 0; i < Keywordsflow.max; i++) {int ran = Random.nextint (arr). length); String tmp = Arr[ran];keywordsflow.feedkeyword (TMP);}}
Source Download : http://download.csdn.net/detail/forwardyzk/8337201
:
Text fly-in and fly-out