2012-12-19 26 views
6

Tôi tạo một số CustomView kéo dài View. Tạo một số CustomView và sử dụng hoạt ảnh để di chuyển và xoay chúng. Sau khi thay đổi backgroundResource và lỗi xảy ra, nền mới không điền tất cả CustomView. Xin vui lòng xem mã:Lỗi đặtChế độ xem sau khi hoạt ảnh

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

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

Xin hãy giúp tôi

+1

Lỗi? Logcat ở đâu? – Egor

+0

Dòng "backgroundResource" ở đâu? thêm lỗi logcat, xin – Sulfkain

Trả lời

1

Khi bạn thay đổi những thứ như parameteres bố trí, chắc chắn bạn cũng gọi View.requestLayout(). Tôi không nghĩ rằng bạn cần điều này ở đây mặc dù. Tất cả bạn cần là như sau:

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

sử dụng ObjectAnimator cho Animate quan điểm, đối với yêu cầu của bạn với, Nó duy trì quan điểm sau khi kết thúc hoạt hình, bạn không muốn thay đổi LayoutParams, vv

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

thay đổi thuộc tính tiện ích con của bạn bằng cách sử dụng trình xử lý có bên dưới

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

    } 

Điều này sẽ giải quyết được sự cố của bạn.

Các vấn đề liên quan