2012-01-02 47 views
11

在android中如何使圖像從一個點使用動畫增長?在android中如何使圖像從一個點使用動畫增長?

我的意思是說...我有一個按鈕..我想要的是,當我點擊那個按鈕時,我的圖像必須增長(升序),從這一點開始變得越來越大......再次我再次點擊那個按鈕,它必須崩潰越來越小,結束在這一點

任何人都可以幫助我這樣做使用android動畫? 我是Android新手

回答

32

這可以使用視圖動畫實用程序來實現。該秤從100%到140%的圖像1秒鐘

將在水庫下面的文件/動畫/ scale.xml

<?xml version="1.0" encoding="utf-8"?> 
<set android:shareInterpolator="false"  
xmlns:android="http://schemas.android.com/apk/res/android"> 
<scale 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fromXScale="1.0" 
    android:toXScale="1.4" 
    android:fromYScale="1.0" 
    android:toYScale="1.4" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:fillAfter="false" 
    android:duration="1000" /> 
</set> 

Java代碼

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final View view = findViewById(R.id.imageView1); 

    final Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale); 

    Button button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      view.startAnimation(anim); 
     } 
    }); 
} 
+0

u能xplain我ñ細節...? – user1125690 2012-01-02 04:51:15

+1

更多描述將會很棒! – MKJParekh 2012-01-02 04:52:03

+1

修改了回覆 – 2012-01-02 05:51:32

2

我建議你看看在這個崗位SO: Android: Expand/collapse animation

public class DropDownAnim extends Animation { 
int targetHeight; 
View view; 
boolean down; 

public DropDownAnim(View view, int targetHeight, boolean down) { 
    this.view = view; 
    this.targetHeight = targetHeight; 
    this.down = down; 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    int newHeight; 
    if (down) { 
     newHeight = (int) (targetHeight * interpolatedTime); 
    } else { 
     newHeight = (int) (targetHeight * (1 - interpolatedTime)); 
    } 
    view.getLayoutParams().height = newHeight; 
    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

thnx for ur幫助..我能夠實現它與規模動漫 – user1125690 2012-01-11 09:38:00

+1

這對我有用!謝謝 – Rako 2014-05-27 15:58:42

1

您可以Rajdeep Dua的答案去,如果你想,直到你打電話clearAnimation(保持放大的圖像)動畫android:fillAfter="true"在XML像

<?xml version="1.0" encoding="utf-8"?> 
<set 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillAfter="true> 
<scale 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fromXScale="1.0" 
    android:toXScale="1.4" 
    android:fromYScale="1.0" 
    android:toYScale="1.4" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:fillAfter="false" 
    android:duration="1000" /> 
</set> 
0

與Android 3.0,首選的方式開始的動畫意見是 使用android.animation包APIs.These Animator基類來改變觀看對象的實際性能,....

或者你可以使用一個ViewPropertyAnimator簡單的事情 - 圖像按鈕,生長一段1000毫秒到1.4×它的大小:

imageButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     imageButton.animate(). 
     scaleX(1.4f). 
     scaleY(1.4f). 
     setDuration(1000).start(); 
    } 
});