转载请注明出处王亟亟的大牛之路

一直对Animation属于可有可无的不在意。看到个样例,认为在不切换的情况下,适当的应用还真是蛮好看的。

包结构:



一个类一个控件。内容简单。

执行效果:



下方的下方箭头会上下移动,页面内的全部散乱的文字都是TextView,以及搜索和带删除的EditText

主Activity

  1. public class MainActivity extends Activity implements OnClickListener {
  2. private static final int FEEDKEY_START = 1;
  3. private ImageView back_arrow;
  4. private ImageView search_button;
  5. private Animation shakeAnim;
  6. private DeletableEditText searchEdit;
  7. private KeywordsFlow keywordsFlow;
  8. private int STATE = 1;
  9. //页面滚动的数据源
  10. private static String[] keywords = new String[] { "弗拉基米尔", "希维尔", "蒙多",
  11. "茂凯", "潘森", "波比", "拉克丝", "索拉卡", "娑娜", "伊泽瑞尔", "费德提克", " 雷克顿",
  12. "古拉加斯", "卡萨丁", "迦娜", "奥莉安娜", "嘉文四世", " 莫德凯撒", " 崔丝塔娜", "布兰德",
  13. "卡尔玛", "塔里克", "莫甘娜", "凯南", " 兰博", "斯维因" ,"卡尔萨斯"};
  14. private Handler handler = new Handler() {
  15. public void handleMessage(android.os.Message msg) {
  16. switch (msg.what) {
  17. case FEEDKEY_START:
  18. keywordsFlow.rubKeywords();
  19. feedKeywordsFlow(keywordsFlow, keywords);
  20. keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
  21. sendEmptyMessageDelayed(FEEDKEY_START, 5000);
  22. break;
  23. }
  24. };
  25. };
  26. private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
  27. Random random = new Random();
  28. for (int i = 0; i < KeywordsFlow.MAX; i++) {
  29. int ran = random.nextInt(arr.length);
  30. String tmp = arr[ran];
  31. keywordsFlow.feedKeyword(tmp);
  32. }
  33. }
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_search);
  38. //载入动画效果
  39. shakeAnim = AnimationUtils.loadAnimation(this, R.anim.shake_y);
  40. initView();
  41. }
  42. private void initView() {
  43. keywordsFlow = (KeywordsFlow) findViewById(R.id.keywordsflow);
  44. keywordsFlow.setDuration(1000l);
  45. keywordsFlow.setOnItemClickListener(this);
  46. back_arrow = (ImageView) findViewById(R.id.back_arrow);
  47. back_arrow.setAnimation(shakeAnim);
  48. searchEdit = (DeletableEditText) findViewById(R.id.search_view);
  49. search_button=(ImageView)findViewById(R.id.search_button);
  50. search_button.setOnClickListener(this);
  51. feedKeywordsFlow(keywordsFlow, keywords);
  52. keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
  53. handler.sendEmptyMessageDelayed(FEEDKEY_START, 5000);
  54. }
  55. @Override
  56. public void onClick(View v) {
  57. if (v instanceof TextView) {
  58. String keyword = ((TextView) v).getText().toString().trim();
  59. searchEdit.setText(keyword);
  60. searchEdit.setSelection(keyword.length());
  61. }
  62. if(v.getId()==R.id.search_button&&searchEdit.getText().toString().length()!=0){
  63. Toast.makeText(MainActivity.this, "你搜索了 "+searchEdit.getText().toString(), Toast.LENGTH_SHORT).show();
  64. }
  65. }
  66. @Override
  67. public void onBackPressed()
  68. {
  69. Intent intent = new Intent(Intent.ACTION_MAIN);
  70. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  71. intent.addCategory(Intent.CATEGORY_HOME);
  72. startActivity(intent);
  73. }
  74. @Override
  75. protected void onDestroy() {
  76. super.onDestroy();
  77. back_arrow.clearAnimation();
  78. handler.removeMessages(FEEDKEY_START);
  79. STATE = 0;
  80. }
  81. @Override
  82. protected void onStop() {
  83. super.onStop();
  84. handler.removeMessages(FEEDKEY_START);
  85. STATE = 0;
  86. }
  87. @Override
  88. public void onPause() {
  89. super.onPause();
  90. handler.removeMessages(FEEDKEY_START);
  91. STATE = 0;
  92. }
  93. @Override
  94. public void onResume() {
  95. super.onResume();
  96. if (STATE == 0) {
  97. keywordsFlow.rubKeywords();
  98. handler.sendEmptyMessageDelayed(FEEDKEY_START, 3000);
  99. }
  100. }
  101. }

