[Android]搜索关键字飞入飞出效果

来源:互联网 发布:软件销售团队 编辑:程序博客网 时间:2024/04/29 19:23

好久没发东西了,快三个月了。忙,无他尔。接下来进入正题。

好多应用在搜索界面都有关键字飞入飞出的效果。我自己也实现了下。先上效果图:



实现该效果需要解决以下五点:

1.布局的选用。
2.确定动画区域,即布局的宽高。
3.对关键字坐标的随机分配。
4.对随机分配的坐标进行向中心靠拢。
5.动画的实现。


下面各个击破:
1.布局的选用。
    在五种常用布局中,可实现此效果的有AbsoluteLayout、FrameLayout、RelativeLayout三种。一开始我选用的AbsoluteLayout,运行结果出来后,发现AbsoluteLayout下的TextView一旦超出其显示范围,超出的范围将无法显示,而余下的两种布局,其超出的范围会自动换行显示出来(TextView长度超出父组件显示范围可在代码中避免,此处仅是举例,说明AbsoluteLayout的先天不足)。另,官方已不再推荐使用AbsoluteLayout,所以本处凭个人喜好我选用FrameLayout。
    
    FrameLayout如何实现AbsoluteLayout对其子组件进行定点放置呢?答案在FrameLayout.LayoutParams上。该类有相关属性为leftMargin及topMargin。要将子组件左上角定点放置在其父组件中的(x,y)处,仅需对leftMargin赋值为x,对topMargin赋值为y即可。
    
2.确定动画区域,即布局的宽高。
    在对显示关键字TextView进行分配坐标之前,应该要先知道父组件的宽高各有多少可供随机分配。
    获取宽高使用到OnGlobalLayoutListener。本例中KeywordsFlow继承自FrameLayout,同时也实现了OnGlobalLayoutListener接口,在其初始化方法init()中设置了监听getViewTreeObserver().addOnGlobalLayoutListener(this);
    当监听事件被触发时,即可获取而已的宽高。

[java] view plaincopy
  1. public void onGlobalLayout() {  
  2.     int tmpW = getWidth();  
  3.     int tmpH = getHeight();  
  4.     if (width != tmpW || height != tmpH) {  
  5.         width = tmpW;  
  6.         height = tmpH;  
  7.         show();  
  8.     }  
  9. }  


    
3.对关键字坐标的随机分配。
    TextView坐标的随机是否到位分配决定着整体效果的好坏。
    本例设定关键字最多为10个,在布局的X Y轴上各自进行10等分。每个关键字依照其添加顺序随机各自在X轴和Y轴上选择等分后的10点中的某个点为margin的值。此值为糙值,需要对X轴进行越界修正,对Y轴进行向中心靠拢修正。对X轴坐标的修正为如下:
