Code example for AnimationSet

Methods: addAnimation, setAnimationListener, setDuration, setFillBefore, setInterpolator

0
//			Log.e("---", "disapper()-layParams.leftMargin="+layParams.leftMargin+ 
//					";layParams.topMargin="+layParams.topMargin); 
			AnimationSet animSet = getAnimationSet(xy,(width >> 1),(height >> 1),txtAnimOutType);
			txt.startAnimation(animSet);
			/************/ 
			animSet.setFillBefore(true);
			/************/ 
			animSet.setAnimationListener(new AnimationListener() {
				 
				public void onAnimationStart(Animation animation) {
				} 
				 
				public void onAnimationRepeat(Animation animation) {
				} 
				 
				public void onAnimationEnd(Animation animation) {
					txt.setOnClickListener(null);
					txt.setClickable(true);
					txt.setVisibility(View.GONE);
				} 
			}); 
		} 
	} 
	 
	private boolean show() { 
		if(width > 0 && height > 0 && vecKeywords != null && vecKeywords.size() > 0 && enableShow){
//			Log.e("---","show()-width="+width+",height="+height); 
			enableShow = false;
			lastStartAnimationTime = System.currentTimeMillis();
			int xCenter = width >> 1, yCenter = height >> 1;
//			Log.e("---","show()-xCenter="+xCenter+",yCenter="+yCenter); 
			int size = vecKeywords.size();
			int xItem = width / size , yItem = height / size;
//			Log.e("---","show()-xItem="+xItem+",yItem="+yItem); 
			LinkedList<Integer> listX = new LinkedList<Integer>(), listY = new LinkedList<Integer>();
			for (int i = 0; i < size; i++) {
				listX.add(i*xItem);
				listY.add(i*yItem + (yItem >> 2));
			} 
			LinkedList<TextView> listTxtTop = new LinkedList<TextView>();
			LinkedList<TextView> listTxtBottom = new LinkedList<TextView>();
			for (int i = 0; i < size; i++) {
				String keyword = vecKeywords.get(i);
				//随机颜色 
				int ranColor =  random.nextInt(4);
				int color = Color.WHITE;
				if(ranColor == 0){
					color = Color.WHITE;
				}else if(ranColor == 1){
					color = Color.GREEN;
				}else if(ranColor == 2){
					color = Color.RED;
				}else if(ranColor == 3){
					color = Color.BLUE;
				} 
				//随机位置,粗糙 
				int xy[] =randomXY(random,listX,listY,xItem);
				//随机字体大小 
				int txtSize = TEXT_SIZE_MIN ;//+ random.nextInt(TEXT_SIZE_MAX - TEXT_SIZE_MIN + 1);
				//实例化Textview 
				final TextView txt = new TextView(getContext());
				txt.setOnClickListener(itemClickListener);
				txt.setText(keyword);
				txt.setTextColor(color);
				txt.setTextSize(TypedValue.COMPLEX_UNIT_SP,txtSize);
				txt.setShadowLayer(2, 2, 2, 0xff696969);
				txt.setGravity(Gravity.CENTER);
				txt.setEllipsize(TruncateAt.MIDDLE);
				txt.setSingleLine(true);
				txt.setEms(10);
				//获取文本长度 
				Paint paint = txt.getPaint();
//				System.out.println("KKKKKKKKKKKKKKKKeyword = "+keyword); 
				int strWidth = (int) Math.ceil(paint.measureText(keyword));
				xy[IDX_TXT_LENGTH] = strWidth;
				if(xy[IDX_X] + strWidth > width - (xItem >> 1)){
					int baseX = width - strWidth;
					xy[IDX_X] = baseX - xItem + random.nextInt(xItem>>1);
				}else if(xy[IDX_X] == 0){
					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);
				} 
			} 
			attch2Screen(listTxtTop,xCenter,yCenter,yItem);
			attch2Screen(listTxtBottom,xCenter,yCenter,xItem);
			return true; 
		} 
		return false; 
	} 
 
	private void attch2Screen(LinkedList<TextView> listTxt, int xCenter,
			int yCenter, int yItem) {
		int size = listTxt.size();
//		Log.e("---","attch2Screen()-size="+size); 
		sortXYList(listTxt,size);
		for (int i = 0; i < size; i++) {
			TextView txt = listTxt.get(i);
			int[] iXY = (int[]) txt.getTag();
			//第二次修??修正Y轴坐?? 
			int yDistance = iXY[IDX_Y] - yCenter;
			//对于????近中心的,其??不会大于yItem 
			//对于可以????下降到中心点??则该值也是其应调整的大小 
			int yMove = Math.abs(yDistance);
			inner:for (int k = i - 1; k >= 0; k--) {
				int[] kXY = (int[]) listTxt.get(k).getTag();
				int startX = kXY[IDX_X];
				int endX = startX + kXY[IDX_TXT_LENGTH];
                //y轴一中心点为分隔??在同????    
				if(yDistance * (kXY[IDX_Y] - yCenter) > 0){
					if(isXMixed(startX, endX, iXY[IDX_X], iXY[IDX_X] + iXY[IDX_TXT_LENGTH])){
						int tmpMove = Math.abs(iXY[IDX_Y] - kXY[IDX_Y]);
						if(tmpMove > yItem){
							yMove = tmpMove;
						}else if(yMove > 0){
							yMove = 0;
						} 
						break inner; 
					} 
				} 
			} 
			if(yMove > yItem){
				int maxMove = yMove - yItem;
				int randomMove = 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);
				//已经调整过的Y????在排?? 
				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);
			//动画 
			AnimationSet animSet = getAnimationSet(iXY, xCenter, yCenter, txtAnimINType);
			txt.startAnimation(animSet);
		} 
	} 
	private AnimationSet getAnimationSet(int[] xy, int xCenter, int yCenter,
			int type) {
		AnimationSet animSet = new AnimationSet(true);
		animSet.setInterpolator(interpolator);
		if(type == OUTSIDE_TO_LOCATION){
			animSet.addAnimation(animAlpha2Opaque);
			animSet.addAnimation(animScaleLarge2Normal);
			TranslateAnimation translate = new TranslateAnimation(
					(xy[IDX_X] +(xy[IDX_TXT_LENGTH]>>1)-xCenter)<<1, 0, (xy[IDX_Y] -yCenter)<<1, 0);
			animSet.addAnimation(translate);
		}else if (type == LOCATION_TO_OUTSIDE){
			animSet.addAnimation(animAlpha2Transparent);
			animSet.addAnimation(animScaleNormal2Large);
			TranslateAnimation translate = new TranslateAnimation(
					0, (xy[IDX_X] +(xy[IDX_TXT_LENGTH]>>1)-xCenter)<<1, 0, (xy[IDX_Y] -yCenter)<<1);
			animSet.addAnimation(translate);
		}else if (type == LOCATION_TO_CENTER){
			animSet.addAnimation(animAlpha2Transparent);
			animSet.addAnimation(animScaleNormal2Zero);
			TranslateAnimation translate = new TranslateAnimation(
					0, xCenter - xy[IDX_X], 0, yCenter - xy[IDX_Y]);
			animSet.addAnimation(translate);
		}else if (type == CENTER_TO_LOCATION){
			animSet.addAnimation(animAlpha2Opaque);
			animSet.addAnimation(animScaleZero2Normal);
			TranslateAnimation tranlate = new TranslateAnimation(
					xCenter - xy[IDX_X], 0, yCenter - xy[IDX_Y], 0);
			animSet.addAnimation(tranlate);
		} 
		animSet.setDuration(animDuration);
		animSet.setFillBefore(true);
		return animSet;
	} 
	private void sortXYList(LinkedList<TextView> listTxt, int endIdx) {
		for (int i = 0; i < endIdx; i++) {
			for (int k = i+1; k < endIdx; k++) {