2012-12-14 113 views
43

我改變的形象圖的左邊空白處按以下方式留有餘量。任何線索?更改Android使用動畫

我試了一下:

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat (image , "x" , VALUE); 
objectAnimator.start(); 

這完美的作品,作爲圖像移動到動畫指定的X值,無論其layoutParams.leftMargin的值保持不變!所以我不能使用這種方法,因爲如果我嘗試使用值爲100的objectAnimator將值layoutParams.leftMargin更改爲100,則應用的值不正確(應用了200而不是100,效果如果objectAnimator仍然eventhough我設置左邊距以下方式:

layoutParams.leftMargin = 100; 

回答

117

使用動畫類,而不是objectAnimator

final int newLeftMargin = <some value>; 
Animation a = new Animation() { 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     LayoutParams params = yourView.getLayoutParams(); 
     params.leftMargin = (int)(newLeftMargin * interpolatedTime); 
     yourView.setLayoutParams(params); 
    } 
}; 
a.setDuration(500); // in ms 
yourView.startAnimation(a); 

請注意,你笑使用正確的LayoutParams類,即如果你的觀點是LinearLayout的孩子,那麼PARAMS應該是LinearLayout.LayoutParams

+0

謝謝你指點我在正確的方向! :) – Leeeeeeelo

+12

不要忘記設置持續時間,我有一些問題,直到我設置持續時間:'a.setDuration(duration);' –

+0

當我嘗試使用params.leftMargin時出現錯誤。顯然這不是一個領域 – Soroush

68

我來了這個問題,但我不能使用它,因爲我想動畫的負值邊緣從0,所以我用user1991679答案valueAnimater基地:

final View animatedView = view.findViewById(R.id.animatedView); 
final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) animatedView.getLayoutParams(); 
ValueAnimator animator = ValueAnimator.ofInt(params.bottomMargin, 0); 
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    @Override 
    public void onAnimationUpdate(ValueAnimator valueAnimator) 
    { 
     params.bottomMargin = (Integer) valueAnimator.getAnimatedValue(); 
     animatedView.requestLayout(); 
    } 
}); 
animator.setDuration(300); 
animator.start(); 

您必須根據animatedView容器改變LinearLayout.LayoutParams。 也可以使用沒有ValueAnimator的舊版本的nineoldandroid。

+0

您可以使用Animation類將邊距從負數動畫到0。沒有任何限制。 – user1991679

+0

@ user1991679以及如何使用「動畫」類從400到200生成動畫? – StuStirling

+3

假設200是newMargin(我們應該動畫的邊距),而400是currentMargin(我們將從此開始計時)。在這種情況下,代碼將如下所示: @Override protected void applyTransformation(float interpolatedTime,Transformation t){ LayoutParams params = yourView.getLayoutParams(); params.leftMargin = currentMargin - (int)((currentMargin - newMargin)* interpolatedTime); yourView.setLayoutParams(params); } – user1991679

-2

您可以使用以下

image.animate().setDuration(durationIn).translationXBy(offsetFloat).start(); 

您還可以添加.setInterpolator(new BounceInterpolator())改變動畫的外觀。

0

從user1991679答案是偉大的,但如果你需要從其他任何值,但0插值保證金,你需要用它在你的計算:

ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams(); 
final int bottomMarginStart = params.bottomMargin; // your start value 
final int bottomMarginEnd = <your value>; // where to animate to 
Animation a = new Animation() { 
    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     ViewGroup.MarginLayoutParams params = (MarginLayoutParams) mBottomLayout.getLayoutParams(); 
     // interpolate the proper value 
     params.bottomMargin = bottomMarginStart + (int) ((bottomMarginEnd - bottomMarginStart) * interpolatedTime); 
     mBottomLayout.setLayoutParams(params); 
    } 
}; 
a.setDuration(300); 
mBottomLayout.startAnimation(a); 

在我來說,我需要動畫的「進入屏幕」動畫,從「-48dp」到0。沒有開始值,動畫總是0,因此跳躍,而不是動畫視圖。解決方案是插入偏移量並將其添加到原始值。