[java] view plaincopy
  1.     // 获取文本长度  
  2. Paint paint = txt.getPaint();  
  3. int strWidth = (int) Math.ceil(paint.measureText(keyword));  
  4. xy[IDX_TXT_LENGTH] = strWidth;  
  5. // 第一次修正:修正x坐标  
  6. if (xy[IDX_X] + strWidth > width - (xItem >> 1)) {  
  7.     int baseX = width - strWidth;  
  8.     // 减少文本右边缘一样的概率  
  9.     xy[IDX_X] = baseX - xItem + random.nextInt(xItem >> 1);  
  10. else if (xy[IDX_X] == 0) {  
  11.     // 减少文本左边缘一样的概率  
  12.     xy[IDX_X] = Math.max(random.nextInt(xItem), xItem / 3);  
  13. }  


4.对随机分配的坐标进行向中心靠拢。
    此操作将修正Y轴坐标。
    由于随机分配中,可能出现某个关键字在朝中心点方向上的空间中再没有其它关键字了,此时该关键字在Y轴上应该朝中心点靠拢。实现代码如下:
[java] view plaincopy
  1.     // 第二次修正:修正y坐标  
  2. int yDistance = iXY[IDX_Y] - yCenter;  
  3. // 对于最靠近中心点的,其值不会大于yItem<br/>  
  4. // 对于可以一路下降到中心点的,则该值也是其应调整的大小<br/>  
  5. int yMove = Math.abs(yDistance);  
  6. inner: for (int k = i - 1; k >= 0; k--) {  
  7.     int[] kXY = (int[]) listTxt.get(k).getTag();  
  8.     int startX = kXY[IDX_X];  
  9.     int endX = startX + kXY[IDX_TXT_LENGTH];  
  10.     // y轴以中心点为分隔线,在同一侧  
  11.     if (yDistance * (kXY[IDX_Y] - yCenter) > 0) {  
  12.         // Log.d("ANDROID_LAB", "compare:" +  
  13.         // listTxt.get(k).getText());  
  14.         if (isXMixed(startX, endX, iXY[IDX_X], iXY[IDX_X] + iXY[IDX_TXT_LENGTH])) {  
  15.             int tmpMove = Math.abs(iXY[IDX_Y] - kXY[IDX_Y]);  
  16.             if (tmpMove > yItem) {  
  17.                 yMove = tmpMove;  
  18.             } else if (yMove > 0) {  
  19.                 // 取消默认值。  
  20.                 yMove = 0;  
  21.             }  
  22.             // Log.d("ANDROID_LAB", "break");  
  23.             break inner;  
  24.         }  
  25.     }  
  26. }  
  27. // Log.d("ANDROID_LAB", txt.getText() + " yMove=" + yMove);  
  28. if (yMove > yItem) {  
  29.     int maxMove = yMove - yItem;  
  30.     int randomMove = random.nextInt(maxMove);  
  31.     int realMove = Math.max(randomMove, maxMove >> 1) * yDistance / Math.abs(yDistance);  
  32.     iXY[IDX_Y] = iXY[IDX_Y] - realMove;  
  33.     iXY[IDX_DIS_Y] = Math.abs(iXY[IDX_Y] - yCenter);  
  34.     // 已经调整过前i个需要再次排序  
  35.     sortXYList(listTxt, i + 1);  
  36. }  


            
            
5.动画的实现。
    每个TextView的动画都有包括三部分:伸缩动画ScaleAnimation、透明度渐变动画AlphaAnimation及位移动画TranslateAnimation。以上三个动画中除了位移动画是独立的,其它两种动画都是可以共用的。三种动画的组合使用AnimationSet拼装在一起同时作用在TextView上。动画的实现如下:
[java] view plaincopy
  1. public AnimationSet getAnimationSet(int[] xy, int xCenter, int yCenter, int type) {  
  2.     AnimationSet animSet = new AnimationSet(true);  
  3.     animSet.setInterpolator(interpolator);  
  4.     if (type == OUTSIDE_TO_LOCATION) {  
  5.         animSet.addAnimation(animAlpha2Opaque);  
  6.         animSet.addAnimation(animScaleLarge2Normal);  
  7.         TranslateAnimation translate = new TranslateAnimation(  
  8.                 (xy[IDX_X] + (xy[IDX_TXT_LENGTH] >> 1) - xCenter) << 10, (xy[IDX_Y] - yCenter) << 10);  
  9.         animSet.addAnimation(translate);  
  10.     } else if (type == LOCATION_TO_OUTSIDE) {  
  11.         animSet.addAnimation(animAlpha2Transparent);  
  12.         animSet.addAnimation(animScaleNormal2Large);  
  13.         TranslateAnimation translate = new TranslateAnimation(0,  
  14.                 (xy[IDX_X] + (xy[IDX_TXT_LENGTH] >> 1) - xCenter) << 10, (xy[IDX_Y] - yCenter) << 1);  
  15.         animSet.addAnimation(translate);  
  16.     } else if (type == LOCATION_TO_CENTER) {  
  17.         animSet.addAnimation(animAlpha2Transparent);  
  18.         animSet.addAnimation(animScaleNormal2Zero);  
  19.         TranslateAnimation translate = new TranslateAnimation(0, (-xy[IDX_X] + xCenter), 0, (-xy[IDX_Y] + yCenter));  
  20.         animSet.addAnimation(translate);  
  21.     } else if (type == CENTER_TO_LOCATION) {  
  22.         animSet.addAnimation(animAlpha2Opaque);  
  23.         animSet.addAnimation(animScaleZero2Normal);  
  24.         TranslateAnimation translate = new TranslateAnimation((-xy[IDX_X] + xCenter), 0, (-xy[IDX_Y] + yCenter), 0);  
  25.         animSet.addAnimation(translate);  
  26.     }  
  27.     animSet.setDuration(animDuration);  
  28.     return animSet;  
  29. }  


    
    最后有个小点需要再次提醒下,使用KeywordsFlow时,在Eclipse开发环境下导出混淆包时,需要在proguard.cfg中添加:-keep public class * extends android.widget.FrameLayout
    否则将会提示无法找到该类。
    好了,文嗦嗦的东西到此结束,贴上Java代码如下,xml代码请根据效果图自己鼓捣吧。
[java] view plaincopy
  1. ActKeywordAnim.java  
  2.   
  3. package lab.sodino.searchkeywordanim;  
  4.   
  5. import java.util.Random;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.Intent;  
  9. import android.net.Uri;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15.   
  16. /** 
  17.  * @author Sodino E-mail:sodinoopen@hotmail.com 
  18.  * @version Time:2011-12-26 下午03:34:16 
  19.  */  
  20. public class ActKeywordAnim extends Activity implements OnClickListener {  
  21.     public static final String[] keywords = { "QQ""Sodino""APK""GFW""铅笔",//  
  22.             "短信""桌面精灵""MacBook Pro""平板电脑""雅诗兰黛",//  
  23.             "卡西欧 TR-100""笔记本""SPY Mouse""Thinkpad E40""捕鱼达人",//  
  24.             "内存清理""地图""导航""闹钟""主题",//  
  25.             "通讯录""播放器""CSDN leak""安全""3D",//  
  26.             "美女""天气""4743G""戴尔""联想",//  
  27.             "欧朋""浏览器""愤怒的小鸟""mmShow""网易公开课",//  
  28.             "iciba""油水关系""网游App""互联网""365日历",//  
  29.             "脸部识别""Chrome""Safari""中国版Siri""A5处理器",//  
  30.             "iPhone4S""摩托 ME525""魅族 M9""尼康 S2500" };  
  31.     private KeywordsFlow keywordsFlow;  
  32.     private Button btnIn, btnOut;  
  33.   
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.         btnIn = (Button) findViewById(R.id.btnIn);  
  38.         btnOut = (Button) findViewById(R.id.btnOut);  
  39.         btnIn.setOnClickListener(this);  
  40.         btnOut.setOnClickListener(this);  
  41.         keywordsFlow = (KeywordsFlow) findViewById(R.id.keywordsFlow);  
  42.         keywordsFlow.setDuration(800l);  
  43.         keywordsFlow.setOnItemClickListener(this);  
  44.         // 添加  
  45.         feedKeywordsFlow(keywordsFlow, keywords);  
  46.         keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);  
  47.     }  
  48.   
  49.     private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {  
  50.         Random random = new Random();  
  51.         for (int i = 0; i < KeywordsFlow.MAX; i++) {  
  52.             int ran = random.nextInt(arr.length);  
  53.             String tmp = arr[ran];  
  54.             keywordsFlow.feedKeyword(tmp);  
  55.         }  
  56.     }  
  57.   
  58.     @Override  
  59.     public void onClick(View v) {  
  60.         if (v == btnIn) {  
  61.             keywordsFlow.rubKeywords();  
  62.             // keywordsFlow.rubAllViews();  
  63.             feedKeywordsFlow(keywordsFlow, keywords);  
  64.             keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);  
  65.         } else if (v == btnOut) {  
  66.             keywordsFlow.rubKeywords();  
  67.             // keywordsFlow.rubAllViews();  
  68.             feedKeywordsFlow(keywordsFlow, keywords);  
  69.             keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);  
  70.         } else if (v instanceof TextView) {  
  71.             String keyword = ((TextView) v).getText().toString();  
  72.             Intent intent = new Intent();  
  73.             intent.setAction(Intent.ACTION_VIEW);  
  74.             intent.addCategory(Intent.CATEGORY_DEFAULT);  
  75.             intent.setData(Uri.parse("http://www.google.com.hk/#q=" + keyword));  
  76.             startActivity(intent);  
  77.         }  
  78.     }  
  79. }  

