2012-12-19 45 views
6

我創建了CustomView擴展View。創建一些CustomView並使用動畫來移動和旋轉它們。改變後backgroundResource發生錯誤,新的背景未填滿全部CustomView。請參閱代碼:錯誤setBackground動畫後查看

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); 

請幫我

+1

錯誤? logcat在哪裏? – Egor

+0

行「backgroundResource」在哪裏?添加logcat錯誤,請 – Sulfkain

回答

1

當您更改佈局一樣東西parameteres,請確保您還呼籲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

爲動畫使用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); 
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; 
     }); 

    } 

這將解決您的問題。