我读过很多关于android tranlsation动画的文章。而且我知道只有一种方法可以使用LayoutParams()
,但我需要设置五个按钮的动画。我已经创建了可以释放这些按钮的示例但我的问题是按钮没有焦点。
如何使用LayoutParams()
这些按钮?
这里是示例代码:
private AnimationSet animate(final Button button, long fromXValue,
final long toXValue, long fromYDelta, final long toYValue,
long startOffset){...}
float angle = 222.2f;
int angle2 = 136;
long x1 = Math.round((mRadius * Math.cos(angle)));
long y1 = Math.round((mRadius * Math.sin(angle)));
long x2 = (int) (mRadius * Math.cos(angle2));
long y2 = (int) (mRadius * Math.sin(angle2));
if (isShown && isFinished) {
animationSet1 = animate(button2, 0, 0, mRadius, 0, 400);
animationSet2 = animate(button3, x1, 0, y1, 0, 300);
animationSet3 = animate(button4, -mRadius, 0, 0, 0, 200);
animationSet4 = animate(button5, x2, 0, y2, 0, 100);
animationSet5 = animate(button6, 0, 0, -mRadius, 0, 0);
isShown = false;
} else if (!isShown && isFinished) {
animationSet1 = animate(button2, 0, 0, 0, mRadius, 0);
animationSet2 = animate(button3, 0, x1, 0, y1, 100);
animationSet3 = animate(button4, 0, -mRadius, 0, 0, 200);
animationSet4 = animate(button5, 0, x2, 0, y2, 300);
animationSet5 = animate(button6, 0, 0, 0, -mRadius, 400);
isShown = true;
}
图片显示了它应该如何显示
EDITED
我发现使用LayoutParams()
的解决方案几乎没问题。这里有样本:
private void animate(final Button button, long fromXValue,
final long toXValue, long fromYDelta, final long toYValue,
long startOffset) {
AnimationSet animationSet = new AnimationSet(false);
TranslateAnimation translateAnimation = new TranslateAnimation(
fromXValue, toXValue, fromYDelta, toYValue);
RotateAnimation rotateAnimation = null;
int dp = getPixelsFromDp(20);
if (!isShown) {
rotateAnimation = new RotateAnimation(-180, 0, dp, dp);
} else {
rotateAnimation = new RotateAnimation(0, -180, dp, dp);
}
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.setDuration(100);
// animationSet.setFillAfter(true);
// animationSet.setFillEnabled(true);
animationSet.setStartOffset(startOffset);
animationSet.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
isFinished = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
isFinished = true;
Log.i("animation", "animation");
RelativeLayout.LayoutParams params = (LayoutParams) button
.getLayoutParams();
params.rightMargin -= toXValue;
params.topMargin += toYValue;
button.setLayoutParams(params);
}
});
button.startAnimation(animationSet);
}
按钮有焦点。但仍然有一个不好的效果:在动画按钮显示两次。按钮放置在右侧的印象以及之后再次显示动画。