我使用TranslateAnimation移动按钮,它工作正常,但在动画开始之前奇怪的闪烁apear。在短时间内,动画视图移动到更高的位置。之后视图回到正常位置并开始动画。
我使用RelativeLayout并移动7个按钮。
public void moveView(final int rotationType) {
AnimationSet animationList[] = new AnimationSet[buttonAmount];
for (int i = 0; i < buttonAmount; i++) {
AnimationSet animationsSet = new AnimationSet(false);
animationsSet.setFillAfter(true);
boolean visible = true;
boolean hide = false;
boolean show = false;
ImageButton button = buttons.get(i);
int position = i + movementCount;
if (position < 0) {
position = getLastButtonId();
} else if (position > getLastButtonId()) {
position = position - buttonAmount;
}
int cL = getDP(leftCoord.get(position));
int cB = getDP(bottomCoord.get(position));
// TODO left right
if (rotationType == RIGHTBUTTON) {
position--;
} else if (rotationType == LEFTBUTTON) {
position++;
}
if (position < 0) {
position = getLastButtonId();
} else if (position > getLastButtonId()) {
position = position - buttonAmount;
}
int nL = getDP(leftCoord.get(position));
int nB = getDP(bottomCoord.get(position));
int left = cL - nL;
int bot = cB - nB;
long time = 1000;
if (rotationType == LEFTBUTTON) {
if (cL == getDP(leftArrowLeft) && cB == getDP(leftArrowBottom)) {
hide = false;
visible = true;
show = true;
}
if (nL == getDP(leftArrowLeft) && nB == getDP(leftArrowBottom)) {
hide = false;
visible = false;
time = 0;
}
if (nL == getDP(rightArrowLeft)
&& nB == getDP(rightArrowBottom)) {
hide = true;
}
} else
if (rotationType == RIGHTBUTTON) {
if (cL == getDP(rightArrowLeft)
&& cB == getDP(rightArrowBottom)) {
hide = false;
visible = true;
show = true;
}
if (nL == getDP(leftArrowLeft) && nB == getDP(leftArrowBottom)) {
hide = true;
}
if (nL == getDP(rightArrowLeft)
&& nB == getDP(rightArrowBottom)) {
hide = false;
visible = false;
show = false;
time = 0;
}
}
TranslateAnimation trans = new TranslateAnimation(left, 0, bot, 0);
final int l = nL;
final int b = nB;
final ImageButton ib = button;
final boolean isVisible = visible;
final boolean isHide = hide;
final boolean isShow = show;
final int count = i;
final RelativeLayout layout = main;
button.clearAnimation();
trans.setAnimationListener(new AnimationListener() {
RelativeLayout.LayoutParams params;
@Override
public void onAnimationStart(Animation animation) {
params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.leftMargin = l;
params.topMargin = b;
ib.setLayoutParams(params);
if (isVisible) {
int tag = (Integer) ib.getTag();
ib.setImageResource(resourcesList[tag]);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (!isVisible) {
ib.setImageDrawable(null);
}
}
});
trans.setDuration(time);
animationsSet.addAnimation(trans);
if (show) {
Animation animShow = new AlphaAnimation(.0f, 1f);
animShow.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// ib.setVisibility(View.VISIBLE);
// buttons.get(count).setVisibility(View.VISIBLE);
}
});
animShow.setDuration(1000);
animationsSet.addAnimation(animShow);
} else if (hide) {
Animation hideShow = new AlphaAnimation(1.0f, .0f);
hideShow.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
hideShow.setDuration(1000);
animationsSet.addAnimation(hideShow);
}
button.startAnimation(animationsSet);
}
if (rotationType == RIGHTBUTTON) {
movementCount--;
} else if (rotationType == LEFTBUTTON) {
movementCount++;
}
if (movementCount < 0) {
movementCount = getLastButtonId();
} else if (movementCount > getLastButtonId()) {
movementCount = 0;
}
}