[java] view plaincopy
  1. KeywordsFlow.java  
  2.   
  3. package lab.sodino.searchkeywordanim;  
  4.   
  5. import java.util.LinkedList;  
  6. import java.util.Random;  
  7. import java.util.Vector;  
  8.   
  9. import android.content.Context;  
  10. import android.graphics.Paint;  
  11. import android.util.AttributeSet;  
  12. import android.util.TypedValue;  
  13. import android.view.Gravity;  
  14. import android.view.View;  
  15. import android.view.ViewTreeObserver.OnGlobalLayoutListener;  
  16. import android.view.animation.AlphaAnimation;  
  17. import android.view.animation.Animation;  
  18. import android.view.animation.Animation.AnimationListener;  
  19. import android.view.animation.AnimationSet;  
  20. import android.view.animation.AnimationUtils;  
  21. import android.view.animation.Interpolator;  
  22. import android.view.animation.ScaleAnimation;  
  23. import android.view.animation.TranslateAnimation;  
  24. import android.widget.FrameLayout;  
  25. import android.widget.TextView;  
  26.   
  27. /** 
  28.  * 注意,出包时出混淆包,应在proguard.cfg中加入:<br/> 
  29.  * -keep public class * extends android.widget.FrameLayout<br/> 
  30.  *  
  31.  * @author Sodino E-mail:sodinoopen@hotmail.com 
  32.  * @version Time:2011-12-26 下午03:34:16 
  33.  */  
  34. public class KeywordsFlow extends FrameLayout implements OnGlobalLayoutListener {  
  35.     public static final int IDX_X = 0;  
  36.     public static final int IDX_Y = 1;  
  37.     public static final int IDX_TXT_LENGTH = 2;  
  38.     public static final int IDX_DIS_Y = 3;  
  39.     /** 由外至内的动画。 */  
  40.     public static final int ANIMATION_IN = 1;  
  41.     /** 由内至外的动画。 */  
  42.     public static final int ANIMATION_OUT = 2;  
  43.     /** 位移动画类型:从外围移动到坐标点。 */  
  44.     public static final int OUTSIDE_TO_LOCATION = 1;  
  45.     /** 位移动画类型:从坐标点移动到外围。 */  
  46.     public static final int LOCATION_TO_OUTSIDE = 2;  
  47.     /** 位移动画类型:从中心点移动到坐标点。 */  
  48.     public static final int CENTER_TO_LOCATION = 3;  
  49.     /** 位移动画类型:从坐标点移动到中心点。 */  
  50.     public static final int LOCATION_TO_CENTER = 4;  
  51.     public static final long ANIM_DURATION = 800l;  
  52.     public static final int MAX = 10;  
  53.     public static final int TEXT_SIZE_MAX = 25;  
  54.     public static final int TEXT_SIZE_MIN = 15;  
  55.     private OnClickListener itemClickListener;  
  56.     private static Interpolator interpolator;  
  57.     private static AlphaAnimation animAlpha2Opaque;  
  58.     private static AlphaAnimation animAlpha2Transparent;  
  59.     private static ScaleAnimation animScaleLarge2Normal, animScaleNormal2Large, animScaleZero2Normal,  
  60.             animScaleNormal2Zero;  
  61.     /** 存储显示的关键字。 */  
  62.     private Vector<String> vecKeywords;  
  63.     private int width, height;  
  64.     /** 
  65.      * go2Show()中被赋值为true,标识开发人员触发其开始动画显示。<br/> 
  66.      * 本标识的作用是防止在填充keywrods未完成的过程中获取到width和height后提前启动动画。<br/> 
  67.      * 在show()方法中其被赋值为false。<br/> 
  68.      * 真正能够动画显示的另一必要条件:width 和 height不为0。<br/> 
  69.      */  
  70.     private boolean enableShow;  
  71.     private Random random;  
  72.     /** 
  73.      * @see ANIMATION_IN 
  74.      * @see ANIMATION_OUT 
  75.      * @see OUTSIDE_TO_LOCATION 
  76.      * @see LOCATION_TO_OUTSIDE 
  77.      * @see LOCATION_TO_CENTER 
  78.      * @see CENTER_TO_LOCATION 
  79.      * */  
  80.     private int txtAnimInType, txtAnimOutType;  
  81.     /** 最近一次启动动画显示的时间。 */  
  82.     private long lastStartAnimationTime;  
  83.     /** 动画运行时间。 */  
  84.     private long animDuration;  
  85.   
  86.     public KeywordsFlow(Context context, AttributeSet attrs, int defStyle) {  
  87.         super(context, attrs, defStyle);  
  88.         init();  
  89.     }  
  90.   
  91.     public KeywordsFlow(Context context, AttributeSet attrs) {  
  92.         super(context, attrs);  
  93.         init();  
  94.     }  
  95.   
  96.     public KeywordsFlow(Context context) {  
  97.         super(context);  
  98.         init();  
  99.     }  
  100.   
  101.     private void init() {  
  102.         lastStartAnimationTime = 0l;  
  103.         animDuration = ANIM_DURATION;  
  104.         random = new Random();  
  105.         vecKeywords = new Vector<String>(MAX);  
  106.         getViewTreeObserver().addOnGlobalLayoutListener(this);  
  107.         interpolator = AnimationUtils.loadInterpolator(getContext(), android.R.anim.decelerate_interpolator);  
  108.         animAlpha2Opaque = new AlphaAnimation(0.0f, 1.0f);  
  109.         animAlpha2Transparent = new AlphaAnimation(1.0f, 0.0f);  
  110.         animScaleLarge2Normal = new ScaleAnimation(2121);  
  111.         animScaleNormal2Large = new ScaleAnimation(1212);  
  112.         animScaleZero2Normal = new ScaleAnimation(0101);  
  113.         animScaleNormal2Zero = new ScaleAnimation(1010);  
  114.     }  
  115.   
  116.     public long getDuration() {  
  117.         return animDuration;  
  118.     }  
  119.   
  120.     public void setDuration(long duration) {  
  121.         animDuration = duration;  
  122.     }  
  123.   
  124.     public boolean feedKeyword(String keyword) {  
  125.         boolean result = false;  
  126.         if (vecKeywords.size() < MAX) {  
  127.             result = vecKeywords.add(keyword);  
  128.         }  
  129.         return result;  
  130.     }  
  131.   
  132.     /** 
  133.      * 开始动画显示。<br/> 
  134.      * 之前已经存在的TextView将会显示退出动画。<br/> 
  135.      *  
  136.      * @return 正常显示动画返回true;反之为false。返回false原因如下:<br/> 
  137.      *         1.时间上不允许,受lastStartAnimationTime的制约;<br/> 
  138.      *         2.未获取到width和height的值。<br/> 
  139.      */  
  140.     public boolean go2Show(int animType) {  
  141.         if (System.currentTimeMillis() - lastStartAnimationTime > animDuration) {  
  142.             enableShow = true;  
  143.             if (animType == ANIMATION_IN) {  
  144.                 txtAnimInType = OUTSIDE_TO_LOCATION;  
  145.                 txtAnimOutType = LOCATION_TO_CENTER;  
  146.             } else if (animType == ANIMATION_OUT) {  
  147.                 txtAnimInType = CENTER_TO_LOCATION;  
  148.                 txtAnimOutType = LOCATION_TO_OUTSIDE;  
  149.             }  
  150.             disapper();  
  151.             boolean result = show();  
  152.             return result;  
  153.         }  
  154.         return false;  
  155.     }  
  156.   
  157.     private void disapper() {  
  158.         int size = getChildCount();  
  159.         for (int i = size - 1; i >= 0; i--) {  
  160.             final TextView txt = (TextView) getChildAt(i);  
  161.             if (txt.getVisibility() == View.GONE) {  
  162.                 removeView(txt);  
  163.                 continue;  
  164.             }  
  165.             FrameLayout.LayoutParams layParams = (LayoutParams) txt.getLayoutParams();  
  166.             // Log.d("ANDROID_LAB", txt.getText() + " leftM=" +  
  167.             // layParams.leftMargin + " topM=" + layParams.topMargin  
  168.             // + " width=" + txt.getWidth());  
  169.             int[] xy = new int[] { layParams.leftMargin, layParams.topMargin, txt.getWidth() };  
  170.             AnimationSet animSet = getAnimationSet(xy, (width >> 1), (height >> 1), txtAnimOutType);  
  171.             txt.startAnimation(animSet);  
  172.             animSet.setAnimationListener(new AnimationListener() {  
  173.                 public void onAnimationStart(Animation animation) {  
  174.                 }  
  175.   
  176.                 public void onAnimationRepeat(Animation animation) {  
  177.                 }  
  178.   
  179.                 public void onAnimationEnd(Animation animation) {  
  180.                     txt.setOnClickListener(null);  
  181.                     txt.setClickable(false);  
  182.                     txt.setVisibility(View.GONE);  
  183.                 }  
  184.             });  
  185.         }  
  186.     }  
  187.   
  188.     private boolean show() {  
  189.         if (width > 0 && height > 0 && vecKeywords != null && vecKeywords.size() > 0 && enableShow) {  
  190.             enableShow = false;  
  191.             lastStartAnimationTime = System.currentTimeMillis();  
  192.             int xCenter = width >> 1, yCenter = height >> 1;  
  193.             int size = vecKeywords.size();  
  194.             int xItem = width / size, yItem = height / size;  
  195.             // Log.d("ANDROID_LAB", "--------------------------width=" + width +  
  196.             // " height=" + height + "  xItem=" + xItem  
  197.             // + " yItem=" + yItem + "---------------------------");  
  198.             LinkedList<Integer> listX = new LinkedList<Integer>(), listY = new LinkedList<Integer>();  
  199.             for (int i = 0; i < size; i++) {  
  200.                 // 准备随机候选数,分别对应x/y轴位置  
  201.                 listX.add(i * xItem);  
  202.                 listY.add(i * yItem + (yItem >> 2));  
  203.             }  
  204.             // TextView[] txtArr = new TextView[size];  
  205.             LinkedList<TextView> listTxtTop = new LinkedList<TextView>();  
  206.             LinkedList<TextView> listTxtBottom = new LinkedList<TextView>();  
  207.             for (int i = 0; i < size; i++) {  
  208.                 String keyword = vecKeywords.get(i);  
  209.                 // 随机颜色  
  210.                 int ranColor = 0xff000000 | random.nextInt(0x0077ffff);  
  211.                 // 随机位置,糙值  
  212.                 int xy[] = randomXY(random, listX, listY, xItem);  
  213.                 // 随机字体大小  
  214.                 int txtSize = TEXT_SIZE_MIN + random.nextInt(TEXT_SIZE_MAX - TEXT_SIZE_MIN + 1);  
  215.                 // 实例化TextView  
  216.                 final TextView txt = new TextView(getContext());  
  217.                 txt.setOnClickListener(itemClickListener);  
  218.                 txt.setText(keyword);  
  219.                 txt.setTextColor(ranColor);  
  220.                 txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, txtSize);  
  221.                 txt.setShadowLayer(2220xff696969);  
  222.                 txt.setGravity(Gravity.CENTER);  
  223.                 // 获取文本长度  
  224.                 Paint paint = txt.getPaint();  
  225.                 int strWidth = (int) Math.ceil(paint.measureText(keyword));  
  226.                 xy[IDX_TXT_LENGTH] = strWidth;  
  227.                 // 第一次修正:修正x坐标  
  228.                 if (xy[IDX_X] + strWidth > width - (xItem >> 1)) {  
  229.                     int baseX = width - strWidth;  
  230.                     // 减少文本右边缘一样的概率  
  231.                     xy[IDX_X] = baseX - xItem + random.nextInt(xItem >> 1);  
  232.                 } else if (xy[IDX_X] == 0) {  
  233.                     // 减少文本左边缘一样的概率  
  234.                     xy[IDX_X] = Math.max(random.nextInt(xItem), xItem / 3);  
  235.                 }  
  236.                 xy[IDX_DIS_Y] = Math.abs(xy[IDX_Y] - yCenter);  
  237.                 txt.setTag(xy);  
  238.                 if (xy[IDX_Y] > yCenter) {  
  239.                     listTxtBottom.add(txt);  
  240.                 } else {  
  241.                     listTxtTop.add(txt);  
  242.                 }  
  243.             }  
  244.             attach2Screen(listTxtTop, xCenter, yCenter, yItem);  
  245.             attach2Screen(listTxtBottom, xCenter, yCenter, yItem);  
  246.             return true;  
  247.         }  
  248.         return false;  
  249.     }  
  250.   
  251.     /** 修正TextView的Y坐标将将其添加到容器上。 */  
  252.     private void attach2Screen(LinkedList<TextView> listTxt, int xCenter, int yCenter, int yItem) {  
  253.         int size = listTxt.size();  
  254.         sortXYList(listTxt, size);  
  255.         for (int i = 0; i < size; i++) {  
  256.             TextView txt = listTxt.get(i);  
  257.             int[] iXY = (int[]) txt.getTag();  
  258.             // Log.d("ANDROID_LAB", "fix[  " + txt.getText() + "  ] x:" +  
  259.             // iXY[IDX_X] + " y:" + iXY[IDX_Y] + " r2="  
  260.             // + iXY[IDX_DIS_Y]);  
  261.             // 第二次修正:修正y坐标  
  262.             int yDistance = iXY[IDX_Y] - yCenter;  
  263.             // 对于最靠近中心点的,其值不会大于yItem<br/>  
  264.             // 对于可以一路下降到中心点的,则该值也是其应调整的大小<br/>  
  265.             int yMove = Math.abs(yDistance);  
  266.             inner: for (int k = i - 1; k >= 0; k--) {  
  267.                 int[] kXY = (int[]) listTxt.get(k).getTag();  
  268.                 int startX = kXY[IDX_X];  
  269.                 int endX = startX + kXY[IDX_TXT_LENGTH];  
  270.                 // y轴以中心点为分隔线,在同一侧  
  271.                 if (yDistance * (kXY[IDX_Y] - yCenter) > 0) {  
  272.                     // Log.d("ANDROID_LAB", "compare:" +  
  273.                     // listTxt.get(k).getText());  
  274.                     if (isXMixed(startX, endX, iXY[IDX_X], iXY[IDX_X] + iXY[IDX_TXT_LENGTH])) {  
  275.                         int tmpMove = Math.abs(iXY[IDX_Y] - kXY[IDX_Y]);  
  276.                         if (tmpMove > yItem) {  
  277.                             yMove = tmpMove;  
  278.                         } else if (yMove > 0) {  
  279.                             // 取消默认值。  
  280.                             yMove = 0;  
  281.                         }  
  282.                         // Log.d("ANDROID_LAB", "break");  
  283.                         break inner;  
  284.                     }  
  285.                 }  
  286.             }  
  287.             // Log.d("ANDROID_LAB", txt.getText() + " yMove=" + yMove);  
  288.             if (yMove > yItem) {  
  289.                 int maxMove = yMove - yItem;  
  290.                 int randomMove = random.nextInt(maxMove);  
  291.                 int realMove = Math.max(randomMove, maxMove >> 1) * yDistance / Math.abs(yDistance);  
  292.                 iXY[IDX_Y] = iXY[IDX_Y] - realMove;  
  293.                 iXY[IDX_DIS_Y] = Math.abs(iXY[IDX_Y] - yCenter);  
  294.                 // 已经调整过前i个需要再次排序  
  295.                 sortXYList(listTxt, i + 1);  
  296.             }  
  297.             FrameLayout.LayoutParams layParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,  
  298.                     FrameLayout.LayoutParams.WRAP_CONTENT);  
  299.             layParams.gravity = Gravity.LEFT | Gravity.TOP;  
  300.             layParams.leftMargin = iXY[IDX_X];  
  301.             layParams.topMargin = iXY[IDX_Y];  
  302.             addView(txt, layParams);  
  303.             // 动画  
  304.             AnimationSet animSet = getAnimationSet(iXY, xCenter, yCenter, txtAnimInType);  
  305.             txt.startAnimation(animSet);  
  306.         }  
  307.     }  
  308.   
  309.     public AnimationSet getAnimationSet(int[] xy, int xCenter, int yCenter, int type) {  
  310.         AnimationSet animSet = new AnimationSet(true);  
  311.         animSet.setInterpolator(interpolator);  
  312.         if (type == OUTSIDE_TO_LOCATION) {  
  313.             animSet.addAnimation(animAlpha2Opaque);  
  314.             animSet.addAnimation(animScaleLarge2Normal);  
  315.             TranslateAnimation translate = new TranslateAnimation(  
  316.                     (xy[IDX_X] + (xy[IDX_TXT_LENGTH] >> 1) - xCenter) << 10, (xy[IDX_Y] - yCenter) << 10);  
  317.             animSet.addAnimation(translate);  
  318.         } else if (type == LOCATION_TO_OUTSIDE) {  
  319.             animSet.addAnimation(animAlpha2Transparent);  
  320.             animSet.addAnimation(animScaleNormal2Large);  
  321.             TranslateAnimation translate = new TranslateAnimation(0,  
  322.                     (xy[IDX_X] + (xy[IDX_TXT_LENGTH] >> 1) - xCenter) << 10, (xy[IDX_Y] - yCenter) << 1);  
  323.             animSet.addAnimation(translate);  
  324.         } else if (type == LOCATION_TO_CENTER) {  
  325.             animSet.addAnimation(animAlpha2Transparent);  
  326.             animSet.addAnimation(animScaleNormal2Zero);  
  327.             TranslateAnimation translate = new TranslateAnimation(0, (-xy[IDX_X] + xCenter), 0, (-xy[IDX_Y] + yCenter));  
  328.             animSet.addAnimation(translate);  
  329.         } else if (type == CENTER_TO_LOCATION) {  
  330.             animSet.addAnimation(animAlpha2Opaque);  
  331.             animSet.addAnimation(animScaleZero2Normal);  
  332.             TranslateAnimation translate = new TranslateAnimation((-xy[IDX_X] + xCenter), 0, (-xy[IDX_Y] + yCenter), 0);  
  333.             animSet.addAnimation(translate);  
  334.         }  
  335.         animSet.setDuration(animDuration);  
  336.         return animSet;  
  337.     }  
  338.   
  339.     /** 
  340.      * 根据与中心点的距离由近到远进行冒泡排序。 
  341.      *  
  342.      * @param endIdx 
  343.      *            起始位置。 
  344.      * @param txtArr 
  345.      *            待排序的数组。 
  346.      *  
  347.      */  
  348.     private void sortXYList(LinkedList<TextView> listTxt, int endIdx) {  
  349.         for (int i = 0; i < endIdx; i++) {  
  350.             for (int k = i + 1; k < endIdx; k++) {  
  351.                 if (((int[]) listTxt.get(k).getTag())[IDX_DIS_Y] < ((int[]) listTxt.get(i).getTag())[IDX_DIS_Y]) {  
  352.                     TextView iTmp = listTxt.get(i);  
  353.                     TextView kTmp = listTxt.get(k);  
  354.                     listTxt.set(i, kTmp);  
  355.                     listTxt.set(k, iTmp);  
  356.                 }  
  357.             }  
  358.         }  
  359.     }  
  360.   
  361.     /** A线段与B线段所代表的直线在X轴映射上是否有交集。 */  
  362.     private boolean isXMixed(int startA, int endA, int startB, int endB) {  
  363.         boolean result = false;  
  364.         if (startB >= startA && startB <= endA) {  
  365.             result = true;  
  366.         } else if (endB >= startA && endB <= endA) {  
  367.             result = true;  
  368.         } else if (startA >= startB && startA <= endB) {  
  369.             result = true;  
  370.         } else if (endA >= startB && endA <= endB) {  
  371.             result = true;  
  372.         }  
  373.         return result;  
  374.     }  
  375.   
  376.     private int[] randomXY(Random ran, LinkedList<Integer> listX, LinkedList<Integer> listY, int xItem) {  
  377.         int[] arr = new int[4];  
  378.         arr[IDX_X] = listX.remove(ran.nextInt(listX.size()));  
  379.         arr[IDX_Y] = listY.remove(ran.nextInt(listY.size()));  
  380.         return arr;  
  381.     }  
  382.   
  383.     public void onGlobalLayout() {  
  384.         int tmpW = getWidth();  
  385.         int tmpH = getHeight();  
  386.         if (width != tmpW || height != tmpH) {  
  387.             width = tmpW;  
  388.             height = tmpH;  
  389.             show();  
  390.         }  
  391.     }  
  392.   
  393.     public Vector<String> getKeywords() {  
  394.         return vecKeywords;  
  395.     }  
  396.   
  397.     public void rubKeywords() {  
  398.         vecKeywords.clear();  
  399.     }  
  400.   
  401.     /** 直接清除所有的TextView。在清除之前不会显示动画。 */  
  402.     public void rubAllViews() {  
  403.         removeAllViews();  
  404.     }  
  405.   
  406.     public void setOnItemClickListener(OnClickListener listener) {  
  407.         itemClickListener = listener;  
  408.     }  
  409.   
  410.     // public void onDraw(Canvas canvas) {  
  411.     // super.onDraw(canvas);  
  412.     // Paint p = new Paint();  
  413.     // p.setColor(Color.BLACK);  
  414.     // canvas.drawCircle((width >> 1) - 2, (height >> 1) - 2, 4, p);  
  415.     // p.setColor(Color.RED);  
  416.     // }  
  417. }