分析:

用handle控制每次动画出现的时间。以及效果排列的算法。

提交按钮之所以不用Button由于 instanceof TextView的时候把Button也算进去了,无法做出分辨(Button 继承于TextView)

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?
  2. >
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/bg_search" >
  7. <LinearLayout
  8. android:id="@+id/search_layout"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentLeft="true"
  12. android:layout_alignParentTop="true"
  13. android:layout_marginLeft="25dp"
  14. android:layout_marginRight="25dp"
  15. android:layout_marginTop="30dp"
  16. android:orientation="vertical" >
  17. <com.wjj.demo.DeletableEditText
  18. android:id="@+id/search_view"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:background="@drawable/bg_edittext"
  22. android:drawableLeft="@drawable/ic_edittext_search"
  23. android:drawablePadding="6dp"
  24. android:drawableRight="@drawable/ic_edittext_delete"
  25. android:hint="请输入您要搜索的内容..."
  26. android:paddingBottom="7dp"
  27. android:paddingLeft="9dp"
  28. android:paddingRight="8dp"
  29. android:paddingTop="7dp"
  30. android:singleLine="true"
  31. android:textColor="#ffffff"
  32. android:textCursorDrawable="@null"
  33. android:textSize="16dp" />
  34. <LinearLayout
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_marginTop="3dp"
  38. android:orientation="horizontal" >
  39. <ImageView
  40. android:id="@+id/search_button"
  41. android:layout_width="80dp"
  42. android:layout_height="40dp"
  43. android:src="@drawable/ic_button_search"/>
  44. </LinearLayout>
  45. </LinearLayout>
  46. <com.wjj.demo.KeywordsFlow
  47. android:id="@+id/keywordsflow"
  48. android:layout_width="match_parent"
  49. android:layout_height="match_parent"
  50. android:layout_below="@+id/search_layout" />
  51. <ImageView
  52. android:id="@+id/back_arrow"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:layout_alignParentBottom="true"
  56. android:layout_centerHorizontal="true"
  57. android:layout_marginBottom="14dp"
  58. android:src="@drawable/ic_back_arrow" />
  59. </RelativeLayout>

