2012-12-07 68 views
1

你好我試圖動畫android的視圖的高度的高度說,每5秒: -動畫的android視圖

  1. 高度從0到5
  2. 高度去5至10
  3. 高度變爲10〜3等

我使用下面的代碼: -

public class ShowAnimation extends Animation{ 
    float finalHeight; 
    View imageview; 

    public ShowAnimation(View view,float deltaheight){ 
     this.imageview=view; 
     this.finalHeight=deltaheight; 
    } 

    protected void applyTransformation(float interpolatedtime,Transformation t){ 
     imageview.getLayoutParams().height=(int)(finalHeight*interpolatedtime); 
     imageview.requestLayout(); 
    } 
    @Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     super.initialize(width, height, parentWidth, parentHeight); 
    } 

    @Override 
    public boolean willChangeBounds() { 
     return true; 
    } 
    } 

並初始化它像這樣: -

Animation anidelta = new ShowAnimation(delta, deltaheight); 
anidelta.setDuration(500/* animation time */); 
delta.startAnimation(anidelta); 

但是有了這個,我得到了以下: -

  1. 高度從0到5
  2. 高度從0到10
  3. 高度去從0到3

我希望高度能夠從它以前的高度動畫而不是從0開始。 可有人請點擊這裏

EDIT1幫助我: - 我這樣做

Animation anidelta = new ShowAnimation(delta, deltaheight); 
anidelta.setDuration(500/* animation time */); 
anidelta.setFillAfter(true); 
delta.startAnimation(anidelta); 

但它仍然從0到動畫的newheight。

+1

set animation.setFillAfter(true);它應該解決問題。 –

+0

@ShreyaShah我做到了,但不起作用。請參閱我的編輯 – Rasmus

回答

-1

嘗試動畫集。我不確定這是否是你想要的。

動畫1爲:高度從0到5
動畫2爲:高度從0到10
動畫3爲:高度從0到3

public void applyAnimation(ImageView ivDH) { 
    System.out.println("Inside applyAnimation()"); 
    scaleAnim1 = new ScaleAnimation(0, 5, 0, 5); 
    scaleAnim1.setDuration(5000); 
    scaleAnim1.setRepeatCount(0); 
    scaleAnim1.setInterpolator(new AccelerateDecelerateInterpolator()); 
    scaleAnim1.setFillAfter(true); 

    scaleAnim2 = new ScaleAnimation(-5, 10, -5, 10); 
    scaleAnim2.setDuration(5000); 
    scaleAnim2.setRepeatCount(0); 
    scaleAnim2.setInterpolator(new AccelerateDecelerateInterpolator()); 
    scaleAnim2.setFillAfter(true); 

    scaleAnim3 = new ScaleAnimation(10, 3, 10, 3); 
    scaleAnim3.setDuration(5000); 
    scaleAnim3.setRepeatCount(0); 
    scaleAnim3.setInterpolator(new AccelerateDecelerateInterpolator()); 
    scaleAnim3.setFillAfter(true); 

    AnimationSet animationSet = new AnimationSet(true); 
    animationSet.addAnimation(scaleAnim1); 
    animationSet.addAnimation(scaleAnim2); 
    animationSet.addAnimation(scaleAnim3); 
    animationSet.setDuration(15000); 
    animationSet.setFillAfter(true); 

    ivDH.clearAnimation(); 
    ivDH.startAnimation(animationSet); 
} 
0

你需要一個額外的標誌來告訴你applyTransformation方法是否要增加或減少的觀點高度。

添加一個標誌來構造和標誌保存爲一個實例變量,並使用該標誌來決定是否增加或減少的觀點高度:

public class ShowAnimation { 
    // other instance variables 
    private boolean increaseHeight; 

    public ShowAnimation(View view, float deltaheight, boolean increaseHeight) { 
     // other initializations 
     this.increaseHeight = increaseHeight; 
    } 

    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     if (increaseHeight) 
      imageview.getLayoutParams().height = (int) (finalHeight * interpolatedTime); 
     else { 
      imageview.getLayoutParams().height = (int) (finalHeight * (1 - interpolatedTime)); 
     } 
     imageview.requestLayout(); 
    } 

} 
+0

我不認爲這會有所幫助,因爲即使我使用increaseHeight爲true,它仍然從0到最終高度動畫 – Rasmus

+1

不要想,試試吧! – neevek

+0

@neevek如何實現這個動畫http://www.dailymotion.com/video/x2qx2if – Erum

7

確定,所以這是怎麼我終於解決了它: -

public class ResizeAnimation extends Animation 
{ 
    View view; 
int startH; 
int endH; 
int diff; 

public ResizeAnimation(View v, int newh) 
{ 
    view = v; 
    startH = v.getLayoutParams().height; 
    endH = newh; 
    diff = endH - startH; 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) 
{ 
    view.getLayoutParams().height = startH + (int)(diff*interpolatedTime); 
    view.requestLayout(); 
} 

@Override 
public void initialize(int width, int height, int parentWidth, int parentHeight) 
{ 
    super.initialize(width, height, parentWidth, parentHeight); 
} 

@Override 
public boolean willChangeBounds() 
{ 
    return true; 
}} 
0

我知道這個職位還沒有超過一年已經活躍了,但我會寄我想反正是動畫視圖的高度的最簡單方法。

,而不用聲明動畫類的,你可以使用可從API級別11

使用這個API View.setScaleY,你可以指定視圖的範圍內規模從0-1(0表示沒有高度和1意味着原高度)在期望的秒鐘內。

在XML中,將視圖的高度設置爲其最大高度,然後在對視圖進行充氣時(例如,在活動#的onCreate()或片段#onViewCreated()),您可以

yourView.setScaleY(0); 

然後在5秒鐘後,你可以設置

yourView.setScaleY(0.5f); 

這將立即擴展您的視圖的高度。如果你想用動畫縮放視圖,你可以舉例如下:

yourView.animate().scaleY(0.5f).setDuration(200).start(); 

我希望它有幫助。