我创建了CustomView
扩展View
。创建一些CustomView
并使用动画移动和旋转它们。更改backgroundResource
并发生错误后,新背景不会填满所有CustomView
。请参阅代码:
clearAnimation();
AnimationSet animation = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(0, destX - srcX, 0, destY - srcY);
translateAnimation.setInterpolator(new LinearInterpolator());
translateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
animation.addAnimation(translateAnimation);
final float finalX = destX;
final float finalY = destY;
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation arg0) {
clearAnimation();
setX(finalX);
setY(finalY);
RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
rotateAnimation.setFillAfter(true);
startAnimation(rotateAnimation);
resetLayout();
mMoveStatus = false;
degree = degrees;
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
});
startAnimation(animation);
和resetLauyout:
lp = (LayoutParams) getLayoutParams();
if (lp == null) {
Log.d(TAG, "FIRST");
lp = new LayoutParams((int) cardW, (int) cardH);
lp.leftMargin = (int) mX;
lp.topMargin = (int) mY;
} else {
Log.d(TAG, "LAST");
lp.leftMargin = (int) mX;
lp.topMargin = (int) mY;
}
setLayoutParams(lp);
请帮帮我
答案 0 :(得分:1)
更改布局参数等内容时,请确保同时调用View.requestLayout()。我认为你不需要这个。您所需要的只是以下内容:
AnimationSet animation = new AnimationSet(true);
animation.setFillAfter(true);
TranslateAnimation translateAnimation = new TranslateAnimation(0, destX - srcX, 0, destY - srcY);
translateAnimation.setInterpolator(new LinearInterpolator());
translateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
animation.addAnimation(translateAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
rotateAnimation.setStartOffset((long) MGConstant.ANIMATE_DURATION);
animation.addAnimation(rotateAnimation);
startAnimation(animation);
答案 1 :(得分:1)
使用ObjectAnimator
根据您的要求为视图设置动画,在动画结束后维护视图,您不想更改LayoutParams
等。
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(customView,
"translation", 0, destX - srcX, 0, destY - srcY);
objectAnimator.setDuration((long) MGConstantANIMATE_DURATION);
ObjectAnimator rotation = ObjectAnimator.ofFloat(customView,
"rotation", 0, degree);
rotation .setDuration((long) MGConstantANIMATE_DURATION);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration((long) MGConstantANIMATE_DURATION);
animatorSet.play(objectAnimator).after(rotation);
customView.setBackgroundResource(R.drawable.your_image);
答案 2 :(得分:1)
使用下面的处理程序
更改窗口小部件属性 public void onAnimationEnd(Animation arg0) {
new Handler.post(new Runnable(){
clearAnimation();
setX(finalX);
setY(finalY);
RotateAnimation rotateAnimation = new RotateAnimation(0, degrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration((long) MGConstant.ANIMATE_DURATION);
rotateAnimation.setFillAfter(true);
startAnimation(rotateAnimation);
resetLayout();
mMoveStatus = false;
degree = degrees;
});
}
这将解决您的问题。