自己定义EditText

  1. public class DeletableEditText extends EditText{
  2. private Drawable mRightDrawable;
  3. private boolean isHasFocus;
  4. public DeletableEditText(Context context) {
  5. super(context);
  6. init();
  7. }
  8. public DeletableEditText(Context context, AttributeSet attrs) {
  9. super(context, attrs);
  10. init();
  11. }
  12. public DeletableEditText(Context context, AttributeSet attrs, int defStyle) {
  13. super(context, attrs, defStyle);
  14. init();
  15. }
  16. private void init(){
  17. //getCompoundDrawables:
  18. //Returns drawables for the left, top, right, and bottom borders.
  19. Drawable [] drawables=this.getCompoundDrawables();
  20. //取得right位置的Drawable
  21. //即我们在布局文件里设置的android:drawableRight
  22. mRightDrawable=drawables[2];
  23. //设置焦点变化的监听
  24. this.setOnFocusChangeListener(new FocusChangeListenerImpl());
  25. //设置EditText文字变化的监听
  26. this.addTextChangedListener(new TextWatcherImpl());
  27. //初始化时让右边clean图标不可见
  28. setClearDrawableVisible(false);
  29. }
  30. /**
  31. * 当手指抬起的位置在clean的图标的区域
  32. * 我们将此视为进行清除操作
  33. * getWidth():得到控件的宽度
  34. * event.getX():抬起时的坐标(改坐标是相对于控件本身而言的)
  35. * getTotalPaddingRight():clean的图标左边缘至控件右边缘的距离
  36. * getPaddingRight():clean的图标右边缘至控件右边缘的距离
  37. * 于是:
  38. * getWidth() - getTotalPaddingRight()表示:
  39. * 控件左边到clean的图标左边缘的区域
  40. * getWidth() - getPaddingRight()表示:
  41. * 控件左边到clean的图标右边缘的区域
  42. * 所以这两者之间的区域刚好是clean的图标的区域
  43. */
  44. @Override
  45. public boolean onTouchEvent(MotionEvent event) {
  46. switch (event.getAction()) {
  47. case MotionEvent.ACTION_UP:
  48. boolean isClean =(event.getX() > (getWidth() - getTotalPaddingRight()))&&
  49. (event.getX() < (getWidth() - getPaddingRight()));
  50. if (isClean) {
  51. setText("");
  52. }
  53. break;
  54. default:
  55. break;
  56. }
  57. return super.onTouchEvent(event);
  58. }
  59. private class FocusChangeListenerImpl implements OnFocusChangeListener{
  60. @Override
  61. public void onFocusChange(View v, boolean hasFocus) {
  62. isHasFocus=hasFocus;
  63. if (isHasFocus) {
  64. boolean isVisible=getText().toString().length()>=1;
  65. setClearDrawableVisible(isVisible);
  66. } else {
  67. setClearDrawableVisible(false);
  68. }
  69. }
  70. }
  71. //当输入结束后推断是否显示右边clean的图标
  72. private class TextWatcherImpl implements TextWatcher{
  73. @Override
  74. public void afterTextChanged(Editable s) {
  75. boolean isVisible=getText().toString().length()>=1;
  76. setClearDrawableVisible(isVisible);
  77. }
  78. @Override
  79. public void beforeTextChanged(CharSequence s, int start, int count,int after) {
  80. }
  81. @Override
  82. public void onTextChanged(CharSequence s, int start, int before,int count) {
  83. }
  84. }
  85. //隐藏或者显示右边clean的图标
  86. protected void setClearDrawableVisible(boolean isVisible) {
  87. Drawable rightDrawable;
  88. if (isVisible) {
  89. rightDrawable = mRightDrawable;
  90. } else {
  91. rightDrawable = null;
  92. }
  93. //使用代码设置该控件left, top, right, and bottom处的图标
  94. setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1],
  95. rightDrawable,getCompoundDrawables()[3]);
  96. }
  97. // 显示一个动画,以提示用户输入
  98. public void setShakeAnimation() {
  99. this.startAnimation(shakeAnimation(5));
  100. }
  101. //CycleTimes动画反复的次数
  102. public Animation shakeAnimation(int CycleTimes) {
  103. Animation translateAnimation = new TranslateAnimation(0, 10, 0, 10);
  104. translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
  105. translateAnimation.setDuration(1000);
  106. return translateAnimation;
  107. }
  108. }

自己定义FrameLayout

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

整个呈现效果还是满美观的,然后详细内容能够看源代码,拿来就能用

源代码地址:http://yunpan.cn/cdDvx3Zi9iA5m 訪问password b1b7

Android 好看的搜索界面,大赞Animation的更多相关文章

  1. Android 5.0(棒棒糖))十大新特性

    Android 5.0(棒棒糖))十大新特性 1. 全新Material Design设计风格 Android Lollipop全新的设计语言是受到了多种因素影响,是一种大胆的平面化创新.换句话说,谷 ...

  2. 精美的 ( Android, iPhone, iPad ) 手机界面设计素材和线框图设计工具

    在制作界面原型的时候,如果有现成的界面基础元素可以使用的话,设计师就可以非常快速的完成原型的制作,能够节省大量的时间和精力.在这篇文章, 我向大家分享45套非常有用的 UI 和 Wireframe 套 ...

  3. 好用的有多种样式的搜索界面创建UISearchBar

    之前看到一个别人封装的第三方PYSearch,相当于一个完整的搜索界面,今天在这里进行代码说明一下. 将PYSearch拖进项目或者使用Pods进行加库,我是直接拖进项目中进行使用 PYSearch库 ...

  4. 【随笔】Android应用市场搜索优化(ASO)

    参考了几篇网上的关于Android应用市场搜索优化(ASO)的分析,总结几点如下: 优化关键字:举例目前美团酒店在各Android市场上的关键字有“美团酒店.钟点房.团购.7天”等等,而酒店类竞对在“ ...

  5. Android Tab类型主界面 Fragment+TabPageIndicator+ViewPager

    文章地址: Android项目Tab类型主界面大总结 Fragment+TabPageIndicator+ViewPager 1.使用ViewPager + PagerAdapter 每个页面的内容都 ...

  6. Lucene搜索方式大合集

    package junit; import java.io.File; import java.io.IOException; import java.text.ParseException; imp ...

  7. Android渗透测试Android渗透测试入门教程大学霸

    Android渗透测试Android渗透测试入门教程大学霸 第1章  Android渗透测试 Android是一种基于Linux的自由及开放源代码的操作系统,主要用于移动设备,如智能手机.平板等.目前 ...

  8. Android 在安装完成界面,点击打开应用程序。在应用程序点击home键,再从桌面打开程序导致产生多个实例或者说程序被重复打开

    Android 在安装完成界面,点击打开应用程序.在应用程序点击home键,再从桌面打开程序导致产生多个实例或者说程序被重复打开. etong_123的专栏 - 博客频道 - CSDN.NET htt ...

  9. android FakeWindow的小应用大用途

    android FakeWindow的小应用大用途 在windowmanager里面有一个FakeWindow,细致一看也就是一个透明的应用覆盖到屏幕的最前端,这样有什么优点呢?首先我们还是从应用的需 ...

随机推荐

  1. FCC 基础JavaScript 练习3

    1.通过使用提供的变量参数:名词myNoun.形容词myAdjective.动词myVerb.副词myAdverb,来创建一个新的句子 result, function wordBlanks(myNo ...

  2. union与union all

    union在合并A和B表的时候会先将B表中的数据与A表进行比较,若A表中已存在,则忽略. union all合并时则不比较,直接将A表与B表进行合并. 因此,若已知A与B不存在重复数据,那么union ...

  3. codeforces_459D_(线段树,离散化,求逆序数)

    链接:http://codeforces.com/problemset/problem/459/D D. Pashmak and Parmida's problem time limit per te ...

  4. Exceptions & Errors - 异常与错误

    来源于 Ry’s Objective-C Tutorial - RyPress 一个学习Objective-C基础知识的网站. 个人觉得很棒,所以决定抽时间把章节翻译一下. 本人的英语水平有限,有让大 ...

  5. wpf 自定义Button按钮

    创建ButtonEx类 public class ButtonEx : Button { static ButtonEx() { DefaultStyleKeyProperty.OverrideMet ...

  6. 简单了解jdbcTemplate的queryForList是如何查询

    queryForList方法会调用query方法,同时会传入一个新的ColumnMapRowMapper对象 ArgumentPreparedStatementSetter对象只有一个Object[] ...

  7. Error LNK2019: unresolved external symbol C++模板类声明与定义链接错误问题

    编译器在编译模板时,并不会生成代码,只有遇到实例化的时候才会生成代码.因此,当我们只引用模板声明文件的时候,在实例化的对象时候,模板的定义问文件是不可见的,于是出现链接错误.例如: //A.h #pr ...

  8. Tensorflow学习笔记(1):tf.slice()函数使用

    tensorflow 当中的一个常用函数:Slice() def slice(input_, begin, size, name=None) 函数的功能是根据begin和size指定获取input的部 ...

  9. UVA-1589 象棋(模拟)

    题目:(传送门) 给出一个象棋的残局,下一步是黑棋走,判断黑棋是不是被将死. 思路: 读完这个题,知道是一个模拟题,然后想到用两个二维数组来模拟棋盘,一个(mp数组)用来存残局,一个(res数组)用来 ...

  10. zabbix+docker

    由于公司线上服务器数量太少,又要用于生产,领导让上zabbix,但熟知zabbix搭建需要LAMP或者LNMP,如果和生产服务器混搭的话,不方便管理,也怕出问题,所以就先使用docker方式搭建管理. ...